-
Notifications
You must be signed in to change notification settings - Fork 2
/
root.go
124 lines (99 loc) · 3.34 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
package cmd
import (
_ "embed"
"fmt"
"strings"
"github.com/hashicorp/go-hclog"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/raito-io/cli/internal/constants"
)
//go:embed help/root-description.txt
var rootDescription string
var (
cfgFile string
)
type rootCmd struct {
cmd *cobra.Command
exit func(int)
}
func (cmd *rootCmd) Execute(args []string) {
cmd.cmd.SetArgs(args)
if err := cmd.cmd.Execute(); err != nil {
cmd.exitForError()
}
}
func (cmd *rootCmd) exitForError() {
cmd.exit(1)
}
func newRootCmd(version string, exit func(int)) *rootCmd {
root := &rootCmd{
exit: exit,
}
rootCmd := &cobra.Command{
Use: "raito",
Short: "The Raito CLI to take care of all your data access needs.",
Long: rootDescription,
Version: version,
}
cobra.OnInitialize(root.initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, constants.ConfigFileFlag, "", "The config file (default is ./raito.yml).")
rootCmd.PersistentFlags().String(constants.LogFileFlag, "", "The log file to store structured logs in. If not specified, no logging to file is done.")
rootCmd.PersistentFlags().Bool(constants.LogOutputFlag, false, "When set, logging is sent to the command line (stderr) instead of more human readable output.")
rootCmd.PersistentFlags().Bool(constants.DebugFlag, false, fmt.Sprintf("If set, extra debug logging is generated. Only useful in combination with %s or %s", constants.LogFileFlag, constants.LogOutputFlag))
BindFlag(constants.ConfigFileFlag, rootCmd)
BindFlag(constants.DebugFlag, rootCmd)
BindFlag(constants.LogFileFlag, rootCmd)
BindFlag(constants.LogOutputFlag, rootCmd)
viper.SetDefault(constants.LogFileFlag, "")
root.cmd = rootCmd
initRunCommand(rootCmd)
initInfoCommand(rootCmd)
initApplyAccessCommand(rootCmd)
return root
}
func hideConfigOptions(rootCmd *cobra.Command, options ...string) {
for _, option := range options {
err := rootCmd.PersistentFlags().MarkHidden(option)
if err != nil {
// No logger yet
fmt.Printf("error while hiding %s flag.\n", option) //nolint:forbidigo
}
}
}
func BindFlag(flag string, cmd *cobra.Command) {
err := viper.BindPFlag(flag, cmd.PersistentFlags().Lookup(flag))
if err != nil {
// No logger yet
fmt.Printf("error while binding flag %q: %s\n", flag, err.Error()) //nolint:forbidigo
}
}
// 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(version string, args []string, exit func(int)) {
newRootCmd(version, exit).Execute(args)
}
func (cmd *rootCmd) initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME/.raito")
viper.SetConfigName("raito")
}
viper.SetEnvPrefix("RAITO")
replacer := strings.NewReplacer(".", "_", "-", "_")
viper.SetEnvKeyReplacer(replacer)
viper.AutomaticEnv()
err := viper.ReadInConfig()
if err != nil && !strings.HasSuffix(viper.ConfigFileUsed(), "/raito") && viper.ConfigFileUsed() != "" {
// No logger yet
fmt.Printf("error while reading config file %q: %s\n", viper.ConfigFileUsed(), err.Error()) //nolint:forbidigo
cmd.exitForError()
}
viper.WatchConfig()
if viper.ConfigFileUsed() != "" {
hclog.L().Debug(fmt.Sprintf("Using config file: %s", viper.ConfigFileUsed()))
}
}