-
Notifications
You must be signed in to change notification settings - Fork 15
/
root.go
88 lines (73 loc) · 1.86 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
package cmd
import (
"os"
"github.com/cybozu-go/cke"
"github.com/cybozu-go/etcdutil"
"github.com/cybozu-go/log"
"github.com/cybozu-go/well"
"github.com/spf13/cobra"
"go.etcd.io/etcd/clientv3"
"sigs.k8s.io/yaml"
)
var (
cfgFile string
etcdClient *clientv3.Client
storage cke.Storage
inf = &cliInfrastructure{}
)
func loadConfig(p string) (*etcdutil.Config, error) {
b, err := os.ReadFile(p)
if err != nil {
return nil, err
}
cfg := cke.NewEtcdConfig()
err = yaml.Unmarshal(b, cfg)
if err != nil {
return nil, err
}
return cfg, nil
}
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "ckecli",
Short: "command-line interface to control CKE",
Long: `ckecli is a command-line interface to control CKE.
It does not communicate CKE server; instead it communicates
with etcd. CKE server watches etcd to receive any updates.`,
Version: cke.Version,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// without this, each subcommand's RunE would display usage text.
cmd.SilenceUsage = true
err := well.LogConfig{}.Apply()
if err != nil {
return err
}
cfg, err := loadConfig(cfgFile)
if err != nil {
return err
}
etcd, err := etcdutil.NewClient(cfg)
if err != nil {
return err
}
etcdClient = etcd
storage = cke.Storage{Client: etcd}
return nil
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if etcdClient != nil {
etcdClient.Close()
}
inf.Close()
},
}
// 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 {
log.ErrorExit(err)
}
}
func init() {
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "/etc/cke/config.yml", "config file")
}