Repository navigation
Expand file tree
/
Copy pathroot.go
More file actions
103 lines (85 loc) · 2.59 KB
/
Copy pathroot.go
File metadata and controls
103 lines (85 loc) · 2.59 KB
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
package cmd
import (
"os"
"strings"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var cfgFile string
var rootCmd = &cobra.Command{
Use: "simulator",
Short: "Simulator command line",
Long: `
A distributed systems and infrastructure simulator for attacking and
debugging Kubernetes
`,
}
var logger *logrus.Logger //nolint:deadcode,unused
func newCmdRoot() *cobra.Command {
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config-file", "c", "", "Path to the simulator config file")
cobra.OnInitialize(initConfig)
rootCmd.AddCommand(newConfigCommand())
rootCmd.AddCommand(newInfraCommand())
rootCmd.AddCommand(newInitCommand())
rootCmd.AddCommand(newScenarioCommand())
rootCmd.AddCommand(newSSHCommand())
rootCmd.AddCommand(newVersionCommand())
rootCmd.AddCommand(newCompletionCmd())
// NOTE the panics here are needed - if these calls fail we cannot recover
// and the cause is most likely programmer error
rootCmd.PersistentFlags().StringP("state-bucket", "b", "",
"The name of the s3 bucket to use for remote-state. Must be globally unique")
if err := viper.BindPFlag("state-bucket", rootCmd.PersistentFlags().Lookup("state-bucket")); err != nil {
panic(err)
}
rootCmd.PersistentFlags().StringP("loglevel", "l", "info", "Level of detail in output logging")
if err := viper.BindPFlag("loglevel", rootCmd.PersistentFlags().Lookup("loglevel")); err != nil {
panic(err)
}
return rootCmd
}
func initConfig() {
viper.SetConfigType("yaml")
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
viper.AddConfigPath("$HOME/.kubesim")
viper.SetConfigName("simulator")
}
err := viper.ReadInConfig()
if err != nil {
// todo(ajm) this errors if not in the same dir as `simulator.yaml`. Move
// those vars here?
panic(errors.Wrapf(err, "Error reading config file"))
}
// read config from environment too
viper.SetEnvPrefix("simulator")
replacer := strings.NewReplacer("-", "_")
viper.SetEnvKeyReplacer(replacer)
viper.AutomaticEnv()
}
func newCompletionCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "completion",
Short: "Generates Bash completion scripts",
Long: `To load completion run
. <(simulator completion)
To configure your Bash shell to load completions for each session add to your bashrc
# ~/.bashrc or ~/.profile
. <(simulator completion)
`,
Run: func(cmd *cobra.Command, args []string) {
if err := rootCmd.GenBashCompletion(os.Stdout); err != nil {
panic(err)
}
},
}
return cmd
}
// Execute starts the aplication
func Execute() error {
cmd := newCmdRoot()
return cmd.Execute()
}