/
root.go
85 lines (73 loc) · 2.45 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
package cmd
import (
"fmt"
"github.com/pinpt/esp/internal/app"
"github.com/pinpt/esp/internal/client"
jww "github.com/spf13/jwalterweatherman"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var verbose bool
var esp *app.Config
var c *client.EspClient
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "esp",
Short: "A utility to browse and export SSM Parameter values into different formats.",
}
// 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() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
esp = app.New(false)
// setting this to SSM just to make the interface nicer, since we only have the SSM backend
esp.Backend = "ssm"
c = client.New(esp)
cobra.OnInitialize(initConfig)
// check AWS Region
if _, ok := os.LookupEnv("AWS_DEFAULT_REGION"); !ok {
fmt.Println("Please set the AWS_DEFAULT_REGION environment variable.")
os.Exit(1)
}
if _, ok := os.LookupEnv("AWS_PROFILE"); !ok {
fmt.Println("Please set the AWS_PROFILE environment variable.")
os.Exit(2)
}
// CLI args
rootCmd.PersistentFlags().StringVarP(&esp.Env, "env", "e", "", "Declare the env to work on.")
rootCmd.PersistentFlags().StringVarP(&esp.Backend, "backend", "b", "ssm", "Set which backend to use.")
rootCmd.PersistentFlags().BoolVar(&verbose, "verbose", false, "Show more output")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
// Just setting for debugging
if verbose {
jww.SetStdoutThreshold(jww.LevelInfo)
}
viper.SetConfigName(esp.Filename)
viper.AddConfigPath(esp.Path)
// If a config file is found, read it in and mark that this is an ESP project.
if err := viper.ReadInConfig(); err == nil {
esp.IsEspProject = true
}
if esp.IsEspProject {
if err := viper.Unmarshal(&esp); err != nil {
fmt.Printf("Error parsing the %s\n", esp.Filename)
}
// not going to force this at the moment. I will add this when I have a second backend
/*if err := rootCmd.MarkFlagRequired("backend"); err != nil {
//fmt.Printf("There is an %s.yaml defined, so you need to set --env arg.\n", esp.Filename)
os.Exit(3)
}*/
if err := rootCmd.MarkFlagRequired("env"); err != nil {
//fmt.Printf("There is an %s.yaml defined, so you need to set --env arg.\n", esp.Filename)
os.Exit(3)
}
}
}