-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.go
executable file
·76 lines (67 loc) · 1.68 KB
/
configuration.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
package configuration
import (
"fmt"
"github.com/fsnotify/fsnotify"
"log"
"path/filepath"
"strings"
"github.com/spf13/viper"
)
const (
FileSource = "file"
RemoteSource = "remote"
)
type Configuration struct {
Schedulers []Scheduler `mapstructure:"schedulers"`
}
type Scheduler struct {
Job string `mapstructure:"job"`
Cron string `mapstructure:"cron"`
Task struct {
URL string `mapstructure:"url"`
Method string `mapstructure:"method"`
Body string `mapstructure:"body"`
Header string `mapstructure:"header"`
} `mapstructure:"task"`
}
type RemoteProvider struct {
Secure bool
Provider string
Endpoint string
Path string
SecretKeyring string
}
var Config Configuration
func Load() error {
viper.SetConfigName("configuration")
viper.AddConfigPath("configuration")
if err := viper.ReadInConfig(); err != nil {
log.Fatalf("Error reading config file, %s", err)
}
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("[INFO] Config file changed:", e.Name)
})
viper.WatchConfig()
err := viper.Unmarshal(&Config)
if err == nil {
fmt.Println("[INFO] Configuration file has been loaded.")
}
return err
}
func LoadRemote(remote RemoteProvider) error {
var err error
if remote.Secure {
err = viper.AddSecureRemoteProvider(remote.Provider, remote.Endpoint, remote.Path, remote.SecretKeyring)
} else {
err = viper.AddRemoteProvider(remote.Provider, remote.Endpoint, remote.Path)
}
viper.SetConfigType(strings.ReplaceAll(filepath.Ext(remote.Path), ".", ""))
err = viper.ReadRemoteConfig()
if err == nil {
err = viper.Unmarshal(&Config)
}
if err == nil {
fmt.Println("[INFO] Configuration remote has been loaded.")
}
return err
}