refactor: move repository file URL generation to Repository method
3 files changed, 26 insertions(+), 24 deletions(-)
M internal/config/repository.go → internal/config/repository.go
@@ -22,6 +22,30 @@ Repo string Revision string `toml:"-"` } +func (r *Repository) GetFileURL(path string, line ...string) (string, errors.E) { + switch r.Type { + case GitHub: + ref := r.Revision + if ref == "" { + ref = "master" + } + u, err := url.JoinPath("https://github.com/", r.Owner, r.Repo, "blob", ref, path) + if err != nil { + return "", errors.Wrap(err, "failed to join path") + } + if len(line) > 0 { + u += fmt.Sprintf("#L%s", line[0]) + } + + return u, nil + default: + return "", errors.Errorf( + "don't know how to generate a repository URL for %s", + r.Type.String(), + ) + } +} + func (r *Repository) String() string { switch r.Type { case GitHub:
M internal/importer/package.go → internal/importer/package.go
@@ -309,7 +309,7 @@ if defURL.IsAbs() { definition = i.pkg.Meta.Position } else { subpath, line, _ := strings.Cut(i.pkg.Meta.Position, ":") - definition, err = makeRepoURL(i.source.Repo, subpath, line) + definition, err = i.source.Repo.GetFileURL(subpath, line) if err != nil { errs <- errors.WithMessagef(err, "failed to make repo URL for package %s", i.pkg.Name) }
M internal/importer/utils.go → internal/importer/utils.go
@@ -3,7 +3,6 @@ import ( "fmt" "io" - "net/url" "strings" "go.alanpearce.eu/searchix/internal/config"@@ -34,29 +33,8 @@ return "very strange" } -func makeRepoURL(repo config.Repository, subPath string, line string) (string, errors.E) { - switch repo.Type { - case config.GitHub: - ref := repo.Revision - if ref == "" { - ref = "master" - } - url, _ := url.JoinPath("https://github.com/", repo.Owner, repo.Repo, "blob", ref, subPath) - if line != "" { - url = url + "#L" + line - } - - return url, nil - default: - return "", errors.Errorf( - "don't know how to generate a repository URL for %s", - repo.Type.String(), - ) - } -} - func MakeChannelLink(repo config.Repository, subPath string) (*nix.Link, errors.E) { - url, err := makeRepoURL(repo, subPath, "") + url, err := repo.GetFileURL(subPath) if err != nil { return nil, err }