diff options
author | Vincent Ambo <tazjin@google.com> | 2019-12-29T03·50+0100 |
---|---|---|
committer | Vincent Ambo <tazjin@google.com> | 2019-12-29T03·50+0100 |
commit | 7c52a205ee3c1df423de0c63b30e666815824839 (patch) | |
tree | 4ae3304d5ca8a7558113e30d7cb26c69a36dcfdc /ops/sync-gcsr/main.go | |
parent | dce11128423a0e4f912e7182a352cd6755195b1b (diff) |
refactor(sync-gcsr): Split clone into separate function r/317
This is in preparation for adding more complex branch-related logic to both functions.
Diffstat (limited to 'ops/sync-gcsr/main.go')
-rw-r--r-- | ops/sync-gcsr/main.go | 45 |
1 files changed, 25 insertions, 20 deletions
diff --git a/ops/sync-gcsr/main.go b/ops/sync-gcsr/main.go index daec76b3464b..358c54805584 100644 --- a/ops/sync-gcsr/main.go +++ b/ops/sync-gcsr/main.go @@ -37,38 +37,43 @@ func updateRepo(repo *git.Repository, tree *git.Worktree, opts *git.PullOptions) return nil } -func main() { - var dest = EnvOr("SYNC_DEST", "/git/depot") - var project = EnvOr("SYNC_PROJECT", "tazjins-infrastructure") - var repo = EnvOr("SYNC_REPO", "depot") - var user = os.Getenv("SYNC_USER") - var pass = os.Getenv("SYNC_PASS") +func cloneRepo(dest, project, repo string, auth *http.BasicAuth) (*git.Repository, error) { + var cloneOpts = git.CloneOptions{ + Auth: auth, + URL: fmt.Sprintf("https://source.developers.google.com/p/%s/r/%s", project, repo), + } - log.Printf("Syncing repository '%s/%s' to destination '%s'", project, repo, dest) + handle, err := git.PlainClone(dest, false, &cloneOpts) - var cloneOpts = git.CloneOptions{ - URL: fmt.Sprintf("https://source.developers.google.com/p/%s/r/%s", project, repo), + if err == git.ErrRepositoryAlreadyExists { + handle, err = git.PlainOpen(dest) } + return handle, err +} + +func main() { + dest := EnvOr("SYNC_DEST", "/git/depot") + project := EnvOr("SYNC_PROJECT", "tazjins-infrastructure") + repo := EnvOr("SYNC_REPO", "depot") + user := os.Getenv("SYNC_USER") + pass := os.Getenv("SYNC_PASS") + + log.Printf("Syncing repository '%s/%s' to destination '%s'", project, repo, dest) + + var auth *http.BasicAuth if user != "" && pass != "" { - cloneOpts.Auth = &http.BasicAuth{ + auth = &http.BasicAuth{ Username: user, Password: pass, } log.Println("Enabling basic authentication as user", user) } - action := "clone" - handle, err := git.PlainClone(dest, false, &cloneOpts) - - if err == git.ErrRepositoryAlreadyExists { - log.Println("Repository has already been cloned!") - handle, err = git.PlainOpen(dest) - action = "open" - } + handle, err := cloneRepo(dest, project, repo, auth) if err != nil { - log.Fatalf("Failed to %s repository: %s", action, err) + log.Fatalf("Failed to clone repository: %s", err) } else { log.Println("Initiating update loop") } @@ -79,7 +84,7 @@ func main() { } pullOpts := git.PullOptions{ - Auth: cloneOpts.Auth, + Auth: auth, Force: true, } |