/
config.go
75 lines (59 loc) · 1.87 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
package config
import (
"path"
"time"
log "github.com/sirupsen/logrus"
gofig "github.com/akutz/gofig/types"
"github.com/akutz/gotil"
"github.com/rexray/rexray/libstorage/api/context"
"github.com/rexray/rexray/libstorage/api/registry"
"github.com/rexray/rexray/libstorage/api/types"
"github.com/rexray/rexray/libstorage/api/utils"
)
// NewConfig returns a new configuration instance.
func NewConfig(ctx types.Context) (gofig.Config, error) {
pathConfig := context.MustPathConfig(ctx)
config := registry.NewConfig()
etcYML := path.Join(pathConfig.Etc, "config.yml")
etcYAML := path.Join(pathConfig.Etc, "config.yaml")
userHomeDir := gotil.HomeDir()
usrYML := path.Join(userHomeDir, "config.yml")
usrYAML := path.Join(userHomeDir, "config.yaml")
if err := readConfigFile(config, etcYML); err != nil {
return nil, err
}
if err := readConfigFile(config, etcYAML); err != nil {
return nil, err
}
if err := readConfigFile(config, usrYML); err != nil {
return nil, err
}
if err := readConfigFile(config, usrYAML); err != nil {
return nil, err
}
types.BackCompat(config)
return config, nil
}
// UpdateLogLevel updates the log level based on the config.
func UpdateLogLevel(config gofig.Config) {
ll, err := log.ParseLevel(config.GetString(types.ConfigLogLevel))
if err != nil {
return
}
log.SetLevel(ll)
}
func readConfigFile(config gofig.Config, path string) error {
if !gotil.FileExists(path) {
return nil
}
return config.ReadConfigFile(path)
}
// DeviceAttachTimeout gets the configured device attach timeout.
func DeviceAttachTimeout(config gofig.Config) time.Duration {
return utils.DeviceAttachTimeout(
config.GetString(types.ConfigDeviceAttachTimeout))
}
// DeviceScanType gets the configured device scan type.
func DeviceScanType(config gofig.Config) types.DeviceScanType {
return types.ParseDeviceScanType(config.GetInt(types.ConfigDeviceScanType))
}