This repository has been archived by the owner on May 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathroot.go
209 lines (192 loc) · 5.59 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
package cmd
import (
"fmt"
"os"
"path"
"strings"
"github.com/danmx/sigil/pkg/aws"
homedir "github.com/mitchellh/go-homedir"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
appName string = "sigil"
// appVersion is the semantic appVersion (added at compile time)
appVersion string
// gitCommit is the git commit id (added at compile time)
gitCommit string
// logLevel level is setting loging level (added at compile time)
logLevel string = "panic"
// dev is turning a debug mode (added at compile time)
dev string = "false"
workDir string
cfg *viper.Viper
cfgFileName = "config"
cfgType = "toml"
workDirName = "." + appName
// rootCmd represents the base command when called without any subcommands
rootCmd = &cobra.Command{
Use: appName,
Short: "AWS SSM Session manager client",
Long: `A tool for establishing a session in EC2 instances with AWS SSM Agent installed`,
Version: fmt.Sprintf("%s (build %s)", appVersion, gitCommit),
DisableAutoGenTag: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// returns err
return aws.AppendUserAgent(appName + "/" + appVersion)
},
}
)
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() error {
// returns err
return rootCmd.Execute()
}
func init() {
// Set debug
if dev == "true" {
log.SetReportCaller(true)
}
// Set startup Log level
if err := setLogLevel(logLevel); err != nil {
log.WithFields(log.Fields{
"logLevel": logLevel,
}).Fatal(err)
}
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Fprintln(os.Stderr, err)
log.Fatal(err)
}
workDir = path.Join(home, workDirName)
stat, err := os.Stat(workDir)
if !(err == nil && stat.IsDir()) {
if err := os.MkdirAll(workDir, 0750); err != nil { //nolint:gomnd // Linux file permissions
fmt.Fprintln(os.Stderr, err)
log.Fatal(err)
}
}
// init config and env vars
cobra.OnInitialize(func() {
if err := initConfig(rootCmd); err != nil {
fmt.Fprintln(os.Stderr, err)
log.Fatal(err)
}
})
// Config file
rootCmd.PersistentFlags().StringP("config", "c", "", "full config file path, supported formats: json/yaml/toml/hcl/props")
rootCmd.PersistentFlags().StringP("config-profile", "p", "default", "pick the config profile")
// Log level
rootCmd.PersistentFlags().String("log-level", logLevel, "specify the log level: trace/debug/info/warn/error/fatal/panic")
// AWS
rootCmd.PersistentFlags().StringP("region", "r", "", "specify AWS region")
rootCmd.PersistentFlags().String("profile", "", "specify AWS profile")
rootCmd.PersistentFlags().StringP("mfa", "m", "", "specify MFA token")
}
// initConfig reads in config file and ENV variables if set
func initConfig(cmd *cobra.Command) error {
cfg = viper.New()
// Environment variables
cfg.SetEnvPrefix(appName)
cfg.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
// Root config bindings
for _, key := range []string{"config", "config-profile", "log-level"} {
if err := cfg.BindEnv(key); err != nil {
log.WithFields(log.Fields{
"env": key,
}).Error(err)
return err
}
if err := cfg.BindPFlag(key, cmd.PersistentFlags().Lookup(key)); err != nil {
log.WithFields(log.Fields{
"flag": key,
}).Error(err)
return err
}
}
cfgFile := cfg.GetString("config")
cfgProfile := cfg.GetString("config-profile")
logLevel := cfg.GetString("log-level")
// Set Log level
if err := setLogLevel(logLevel); err != nil {
fmt.Fprintln(os.Stderr, err)
log.WithFields(log.Fields{
"logLevel": logLevel,
}).Error(err)
return err
}
if cfgFile != "" {
// Use config file from the flag.
cfg.SetConfigFile(cfgFile)
} else {
// Search config in home directory with name from cfgFileName (without extension).
cfg.AddConfigPath(workDir)
cfg.SetConfigName(cfgFileName)
cfg.SetConfigType(cfgType)
}
// If a config file is found, read it in.
if err := cfg.ReadInConfig(); err == nil {
log.WithFields(log.Fields{
"config": cfg.ConfigFileUsed(),
}).Debug("Using config file")
cfg, err = safeSub(cfg, cfgProfile)
if err != nil {
fmt.Fprintln(os.Stderr, err)
log.WithFields(log.Fields{
"config-profile": cfgProfile,
}).Error(err)
return err
}
}
// Rebinding config bindings that will be propagated to subcommands because of the subconfig (config profile)
cfg.SetEnvPrefix(appName)
if err := cfg.BindEnv("mfa"); err != nil {
log.WithFields(log.Fields{
"env": "mfa",
}).Error(err)
return err
}
for _, key := range []string{"region", "config-profile", "profile"} {
if err := cfg.BindPFlag(key, cmd.PersistentFlags().Lookup(key)); err != nil {
log.WithFields(log.Fields{
"flag": key,
}).Error(err)
return err
}
}
return nil
}
// because of https://github.com/spf13/viper/issues/616
func safeSub(v *viper.Viper, profile string) (*viper.Viper, error) {
subConfig := v.Sub(profile)
if subConfig == nil {
return nil, fmt.Errorf("config profile doesn't exist. Profile: %s", profile)
}
return subConfig, nil
}
// setLogLevel sets the log level
func setLogLevel(level string) error {
// Log level
switch level {
case "error":
log.SetLevel(log.ErrorLevel)
case "debug":
log.SetLevel(log.DebugLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "warn":
log.SetLevel(log.WarnLevel)
case "fatal":
log.SetLevel(log.FatalLevel)
case "panic":
log.SetLevel(log.PanicLevel)
case "trace":
log.SetLevel(log.TraceLevel)
default:
return fmt.Errorf("unsupported log level: %s", level)
}
return nil
}