/
update_default.go
179 lines (142 loc) · 3.87 KB
/
update_default.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package app
import (
"context"
"fmt"
"os"
"os/exec"
"path"
"strings"
"github.com/cuvva/cuvva-public-go/lib/cher"
"github.com/cuvva/cuvva-public-go/lib/slicecontains"
"github.com/cuvva/cuvva-public-go/tools/cdep"
"github.com/cuvva/cuvva-public-go/tools/cdep/git"
"github.com/cuvva/cuvva-public-go/tools/cdep/parsers"
"github.com/cuvva/cuvva-public-go/tools/cdep/paths"
gogit "github.com/go-git/go-git/v5"
log "github.com/sirupsen/logrus"
)
func (a App) UpdateDefault(ctx context.Context, req *parsers.Params, overruleChecks []string) error {
log.Info("getting latest commit hash")
latestHash, err := git.GetLatestCommitHash(ctx, req.Branch)
if err != nil {
return err
}
repoPath, err := paths.GetConfigRepo()
if err != nil {
return err
}
log.Info("fetching config repo")
if out, err := exec.CommandContext(ctx, "git", "-C", repoPath, "fetch", "--all").CombinedOutput(); err != nil {
fmt.Println(string(out))
return err
}
log.Info("opening config repo")
configRepo, err := gogit.PlainOpen(repoPath)
if err != nil {
return cher.New("git_repo_error", cher.M{
"error": err,
})
}
_, err = git.CheckRepo(configRepo)
if err != nil {
return err
}
ref, err := configRepo.Head()
if err != nil {
return err
}
defaultRef := fmt.Sprintf("refs/heads/%s", cdep.DefaultBranch)
if ref.Name().String() != defaultRef {
return cher.New("config_not_on_default", nil)
}
log.Info("pulling config repo from remote")
if out, err := exec.CommandContext(ctx, "git", "-C", repoPath, "pull", "origin", cdep.DefaultBranch).CombinedOutput(); err != nil {
fmt.Println(string(out))
return err
}
wt, err := configRepo.Worktree()
if err != nil {
return err
}
err = git.CheckWorkingCopy(wt)
if err != nil {
if !slicecontains.String(overruleChecks, "working_copy_dirty") {
return err
}
log.Warn("working_copy_dirty overruled")
}
envs, err := a.LoadEnvs(repoPath, req.System, req.Environment)
if err != nil {
return err
}
loadedPaths := map[string][]string{}
for env := range envs {
path := path.Join(repoPath, req.System, env, req.Type)
loadedPaths[env] = append(loadedPaths[env], path)
}
updatedFiles := []string{}
log.Info("editing json files")
for env, paths := range loadedPaths {
for _, p := range paths {
files, err := os.ReadDir(p)
if err != nil {
return err
}
for _, file := range files {
if file.IsDir() {
continue
}
fullPath := path.Join(p, file.Name())
var changed bool
if strings.HasSuffix(fullPath, "_base.json") || strings.HasSuffix(fullPath, ".yaml") {
changed, err = a.AddToConfig(fullPath, req.Branch, latestHash)
if err != nil {
return err
}
} else {
changed, err = a.RemFromConfig(fullPath)
if err != nil {
e := cher.Coerce(err)
if e.Code != "frozen" {
return err
}
log.Warn(fmt.Sprintf("skipping %s due to cdep freeze", file.Name()))
}
}
if changed {
shortPath := path.Join(req.System, env, req.Type, file.Name())
updatedFiles = append(updatedFiles, shortPath)
}
}
}
}
if len(updatedFiles) == 0 {
return cher.New("nothing_changed", nil)
}
commitMessage := fmt.Sprintf("cdep: %s", req.String("update-default"))
if err := a.PublishToSlack(ctx, req, commitMessage, updatedFiles, repoPath); err != nil {
return err
}
if a.DryRun {
log.Info("Dry run only, stopping now")
log.Infof("commit message (%s)\n", commitMessage)
return nil
}
for _, p := range updatedFiles {
log.Infof("adding %s to commit", p)
_, err := wt.Add(p)
if err != nil {
return err
}
}
_, err = wt.Commit(commitMessage, &gogit.CommitOptions{})
if err != nil {
return err
}
log.Info("pushing commit to config repo")
if out, err := exec.CommandContext(ctx, "git", "-C", repoPath, "push", "origin", cdep.DefaultBranch).CombinedOutput(); err != nil {
fmt.Println(string(out))
return err
}
return nil
}