-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathpull.go
55 lines (43 loc) · 1.24 KB
/
pull.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package git
import (
"os"
"github.com/defenseunicorns/zarf/cli/internal/log"
"github.com/defenseunicorns/zarf/cli/internal/utils"
"github.com/go-git/go-git/v5"
"github.com/sirupsen/logrus"
)
const onlineRemoteName = "online-upstream"
func DownloadRepoToTemp(gitUrl string) string {
path := utils.MakeTempDir()
pull(gitUrl, path)
return path
}
func Pull(gitUrl string, targetFolder string) {
path := targetFolder + "/" + transformURLtoRepoName(gitUrl)
pull(gitUrl, path)
}
func pull(gitUrl string, targetFolder string) {
logContext := log.Logger.WithFields(logrus.Fields{
"Remote": gitUrl,
})
logContext.Info("Processing git repo")
gitCred := FindAuthForHost(gitUrl)
cloneOptions := &git.CloneOptions{
URL: gitUrl,
Progress: os.Stdout,
RemoteName: onlineRemoteName,
}
// Gracefully handle no git creds on the system (like our CI/CD)
if gitCred.Auth.Username != "" {
cloneOptions.Auth = &gitCred.Auth
}
// Clone the given repo
_, err := git.PlainClone(targetFolder, false, cloneOptions)
if err == git.ErrRepositoryAlreadyExists {
logContext.Info("Repo already cloned")
} else if err != nil {
logContext.Debug(err)
logContext.Fatal("Not a valid git repo or unable to clone")
}
logContext.Info("Git repo synced")
}