-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
81 lines (74 loc) · 2.38 KB
/
config.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
package backive
import (
"fmt"
"log"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
// Configuration struct holding the settings and config items of devices and backups
type Configuration struct {
Settings Settings `mapstructure:"settings"`
Devices Devices `mapstructure:"devices"`
Backups Backups `mapstructure:"backups"`
Vconfig *viper.Viper
}
// Settings struct holds the global configuration items
type Settings struct {
SystemMountPoint string `mapstructure:"systemMountPoint"`
UserMountPoint string `mapstructure:"userMountPoint"`
UnixSocketLocation string `mapstructure:"unixSocketLocation"`
LogLocation string `mapstructure:"logLocation"`
DbLocation string `mapstructure:"dbLocation"`
}
// CreateViper creates a viper instance for usage later
func (c *Configuration) CreateViper() {
if c.Vconfig == nil {
vconfig := viper.New()
// vconfig.Debug()
vconfig.SetConfigName("backive")
// do not set config file explicitly or viper doesnt search for it, and /etc search fails
//vconfig.SetConfigFile("backive.yml")
//vconfig.SetConfigFile("backive.yaml")
vconfig.SetConfigType("yaml")
//vconfig.AddConfigPath("$HOME/.backive/")
vconfig.AddConfigPath(".") // backup config in local dir
vconfig.AddConfigPath("/etc/backive/") // system config
vconfig.OnConfigChange(func(e fsnotify.Event) {
log.Printf("Event: %s", e)
if e.Op == fsnotify.Write {
log.Printf("Reloading %s", e.Name)
c.Load()
}
})
vconfig.WatchConfig()
c.Vconfig = vconfig
}
}
// Load loads the configuration from the disk
func (c *Configuration) Load() {
c.CreateViper()
vc := c.Vconfig
if err := vc.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
panic(fmt.Errorf("fatal: No config file could be found: %w", err))
}
panic(fmt.Errorf("fatal error config file: %w ", err))
}
log.Printf("Configuration file used: %s", vc.ConfigFileUsed())
//Unmarshal all into Configuration type
err := vc.Unmarshal(c)
if err != nil {
fmt.Printf("Error occured when loading config: %v\n", err)
panic("No configuration available!")
}
for k, v := range c.Backups {
log.Printf("Initializing backup '%s'\n", k)
v.Name = k
log.Printf("Initialized backup '%s'\n", v.Name)
}
for k, v := range c.Devices {
log.Printf("Initializing device '%s'\n", k)
v.Name = k
log.Printf("Initialized device '%s'\n", v.Name)
}
}