/
root.go
111 lines (89 loc) · 2.83 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
package cmd
import (
"fmt"
"os"
"strings"
"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/tenant-api/db"
"go.infratographer.com/tenant-api/internal/config"
)
const appName = "tenant-api"
var (
cfgFile string
logger *zap.SugaredLogger
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "infra-tenant-api",
Short: "Infratographer Tenant API Service handles tenant hierarchy",
}
// 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() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is /etc/infratographer/tenant-api.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) })
// OTEL Flags
otelx.MustViperFlags(viper.GetViper(), rootCmd.Flags())
// Database Flags
crdbx.MustViperFlags(viper.GetViper(), rootCmd.Flags())
// Add migrate command
goosex.RegisterCobraCommand(rootCmd, func() {
goosex.SetBaseFS(dbm.Migrations)
goosex.SetLogger(logger)
goosex.SetDBURI(config.AppConfig.CRDB.GetURI())
})
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
viper.AddConfigPath("/etc/infratographer/")
viper.SetConfigType("yaml")
viper.SetConfigName("tenant-api")
}
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
viper.SetEnvPrefix("tenantapi")
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()
}
// 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)
}
}