-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathpush.go
66 lines (54 loc) · 1.58 KB
/
push.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
56
57
58
59
60
61
62
63
64
65
66
package git
import (
"github.com/defenseunicorns/zarf/cli/config"
"github.com/defenseunicorns/zarf/cli/internal/log"
"github.com/defenseunicorns/zarf/cli/internal/utils"
"github.com/go-git/go-git/v5"
goConfig "github.com/go-git/go-git/v5/config"
)
const offlineRemoteName = "offline-downstream"
func PushAllDirectories(localPath string) {
paths := utils.ListDirectories(localPath)
for _, path := range paths {
push(path)
}
}
func push(localPath string) {
logContext := log.Logger.WithField("repo", localPath)
logContext.Info("Processing git repo")
// Open the given repo
repo, err := git.PlainOpen(localPath)
if err != nil {
logContext.Warn("Not a valid git repo or unable to open")
return
}
// Get the upstream URL
remote, err := repo.Remote(onlineRemoteName)
if err != nil {
logContext.Warn("Unable to find the git remote")
return
}
remoteUrl := remote.Config().URLs[0]
targetUrl := transformURL("https://"+config.ZarfLocalIP, remoteUrl)
_, _ = repo.CreateRemote(&goConfig.RemoteConfig{
Name: offlineRemoteName,
URLs: []string{targetUrl},
})
gitCred := FindAuthForHost(config.ZarfLocalIP)
err = repo.Push(&git.PushOptions{
RemoteName: offlineRemoteName,
Auth: &gitCred.Auth,
RefSpecs: []goConfig.RefSpec{
"refs/heads/*:refs/heads/*",
"refs/tags/*:refs/tags/*",
},
})
pushContext := logContext.WithField("target", targetUrl)
if err == git.NoErrAlreadyUpToDate {
pushContext.Info("Repo already up-to-date")
} else if err != nil {
pushContext.Warn("Unable to push repo to the gitops service")
} else {
pushContext.Info("Repo updated")
}
}