/
root.go
63 lines (52 loc) · 1.49 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
package cmd
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/adrg/xdg"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"github.com/oom-ai/oomstore/pkg/oomstore/types"
"github.com/oom-ai/oomstore/version"
)
var cfgFile string
var defaultCfgFile = filepath.Join(xdg.Home, ".config", "oomstore", "config.yaml")
var oomStoreCfg types.OomStoreConfig
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "oomcli",
Short: "A cli tool that lets you control the oom feature store.",
Version: version.String(),
}
// 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() {
cobra.OnInitialize(initConfig)
rootCmd.SetHelpCommand(&cobra.Command{
Use: "no-help",
Hidden: true,
})
pFlags := rootCmd.PersistentFlags()
pFlags.StringVar(&cfgFile, "config", defaultCfgFile, "config file")
rootCmd.SetVersionTemplate(`{{printf "%s\n" .Version}}`)
}
// initConfig reads in config file
func initConfig() {
if envCfgFile := os.Getenv("OOMCLI_CONFIG"); envCfgFile != "" {
cfgFile = envCfgFile
}
cfgContent, err := ioutil.ReadFile(cfgFile)
if err != nil {
exitf("failed reading config file: %v\n", err)
}
if err := yaml.Unmarshal(cfgContent, &oomStoreCfg); err != nil {
exitf("failed loading config : %v\n", err)
}
}