-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
132 lines (121 loc) · 2.82 KB
/
server.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package cmd
import (
cloudconfig "cloudsidecar/pkg/config"
"cloudsidecar/pkg/logging"
"cloudsidecar/pkg/server"
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
"path/filepath"
"strings"
)
var config *cloudconfig.Config
var change chan string
var rootCmd = &cobra.Command{
Use: "server",
Short: "Run cloud cloudsidecar",
Long: `Run cloud cloudsidecar`,
Run: func(cmd *cobra.Command, args []string) {
server.Main(config, change, cmd, args)
},
}
var configFile string
var configDir string
var versionFlag bool
var watcher *fsnotify.Watcher
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&configFile, "config", "", "config file")
rootCmd.PersistentFlags().StringVar(&configDir, "config-dir", "", "config directory")
rootCmd.PersistentFlags().BoolVar(&versionFlag, "version", false, "display version")
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func readInConfigsFromDirectory() error {
i := 0
var err error
if watcher != nil {
if err = watcher.Close(); err != nil {
logging.Log.Error("Close failure", err)
return err
}
}
watcher, err = fsnotify.NewWatcher()
if err != nil {
logging.Log.Error("Watch error", err)
return err
}
if watcherErr := watcher.Add(configDir); watcherErr != nil {
return watcherErr
}
err = filepath.Walk(configDir, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() && isConfigFile(info.Name()) {
logging.Log.Info("Loading config file %s", path)
viper.SetConfigFile(path)
if i == 0 {
i++
return viper.ReadInConfig()
} else {
i++
return viper.MergeInConfig()
}
} else {
return nil
}
})
go watchMultipleFiles()
return err
}
func isConfigFile(filename string) bool {
return strings.HasSuffix(filename, ".yaml") || strings.HasSuffix(filename, ".json")
}
func watchMultipleFiles() {
select {
case e, ok := <-watcher.Events:
if ok {
if err := readInConfigsFromDirectory(); err != nil {
logging.Log.Error("Error reloading", err)
}
if isConfigFile(e.Name) {
change <- e.Name
}
return
}
case err, _ := <-watcher.Errors:
logging.Log.Errorf("Error watching", err)
return
}
return
}
func initConfig() {
config = &cloudconfig.Config{}
change = make(chan string)
logging.Init("", "")
if versionFlag {
fmt.Println(server.Version)
os.Exit(0)
}
var err error
if configFile != "" {
viper.SetConfigFile(configFile)
err = viper.ReadInConfig()
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
change <- e.Name
})
} else if configDir != "" {
err = readInConfigsFromDirectory()
} else {
panic("--config or --config-dir required")
}
if err != nil {
logging.Log.Error("", err)
panic(fmt.Sprintf("Cannot load config %s %s", os.Args[1], err))
}
}