From f79f1deb956cd3f5788e7f602d883417badaebbd Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Thu, 16 May 2024 17:03:55 +0200 Subject: refactor(importer): move functions to utils file --- internal/importer/utils.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 internal/importer/utils.go (limited to 'internal/importer/utils.go') diff --git a/internal/importer/utils.go b/internal/importer/utils.go new file mode 100644 index 0000000..f7ba36a --- /dev/null +++ b/internal/importer/utils.go @@ -0,0 +1,60 @@ +package importer + +import ( + "fmt" + "net/url" + "searchix/internal/options" + + "github.com/bcicen/jstream" +) + +func ValueTypeToString(valueType jstream.ValueType) string { + switch valueType { + case jstream.Unknown: + return "unknown" + case jstream.Null: + return "null" + case jstream.String: + return "string" + case jstream.Number: + return "number" + case jstream.Boolean: + return "boolean" + case jstream.Array: + return "array" + case jstream.Object: + return "object" + } + + return "very strange" +} + +func makeGitHubFileURL(userRepo string, ref string, subPath string, line string) string { + if ref == "" { + ref = "master" + } + url, _ := url.JoinPath("https://github.com/", userRepo, "blob", ref, subPath) + if line != "" { + url = url + "#L" + line + } + + return url +} + +// make configurable? +var channelRepoMap = map[string]string{ + "nixpkgs": "NixOS/nixpkgs", + "nix-darwin": "LnL7/nix-darwin", + "home-manager": "nix-community/home-manager", +} + +func MakeChannelLink(channel string, ref string, subPath string) (*options.Link, error) { + if channelRepoMap[channel] == "" { + return nil, fmt.Errorf("don't know what repository relates to channel <%s>", channel) + } + + return &options.Link{ + Name: fmt.Sprintf("<%s/%s>", channel, subPath), + URL: makeGitHubFileURL(channelRepoMap[channel], ref, subPath, ""), + }, nil +} -- cgit 1.4.1