all repos — searchix @ ad670006a58faa22a183212f3b68189b6ab8d5fa

Search engine for NixOS, nix-darwin, home-manager and NUR users

refactor: move repository file URL generation to Repository method

Alan Pearce
commit

ad670006a58faa22a183212f3b68189b6ab8d5fa

parent

b767d7bc25ef13c86c126dbbf39f05604750963c

1 file changed, 24 insertions(+), 0 deletions(-)

changed files
M internal/config/repository.gointernal/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: