/
root.go
71 lines (59 loc) · 1.9 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
// Package cmd enables simple management of the backend via CLI
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/bsinou/vitrnx-goback/conf"
)
var ()
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "vitrnx-goback",
Short: "Simple Go Backend for the VitrnX Project",
Long: `
`,
PreRun: func(cmd *cobra.Command, args []string) {
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if args[0] == "" {
cmd.Println("no instance name provided, canot launch Vitrnx Backend")
os.Exit(1)
}
conf.VitrnxInstanceID = args[0]
// Override environment type if provided
if viper.GetString(conf.KeyEnvType) != "" {
// TODO check if env value is valid
conf.Env = viper.GetString(conf.KeyEnvType)
}
// Load configuration
viper.SetConfigName(conf.BaseName) // name of config file (without extension)
for _, path := range conf.GetKnownConfFolderPaths() {
fmt.Println("Adding confing path: " + path)
viper.AddConfigPath(path)
}
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("fatal error config file: %s", err))
}
},
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
func init() {
viper.SetEnvPrefix("vitrnx")
viper.AutomaticEnv()
flags := RootCmd.PersistentFlags()
flags.StringP(conf.KeyEnvType, "e", "", "Override default environment mode defined by the build. Valid values are: dev, test, staging, prod")
viper.BindPFlag(conf.KeyEnvType, RootCmd.PersistentFlags().Lookup(conf.KeyEnvType))
}
// 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)
}
}