-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
96 lines (77 loc) · 2.57 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package godin
import (
"fmt"
"os"
"path"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
type ConfigProvider interface {
// Identifier returns the unique identifier by which a module can be distinguished from others.
// An identifier is a '.' separated namespace which will be resolved to the config hierarchy.
Identifier() string
// Configuration returns the configuration of the ConfigProvider object.
Configuration() interface{}
}
const ConfigFileName = "godin"
const ConfigFileType = "yaml"
type Configurator struct {
configRegistry map[string]ConfigProvider
RootPath string
}
func NewConfigurator(configPath string) *Configurator {
return &Configurator{
configRegistry: make(map[string]ConfigProvider),
RootPath: configPath,
}
}
func (cfg *Configurator) Initialize() error {
viper.AddConfigPath(cfg.RootPath)
viper.SetConfigName(ConfigFileName)
viper.SetConfigType(ConfigFileType)
return viper.ReadInConfig()
}
func (cfg *Configurator) IsSet(key string) bool {
return viper.IsSet(key)
}
func (cfg *Configurator) Get(key string) interface{} {
return viper.Get(key)
}
func (cfg *Configurator) Unmarshal(key string, target interface{}) error {
return viper.UnmarshalKey(key, target)
}
func (cfg *Configurator) Register(provider ConfigProvider) error {
if existing := cfg.configRegistry[provider.Identifier()]; existing != nil {
return fmt.Errorf("a config provider with identifier '%s' is already registered", provider.Identifier())
}
cfg.configRegistry[provider.Identifier()] = provider
return nil
}
func (cfg *Configurator) Save() error {
for _, reg := range cfg.configRegistry {
viper.Set(reg.Identifier(), reg.Configuration())
}
return viper.WriteConfig()
}
// ConfigExists checks whether a configuration file exists. That's the indicator whether a project
// has been initialized.
func (cfg *Configurator) ConfigExists() bool {
p := path.Join(cfg.RootPath, fmt.Sprintf("%s.%s", ConfigFileName, ConfigFileType))
if _, err := os.Stat(p); err != nil {
return false
}
return true
}
// EnsureConfigFile a godin project directory in the configured 'rootPath' and ensure a configuration file exists.
// If the project is already initialized, nothing is returned (silent fail) which makes this method idempotent.
// Note: If a config file is created, it will be empty.
func (cfg *Configurator) EnsureConfigFile() error {
if cfg.ConfigExists() {
return nil
}
_, err := os.Create(path.Join(cfg.RootPath, fmt.Sprintf("%s.%s", ConfigFileName, ConfigFileType)))
if err != nil {
return errors.Wrap(err, "failed to initialize project")
}
return nil
}