From cedd863bd16fdfa62c06f029b36ff42a0c1fe656 Mon Sep 17 00:00:00 2001 From: Xavier Calland Date: Fri, 26 Apr 2024 10:05:29 +0200 Subject: [PATCH 1/2] zoekt-mirror-gerrit: allow to use reponame without host in the index The repo is indexed only with the reponame which is more user-friendly in the search when there is onely one host Filter string will be "r:my-repo" instead of "r:my-gerrit-sever.com/my-repo" --- cmd/zoekt-mirror-gerrit/main.go | 40 ++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/cmd/zoekt-mirror-gerrit/main.go b/cmd/zoekt-mirror-gerrit/main.go index 853da1767..523d9537d 100644 --- a/cmd/zoekt-mirror-gerrit/main.go +++ b/cmd/zoekt-mirror-gerrit/main.go @@ -73,9 +73,40 @@ func newLoggingClient() *http.Client { } } +type RepoFormatEnum string + +const ( + WithServerHost RepoFormatEnum = "WithServerHost" + WithoutServerHost RepoFormatEnum = "WithoutServerHost" +) + +// Implementing flag.Value interface +type RepoFormatEnumValue struct { + value *RepoFormatEnum +} + +func (l *RepoFormatEnumValue) String() string { + return string(*l.value) +} + +func (l *RepoFormatEnumValue) Set(s string) error { + switch s { + case string(WithServerHost): + *l.value = WithServerHost + return nil + case string(WithoutServerHost): + *l.value = WithoutServerHost + return nil + } + return fmt.Errorf("invalid RepoFormatEnum '%s'", s) +} + func main() { dest := flag.String("dest", "", "destination directory") namePattern := flag.String("name", "", "only clone repos whose name matches the regexp.") + // Default value for + repoFormat := WithServerHost + flag.Var(&RepoFormatEnumValue{&repoFormat}, "repo-name-format", "the format of the local repo name in zoekt (WithServerHost or WithoutServerHost)") excludePattern := flag.String("exclude", "", "don't mirror repos whose names match this regexp.") deleteRepos := flag.Bool("delete", false, "delete missing repos") httpCrendentialsPath := flag.String("http-credentials", "", "path to a file containing http credentials stored like 'user:password'.") @@ -162,8 +193,15 @@ func main() { } name := filepath.Join(cloneURL.Host, cloneURL.Path) + var zoektName string + switch repoFormat { + case WithServerHost: + zoektName = name + case WithoutServerHost: + zoektName = k + } config := map[string]string{ - "zoekt.name": name, + "zoekt.name": zoektName, "zoekt.gerrit-project": k, "zoekt.gerrit-host": rootURL.String(), "zoekt.archived": marshalBool(v.State == "READ_ONLY"), From 7cb5d1637257284671423abec75b0ce239d94ade Mon Sep 17 00:00:00 2001 From: Xavier Calland Date: Mon, 29 Apr 2024 12:14:56 +0200 Subject: [PATCH 2/2] use flag.String to handle repo-name-format param --- cmd/zoekt-mirror-gerrit/main.go | 42 ++++++++++----------------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/cmd/zoekt-mirror-gerrit/main.go b/cmd/zoekt-mirror-gerrit/main.go index 523d9537d..e593b62d9 100644 --- a/cmd/zoekt-mirror-gerrit/main.go +++ b/cmd/zoekt-mirror-gerrit/main.go @@ -26,6 +26,7 @@ import ( "net/url" "os" "path/filepath" + "slices" "strconv" "strings" @@ -73,40 +74,22 @@ func newLoggingClient() *http.Client { } } -type RepoFormatEnum string +const qualifiedRepoNameFormat = "qualified" +const projectRepoNameFormat = "project" -const ( - WithServerHost RepoFormatEnum = "WithServerHost" - WithoutServerHost RepoFormatEnum = "WithoutServerHost" -) - -// Implementing flag.Value interface -type RepoFormatEnumValue struct { - value *RepoFormatEnum -} +var validRepoNameFormat = []string{qualifiedRepoNameFormat, projectRepoNameFormat} -func (l *RepoFormatEnumValue) String() string { - return string(*l.value) -} - -func (l *RepoFormatEnumValue) Set(s string) error { - switch s { - case string(WithServerHost): - *l.value = WithServerHost - return nil - case string(WithoutServerHost): - *l.value = WithoutServerHost - return nil +func validateRepoNameFormat(s string) { + if !slices.Contains(validRepoNameFormat, s) { + log.Fatal(fmt.Sprintf("repo-name-format must be one of %s", strings.Join(validRepoNameFormat, ", "))) } - return fmt.Errorf("invalid RepoFormatEnum '%s'", s) } func main() { + dest := flag.String("dest", "", "destination directory") namePattern := flag.String("name", "", "only clone repos whose name matches the regexp.") - // Default value for - repoFormat := WithServerHost - flag.Var(&RepoFormatEnumValue{&repoFormat}, "repo-name-format", "the format of the local repo name in zoekt (WithServerHost or WithoutServerHost)") + repoNameFormat := flag.String("repo-name-format", qualifiedRepoNameFormat, fmt.Sprintf("the format of the local repo name in zoekt (valid values: %s)", strings.Join(validRepoNameFormat, ", "))) excludePattern := flag.String("exclude", "", "don't mirror repos whose names match this regexp.") deleteRepos := flag.Bool("delete", false, "delete missing repos") httpCrendentialsPath := flag.String("http-credentials", "", "path to a file containing http credentials stored like 'user:password'.") @@ -116,6 +99,7 @@ func main() { if len(flag.Args()) < 1 { log.Fatal("must provide URL argument.") } + validateRepoNameFormat(*repoNameFormat) rootURL, err := url.Parse(flag.Arg(0)) if err != nil { @@ -194,10 +178,10 @@ func main() { name := filepath.Join(cloneURL.Host, cloneURL.Path) var zoektName string - switch repoFormat { - case WithServerHost: + switch *repoNameFormat { + case qualifiedRepoNameFormat: zoektName = name - case WithoutServerHost: + case projectRepoNameFormat: zoektName = k } config := map[string]string{