-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure.go
52 lines (42 loc) · 1.16 KB
/
configure.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
package main
import (
"github.com/fsnotify/fsnotify"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
func configure(file string, registries imagesAndRegistries, log *logrus.Logger) error {
viper.AutomaticEnv()
viper.SetConfigFile(file)
viper.SetDefault("port", "8000")
if err := viper.ReadInConfig(); err != nil {
return err
}
updateConfig(registries, log)
viper.WatchConfig()
viper.OnConfigChange(func(_ fsnotify.Event) {
updateConfig(registries, log)
})
return nil
}
func updateConfig(v imagesAndRegistries, log *logrus.Logger) {
logLevel := viper.GetString("log_level")
registries := viper.GetStringSlice("allowed_registries")
images := viper.GetStringSlice("allowed_images")
log.WithField("logLevel", logLevel).
WithField("registries", registries).
WithField("images", images).
Info("Loaded config")
if logLevel != "" {
if level, err := logrus.ParseLevel(logLevel); err == nil {
log.Level = level
}
}
v.SetRegistries(registries)
if err := v.SetImages(images); err != nil {
log.Errorf("images %s is invalid: %s", images, err)
}
}
type imagesAndRegistries interface {
SetImages([]string) error
SetRegistries([]string)
}