generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
root.go
219 lines (189 loc) · 5.93 KB
/
root.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package commands
import (
"fmt"
"os"
"path/filepath"
"github.com/SAP/configuration-tools-for-gitops/pkg/git"
"github.com/SAP/configuration-tools-for-gitops/pkg/log"
"github.com/SAP/configuration-tools-for-gitops/pkg/version"
"github.com/spf13/cobra"
"go.uber.org/zap"
"github.com/spf13/viper"
)
var (
componentCfgFile, cfgFile, gp, gu string
remote, defaultBranch string
mD int
logLvl log.Level
)
const (
gitPathKey = "git.path"
gitURLKey = "git.URL"
gitRemoteKey = "git.remote"
gitDepthKey = "git.depth"
componentCfg = "component.cfg"
// cannot restrict checkout depth due to upstream bug
// (see https://github.com/go-git/go-git/issues/328 for issue tracking)
overWriteGitDepth = 0
)
func Execute() {
cmd := newRoot()
cobra.OnInitialize(initConfig)
cobra.CheckErr(cmd.Execute())
}
func newRoot() *cobra.Command {
var c = &cobra.Command{
Use: "coco",
Short: "CLI to interact with the gitops repository",
Long: `coco is a CLI to interact with a gitops repository and shall provide
various solutions, ranging from file-generation over the calculation of
dependency trees to various interactions with git and github.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
c.AddCommand(newVersion())
c.AddCommand(newDependencies())
c.AddCommand(newGenerate())
c.AddCommand(newInspect())
c.AddCommand(newReconcile())
c.PersistentFlags().StringVar(
&cfgFile, "config", "", "config file (default $HOME/.coco)",
)
bindFlag(c.PersistentFlags(), "cfgfile", "config", "COCO_CFG")
c.PersistentFlags().StringVarP(
&componentCfgFile, "component-cfg", "c", "coco.yaml",
`name of the component-specific configuration file`,
)
bindFlag(c.PersistentFlags(), componentCfg, "component-cfg", "COCO_COMPONENT_CFG")
c.PersistentFlags().VarP(
&logLvl, "loglvl", "l",
fmt.Sprintf("sets the log level of the application - key or value of %v", logLvl.AllLevels()),
)
bindFlag(c.PersistentFlags(), "loglvl", "loglvl", "LOGLEVEL")
c.PersistentFlags().StringVarP(
&gp, "git-path", "p", "", "path where the configuration repository locally resides",
)
bindFlag(c.PersistentFlags(), gitPathKey, "git-path", "GIT_PATH")
c.PersistentFlags().StringVarP(
&gu, "git-url", "u", "", "git URL of the configuration repository",
)
bindFlag(c.PersistentFlags(), gitURLKey, "git-url", "GIT_URL")
c.PersistentFlags().StringVarP(
&remote, "git-remote", "r", "origin",
"remote branch to compare against for changed components",
)
bindFlag(c.PersistentFlags(), gitRemoteKey, "git-remote", "GIT_REMOTE")
c.PersistentFlags().StringVarP(
&defaultBranch, "git-defaultbranch", "b", "main", "default branch",
)
bindFlag(c.PersistentFlags(), "git.defaultBranch", "git-defaultbranch", "GIT_DEFAULT_BRANCH")
c.PersistentFlags().IntVar(
&mD, "git-depth", 0,
`[NOT IN USE (upstream bug: see https://github.com/go-git/go-git/issues/328 for issue tracking)]
max checkout depth of the git repository`,
)
bindFlag(c.PersistentFlags(), gitDepthKey, "git-depth", "GIT_DEPTH")
cobra.CheckErr(viper.BindEnv("git-token", "GITHUB_TOKEN"))
return c
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if err := log.Init(logLvl, "2006-01-02T15:04:05Z07:00", true); err != nil {
zap.S().Fatal(err)
}
v := version.ReadAll()
log.Sugar.Debugf("build details: %+v", v)
viper.SetConfigType("yaml")
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
home, err := os.UserHomeDir()
if err != nil {
zap.S().Fatal(err)
}
viper.AddConfigPath(home)
viper.SetConfigName(".coco")
viper.SetDefault("cfgfile", filepath.Join(home, ".coco"))
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
log.Sugar.Debugf("Using config file: %s", viper.ConfigFileUsed())
}
if viper.GetString(gitPathKey) == "" {
path, err := os.Getwd()
if err != nil {
zap.S().Fatal(err)
}
log.Sugar.Debug("git.path not set - using default ", zap.String("git.path", path))
viper.Set(gitPathKey, path)
}
if viper.GetInt(gitDepthKey) != 0 {
log.Sugar.Warnf(
"%s cannot be used at the moment due to upstream bug (https://github.com/go-git/go-git/issues/328)",
gitDepthKey,
)
}
if ok := consistentGitSetup(
viper.GetString(gitPathKey),
viper.GetString(gitURLKey),
viper.GetString("git-token"),
viper.GetString(gitRemoteKey),
viper.GetString("git.defaultBranch"),
overWriteGitDepth, // viper.GetInt(gitDepth),
logLvl,
); !ok {
os.Exit(1)
}
if logLvl.Is(log.Debug()) {
zf := []interface{}{"all settings: "}
for k, v := range viper.AllSettings() {
if k == "git-token" {
zf = append(zf, zap.String(k, "<is-set>"))
} else {
zf = append(zf, zap.String(k, fmt.Sprintf("%+v", v)))
}
}
log.Log.Sugar().Debug(zf...)
}
}
func consistentGitSetup(
path, url, token, remote, defaultBranch string, gitDepth int, logLvl log.Level,
) bool {
if remote == "" {
return true
}
if remote == "origin" && url == "" {
log.Sugar.Debug(
"git.remote and git.URL appear to be default: ",
"functionality relying on external git calls may not work.",
)
return true
}
if url == "" {
log.Sugar.Errorf(
"\"git.remote\" is set but \"git.URL\" is missing.\n%s",
provideBy(gitURLKey, "--git-url", "git.URL", "GIT_URL"),
)
return false
}
if _, err := git.New(
path, url, token, remote, defaultBranch, gitDepth, logLvl,
); err != nil {
log.Sugar.Errorf(
"failed to validate repository in path \"%s\": %s", path, err,
)
return false
}
return true
}
func provideBy(name, par, config, env string) string {
return fmt.Sprintf(
" Provide \"%s\" in either of the following ways:"+
"\n - CLI parameter (%s)"+
"\n - coco config (%s)"+
"\n - environment variable (%s)",
name, par, config, env,
)
}