/
root.go
110 lines (90 loc) · 3 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
// Package cmd is our cobra/viper cli implementation
package cmd
import (
"fmt"
"os"
"strings"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.infratographer.com/x/crdbx"
"go.infratographer.com/x/goosex"
"go.infratographer.com/x/loggingx"
"go.infratographer.com/x/otelx"
"go.infratographer.com/x/versionx"
"go.infratographer.com/x/viperx"
"go.uber.org/zap"
dbm "go.infratographer.com/load-balancer-api/db"
"go.infratographer.com/load-balancer-api/internal/config"
)
// TODO: update app name
const appName = "loadbalancer-api"
var (
cfgFile string
logger *zap.SugaredLogger
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: appName,
Short: "A utility for managing load balancers definitions",
Long: `loadbalancer-api is a service for managing abstract lb relationships`,
}
// 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() {
cobra.CheckErr(rootCmd.Execute())
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/."+appName+".yaml)")
viperx.MustBindFlag(viper.GetViper(), "config", rootCmd.PersistentFlags().Lookup("config"))
// Logging flags
loggingx.MustViperFlags(viper.GetViper(), rootCmd.PersistentFlags())
// Register version command
versionx.RegisterCobraCommand(rootCmd, func() { versionx.PrintVersion(logger) })
otelx.MustViperFlags(viper.GetViper(), rootCmd.Flags())
crdbx.MustViperFlags(viper.GetViper(), rootCmd.Flags())
// Setup migrate command
goosex.RegisterCobraCommand(rootCmd, func() {
goosex.SetBaseFS(dbm.Migrations)
goosex.SetDBURI(config.AppConfig.CRDB.URI)
goosex.SetLogger(logger)
})
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
cobra.CheckErr(err)
// load the config file
viper.AddConfigPath(home)
viper.SetConfigName("." + appName)
}
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
viper.SetEnvPrefix("loadbalancerapi")
viper.AutomaticEnv() // read in environment variables that match
setupAppConfig()
// setupLogging()
logger = loggingx.InitLogger(appName, config.AppConfig.Logging)
// If a config file is found, read it in.
err := viper.ReadInConfig()
if err == nil {
logger.Infow("using config file",
"file", viper.ConfigFileUsed(),
)
}
}
// setupAppConfig loads our config.AppConfig struct with the values bound by
// viper. Then, anywhere we need these values, we can just return to AppConfig
// instead of performing viper.GetString(...), viper.GetBool(...), etc.
func setupAppConfig() {
err := viper.Unmarshal(&config.AppConfig)
if err != nil {
fmt.Printf("unable to decode app config: %s", err)
os.Exit(1)
}
}