-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.go
65 lines (55 loc) · 1.43 KB
/
settings.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
package medialocker
import (
"github.com/Sirupsen/logrus"
"github.com/BurntSushi/toml"
)
type Settings struct {
DB dbSettings `toml:"Database"`
Server serverSettings
Logger loggerSettings
}
type dbSettings struct {
DataPath string
LogSQL, MemDB bool
}
type serverSettings struct {
Bind string
AutoBind, AutoCert bool
CertPath, KeyPath string
}
func BuildSettingsTemplate(ctx AppContext) Settings {
fs := ctx.FileSystem()
return Settings{
Logger: loggerSettings{
LogLevel: logrus.InfoLevel,
LogPath: fs.DataPath("locker.log"),
},
Server: serverSettings{
AutoBind: true,
AutoCert: true,
},
DB: dbSettings{
DataPath: fs.DataPath("locker.db"),
},
}
}
func LoadSettings(ctx AppContext, settingsPath string) Settings {
var settings Settings
fs := ctx.FileSystem()
log := ctx.Logger()
if settingsPath == "" {
settingsPath = fs.ConfigPath("locker.conf")
}
log.Must("Failed to ensure config file directory exist at %s.", settingsPath).Do(fs.EnsureFileDirectory(settingsPath))
if fs.FileExists(settingsPath) {
log.Must("Failed to load locker config file %s.", settingsPath).Do(toml.DecodeFile(settingsPath, &settings))
} else {
settings = BuildSettingsTemplate(ctx)
settingsFile, err := fs.Create(settingsPath)
log.Must("Failed to create new config file %s.", settingsPath).Do(err)
defer settingsFile.Close()
encoder := toml.NewEncoder(settingsFile)
encoder.Encode(settings)
}
return settings
}