-
Notifications
You must be signed in to change notification settings - Fork 68
/
main.go
103 lines (91 loc) · 2.91 KB
/
main.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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
cfgCmd "github.com/kyma-incubator/reconciler/cmd/mothership/config"
localCmd "github.com/kyma-incubator/reconciler/cmd/mothership/local"
msCmd "github.com/kyma-incubator/reconciler/cmd/mothership/mothership"
"github.com/kyma-incubator/reconciler/internal/cli"
file "github.com/kyma-incubator/reconciler/pkg/files"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
envVarPrefix = "RECONCILER"
)
var DefaultConfigFile string
func main() {
o := &cli.Options{}
cmd := newCmd(
o,
filepath.Base(os.Args[0]),
"Kyma reconciler CLI",
"Command line tool to administrate the Kyma reconciler system")
cmd.AddCommand(cfgCmd.NewCmd(o))
cmd.AddCommand(msCmd.NewCmd(o))
cmd.AddCommand(localCmd.NewCmd(localCmd.NewOptions(o)))
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}
func newCmd(o *cli.Options, name, shortDesc, longDesc string) *cobra.Command {
cmd := &cobra.Command{
Use: name,
Short: shortDesc,
Long: longDesc,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
//validate given user input
if err := o.Validate(); err != nil {
return err
}
return o.InitApplicationRegistry(false)
},
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
if o.Registry != nil {
//shutdown object context
return o.Registry.Close()
}
return nil
},
SilenceErrors: false,
SilenceUsage: true,
}
cobra.OnInitialize(initViper(o))
cmd.PersistentFlags().StringVarP(&DefaultConfigFile, "config", "c", "configs/reconciler.yaml", `Path to the configuration file`)
cmd.PersistentFlags().BoolVarP(&o.Verbose, "verbose", "v", false, "Show detailed information about the executed command actions")
cmd.PersistentFlags().BoolVar(&o.NonInteractive, "non-interactive", false, "Enables the non-interactive shell mode")
cmd.PersistentFlags().BoolVarP(&o.InitRegistry, "init-registry", "r", false, "Auto-initialize application registry ")
cmd.PersistentFlags().BoolP("help", "h", false, "Command help")
return cmd
}
func initViper(o *cli.Options) func() {
return func() {
//read configuration from ENV vars
viper.SetEnvPrefix(envVarPrefix)
viper.AutomaticEnv()
//read configuration from config file
cfgFile, err := getConfigFile()
if err != nil {
o.Logger().Warn(err.Error())
return
}
viper.SetConfigFile(cfgFile)
if err := viper.ReadInConfig(); err == nil {
o.Logger().Debugf("Using configuration file '%s'", viper.ConfigFileUsed())
} else {
o.Logger().Errorf("Failed to read configuration file '%s': %s", cfgFile, err)
}
}
}
func getConfigFile() (string, error) {
configFile := strings.TrimSpace(viper.GetString("config"))
if configFile == "" {
configFile = DefaultConfigFile
}
if !file.Exists(configFile) {
return "", fmt.Errorf("no configuration file found: set environment variable $%s_CONFIG or define it as CLI parameter", envVarPrefix)
}
return configFile, nil
}