Allow gerrit mirror to be defined in config

indexserver cmd config file was not including Gerrit type
mirror. It handles http basic auth in a CredentialPath entry
and call zoekt-mirror-gerrit with the according flags.

It also implement gerrit default weblink implementation of
Gitiles (WebLink.Name = browse)

Change-Id: Ia2fd0921ad9fc4e11914fbc61f07fd9601980492
diff --git a/cmd/zoekt-indexserver/config.go b/cmd/zoekt-indexserver/config.go
index d976904..edea3dc 100644
--- a/cmd/zoekt-indexserver/config.go
+++ b/cmd/zoekt-indexserver/config.go
@@ -44,6 +44,7 @@
 	Exclude                string
 	GitLabURL              string
 	OnlyPublic             bool
+	GerritApiURL           string
 }
 
 func randomize(entries []ConfigEntry) []ConfigEntry {
@@ -226,6 +227,19 @@
 			if c.CredentialPath != "" {
 				cmd.Args = append(cmd.Args, "-token", c.CredentialPath)
 			}
+		} else if c.GerritApiURL != "" {
+			cmd = exec.Command("zoekt-mirror-gerrit",
+				"-dest", repoDir)
+			if c.CredentialPath != "" {
+				cmd.Args = append(cmd.Args, "-http-credentials", c.CredentialPath)
+			}
+			if c.Name != "" {
+				cmd.Args = append(cmd.Args, "-name", c.Name)
+			}
+			if c.Exclude != "" {
+				cmd.Args = append(cmd.Args, "-exclude", c.Exclude)
+			}
+			cmd.Args = append(cmd.Args, c.GerritApiURL)
 		}
 
 		stdout, _ := loggedRun(cmd)
diff --git a/cmd/zoekt-mirror-gerrit/main.go b/cmd/zoekt-mirror-gerrit/main.go
index 35f74fa..b253e4f 100644
--- a/cmd/zoekt-mirror-gerrit/main.go
+++ b/cmd/zoekt-mirror-gerrit/main.go
@@ -75,6 +75,7 @@
 	dest := flag.String("dest", "", "destination directory")
 	namePattern := flag.String("name", "", "only clone repos whose name matches the regexp.")
 	excludePattern := flag.String("exclude", "", "don't mirror repos whose names match this regexp.")
+	httpCrendentialsPath := flag.String("http-credentials", "", "path to a file containing http credentials stored like 'user:password'.")
 	flag.Parse()
 
 	if len(flag.Args()) < 1 {
@@ -86,6 +87,16 @@
 		log.Fatalf("url.Parse(): %v", err)
 	}
 
+	if *httpCrendentialsPath != "" {
+		creds, err := ioutil.ReadFile(*httpCrendentialsPath)
+		if err != nil {
+			log.Print("Cannot read gerrit http credentials, going Anonymous")
+		} else {
+			splitCreds := strings.Split(strings.TrimSpace(string(creds)), ":")
+			rootURL.User = url.UserPassword(splitCreds[0], splitCreds[1])
+		}
+	}
+
 	if *dest == "" {
 		log.Fatal("must set --dest")
 	}
@@ -94,6 +105,7 @@
 	if err != nil {
 		log.Fatal(err)
 	}
+
 	client, err := gerrit.NewClient(rootURL.String(), newLoggingClient())
 	if err != nil {
 		log.Fatalf("NewClient(%s): %v", rootURL, err)
@@ -134,8 +146,17 @@
 		}
 
 		for _, wl := range v.WebLinks {
-			config["zoekt.web-url"] = wl.URL
-			config["zoekt.web-url-type"] = wl.Name
+			// default gerrit gitiles config is named browse, and does not include
+			// root domain name in it. Cheating.
+			switch wl.Name {
+			case "browse":
+				config["zoekt.web-url"] = fmt.Sprintf("%s://%s%s", rootURL.Scheme,
+					rootURL.Host, wl.URL)
+				config["zoekt.web-url-type"] = "gitiles"
+			default:
+				config["zoekt.web-url"] = wl.URL
+				config["zoekt.web-url-type"] = wl.Name
+			}
 		}
 
 		if dest, err := gitindex.CloneRepo(*dest, name, cloneURL.String(), config); err != nil {