This repository has been archived by the owner on Oct 28, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (90 loc) · 2.35 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
104
105
106
107
108
109
110
111
112
package cmd
import (
"os"
"github.com/fsnotify/fsnotify"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/updatecli/updatemonitor/pkg/engine"
)
var (
// Server configuration file
cfgFile string
// Verbose allows to enable/disable debug logging
verbose bool
rootCmd = &cobra.Command{
Use: "updatemonitor",
Short: "updatemonitor is an Update monitoring platform",
PostRun: func(cmd *cobra.Command, args []string) {
logrus.Infoln("See you next time")
},
}
)
// Execute executes the root command.
func Execute() {
if err := rootCmd.Execute(); err != nil {
logrus.Errorf("%s", err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "set config file")
rootCmd.PersistentFlags().BoolVarP(&verbose, "debug", "", false, "set log level")
rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
if verbose {
logrus.SetLevel(logrus.DebugLevel)
}
}
rootCmd.AddCommand(
versionCmd,
serverCmd,
agentCmd,
)
}
func initConfig() {
viper.SetConfigName("config") // name of config file (without extension)
if cfgFile != "" {
viper.SetConfigName(cfgFile)
}
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
viper.AddConfigPath("/etc/updatemonitor/") // path to look for the config file in
viper.AddConfigPath("$HOME/.updatemonitor") // call multiple times to add many search paths
viper.AddConfigPath(".") // optionally look for config in the working directory
// Find and read the config file
if err := viper.ReadInConfig(); err != nil {
logrus.Errorln(err)
}
viper.OnConfigChange(func(e fsnotify.Event) {
logrus.Infof("Config file changed:", e.Name)
var o engine.Options
if err := viper.Unmarshal(&o); err != nil {
logrus.Errorln(err)
}
engine := engine.Engine{
Options: o,
}
if err := engine.SaveConfigDashboard(); err != nil {
logrus.Errorln(err)
}
})
viper.WatchConfig()
}
func run(command string) error {
var o engine.Options
if err := viper.Unmarshal(&o); err != nil {
return err
}
e := engine.Engine{
Options: o,
}
switch command {
case "serverStart":
e.StartServer()
case "agentStart":
e.StartRunner()
default:
logrus.Warnf("Wrong command %q", command)
}
return nil
}