-
Notifications
You must be signed in to change notification settings - Fork 688
/
Copy pathroot.go
77 lines (60 loc) · 1.92 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
package single
import (
"context"
"flag"
"fmt"
"os"
"github.com/flyteorg/flyte/flytestdlib/logger"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/flyteorg/flyte/flytestdlib/config"
"github.com/flyteorg/flyte/flytestdlib/config/viper"
_ "github.com/flyteorg/flyte/flytestdlib/promutils"
)
var (
cfgFile string
configAccessor = viper.NewAccessor(config.Options{})
)
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "flyte",
Short: "Flyte cluster",
Long: `
Use the start subcommand which will start the Flyte cluster
flyte start --config flyte_config.yaml
`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return initConfig(cmd.Flags())
},
}
// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() error {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
return err
}
return nil
}
func init() {
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
// Add persistent flags - persistent flags persist through all sub-commands
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is ./flyte.yaml)")
RootCmd.AddCommand(viper.GetConfigCommand())
// Allow viper to read the value of the flags
configAccessor.InitializePflags(RootCmd.PersistentFlags())
err := flag.CommandLine.Parse([]string{})
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
}
func initConfig(flags *pflag.FlagSet) error {
configAccessor = viper.NewAccessor(config.Options{
SearchPaths: []string{cfgFile, ".", "/etc/flyte/config", "$GOPATH/src/github.com/flyteorg/flyte"},
StrictMode: false,
})
logger.Infof(context.TODO(), "Using config file: %v", configAccessor.ConfigFilesUsed())
configAccessor.InitializePflags(flags)
return configAccessor.UpdateConfig(context.TODO())
}