refactor(sync-gcsr): Split clone into separate function
This is in preparation for adding more complex branch-related logic to both functions.
This commit is contained in:
parent
dce1112842
commit
7c52a205ee
1 changed files with 25 additions and 20 deletions
|
|
@ -37,38 +37,43 @@ func updateRepo(repo *git.Repository, tree *git.Worktree, opts *git.PullOptions)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func cloneRepo(dest, project, repo string, auth *http.BasicAuth) (*git.Repository, error) {
|
||||||
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")
|
|
||||||
|
|
||||||
log.Printf("Syncing repository '%s/%s' to destination '%s'", project, repo, dest)
|
|
||||||
|
|
||||||
var cloneOpts = git.CloneOptions{
|
var cloneOpts = git.CloneOptions{
|
||||||
|
Auth: auth,
|
||||||
URL: fmt.Sprintf("https://source.developers.google.com/p/%s/r/%s", project, repo),
|
URL: fmt.Sprintf("https://source.developers.google.com/p/%s/r/%s", project, repo),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handle, err := git.PlainClone(dest, false, &cloneOpts)
|
||||||
|
|
||||||
|
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 != "" {
|
if user != "" && pass != "" {
|
||||||
cloneOpts.Auth = &http.BasicAuth{
|
auth = &http.BasicAuth{
|
||||||
Username: user,
|
Username: user,
|
||||||
Password: pass,
|
Password: pass,
|
||||||
}
|
}
|
||||||
log.Println("Enabling basic authentication as user", user)
|
log.Println("Enabling basic authentication as user", user)
|
||||||
}
|
}
|
||||||
|
|
||||||
action := "clone"
|
handle, err := cloneRepo(dest, project, repo, auth)
|
||||||
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"
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to %s repository: %s", action, err)
|
log.Fatalf("Failed to clone repository: %s", err)
|
||||||
} else {
|
} else {
|
||||||
log.Println("Initiating update loop")
|
log.Println("Initiating update loop")
|
||||||
}
|
}
|
||||||
|
|
@ -79,7 +84,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
pullOpts := git.PullOptions{
|
pullOpts := git.PullOptions{
|
||||||
Auth: cloneOpts.Auth,
|
Auth: auth,
|
||||||
Force: true,
|
Force: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue