refactor: make declaration/definition URLs from Source.Repo
Alan Pearce alan@alanpearce.eu
Mon, 20 May 2024 23:14:40 +0200
3 files changed, 8 insertions(+), 19 deletions(-)
M internal/importer/options.go → internal/importer/options.go
@@ -124,7 +124,7 @@ switch decl := reflect.ValueOf(decl); decl.Kind() { case reflect.String: s := decl.String() - link, err := MakeChannelLink(i.source.Channel, i.source.Repo.Revision, s) + link, err := MakeChannelLink(i.source.Repo, s) if err != nil { errs <- errors.WithMessagef(err, "could not make a channel link for channel %s, revision %s and subpath %s",
M internal/importer/package.go → internal/importer/package.go
@@ -115,7 +115,6 @@ defer close(results) defer close(errs) defer cancel() - userRepo := i.source.Repo.Owner + "/" + i.source.Repo.Repo outer: for mv := range i.dec.Stream() { var err error @@ -240,7 +239,7 @@ MainProgram: i.pkg.Meta.MainProgram, Platforms: i.pkg.Meta.Platforms, Licenses: licenses, Maintainers: maintainers, - Definition: makeGitHubFileURL(userRepo, "", subpath, line), + Definition: makeRepoURL(i.source.Repo, subpath, line), } } }()
M internal/importer/utils.go → internal/importer/utils.go
@@ -36,11 +36,12 @@ return "very strange" } -func makeGitHubFileURL(userRepo string, ref string, subPath string, line string) string { +func makeRepoURL(repo config.Repository, subPath string, line string) string { + ref := repo.Revision if ref == "" { ref = "master" } - url, _ := url.JoinPath("https://github.com/", userRepo, "blob", ref, subPath) + url, _ := url.JoinPath("https://github.com/", repo.Owner, repo.Repo, "blob", ref, subPath) if line != "" { url = url + "#L" + line } @@ -48,21 +49,10 @@ 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) (*nix.Link, error) { - if channelRepoMap[channel] == "" { - return nil, fmt.Errorf("don't know what repository relates to channel <%s>", channel) - } - +func MakeChannelLink(repo config.Repository, subPath string) (*nix.Link, error) { return &nix.Link{ - Name: fmt.Sprintf("<%s/%s>", channel, subPath), - URL: makeGitHubFileURL(channelRepoMap[channel], ref, subPath, ""), + Name: fmt.Sprintf("<%s/%s>", repo.Repo, subPath), + URL: makeRepoURL(repo, subPath, ""), }, nil }