-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
172 lines (143 loc) · 4.81 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package config
import (
"fmt"
"path/filepath"
"strconv"
"naksu/constants"
"naksu/log"
"github.com/go-ini/ini"
"github.com/mitchellh/go-homedir"
)
var cfg *ini.File
func getIniFilePath() string {
homeDir, errHome := homedir.Dir()
if errHome != nil {
panic("Could not get home directory")
}
return filepath.Join(homeDir, "naksu.ini")
}
func setIfMissing(section string, key string, defaultValue string) {
if !cfg.Section(section).HasKey(key) {
cfg.Section(section).Key(key).SetValue(defaultValue)
}
}
type defaultValue struct {
section, key, value string
}
var defaults = []defaultValue{
defaultValue{"common", "iniVersion", strconv.FormatInt(1, 10)},
defaultValue{"common", "language", constants.AvailableLangs[0].ConfigValue},
defaultValue{"selfupdate", "disabled", strconv.FormatBool(false)},
defaultValue{"environment", "nic", constants.AvailableNics[0].ConfigValue},
defaultValue{"environment", "extnic", ""},
}
func fillDefaults() {
for _, defaultValue := range defaults {
setIfMissing(defaultValue.section, defaultValue.key, defaultValue.value)
}
}
func getDefault(section string, key string) string {
for _, defaultValue := range defaults {
if defaultValue.section == section && defaultValue.key == key {
return defaultValue.value
}
}
panic(fmt.Sprintf("Default for %v / %v is not defined!", section, key))
}
func getIniKey(section string, key string) *ini.Key {
return cfg.Section(section).Key(key)
}
func getBoolean(section string, key string) bool {
value, err := getIniKey(section, key).Bool()
if err != nil {
log.Debug(fmt.Sprintf("Parsing key %s / %s as bool failed", section, key))
defaultValue := getDefault(section, key)
value, err = strconv.ParseBool(defaultValue)
if err != nil {
panic(fmt.Sprintf("Default boolean parsing for %v / %v (%v) failed to parse to boolean!", section, key, defaultValue))
}
setValue(section, key, defaultValue)
}
return value
}
func getString(section string, key string) string {
return getIniKey(section, key).String()
}
func setValue(section string, key string, value string) {
log.Debug(fmt.Sprintf("Setting new configuration: section %s, key: %s, value: %s", section, key, value))
cfg.Section(section).Key(key).SetValue(value)
save()
}
// Load or initialize configuration to empty object
func Load() {
naksuIniPath := getIniFilePath()
var err error
cfg, err = ini.Load(naksuIniPath)
if err != nil {
log.Debug(fmt.Sprintf("%s not found, setting up empty config with defaults", naksuIniPath))
cfg = ini.Empty()
}
fillDefaults()
save()
}
func validateStringChoice(section string, key string, choices []constants.AvailableSelection) string {
value := getString(section, key)
id := constants.GetAvailableSelectionID(value, choices, -1)
if id >= 0 {
return value
}
defaultValue := getDefault(section, key)
log.Debug(fmt.Sprintf("Correcting malformed ini-key %v / %v to default value %v", section, key, defaultValue))
setValue(section, key, defaultValue)
return defaultValue
}
// Save configuration to disk
func save() {
naksuIniPath := getIniFilePath()
err := cfg.SaveTo(naksuIniPath)
if err != nil {
log.Debug(fmt.Sprintf("%s save failed: %v", naksuIniPath, err))
}
}
// GetLanguage returns user language preference. defaults to fi
func GetLanguage() string {
return validateStringChoice("common", "language", constants.AvailableLangs)
}
// SetLanguage stores user language preference
func SetLanguage(language string) {
if constants.GetAvailableSelectionID(language, constants.AvailableLangs, -1) < 0 {
setValue("common", "language", getDefault("common", "language"))
} else {
setValue("common", "language", language)
}
}
// IsSelfUpdateDisabled returns true, if self-update functionality should be disabled
func IsSelfUpdateDisabled() bool {
return getBoolean("selfupdate", "disabled")
}
// SetSelfUpdateDisabled sets the state of self-update functionality
func SetSelfUpdateDisabled(isSelfUpdateDisabled bool) {
setValue("selfupdate", "disabled", strconv.FormatBool(isSelfUpdateDisabled))
}
// GetNic returns vagrant NIC value. Defaults to "virtio"
func GetNic() string {
return validateStringChoice("environment", "nic", constants.AvailableNics)
}
// SetNic sets the state of vagrant NIC value
func SetNic(nic string) {
if constants.GetAvailableSelectionID(nic, constants.AvailableNics, -1) < 0 {
setValue("environment", "nic", getDefault("environment", "nic"))
} else {
setValue("environment", "nic", nic)
}
}
// GetExtNic returns vagrant EXTNIC value. If empty no external network connection
// should be passed to Vagrant via EXTNIC environment variable.
func GetExtNic() string {
// Since there are no pre-set selection of variables we dont use validateStringChoice() here
return getString("environment", "extnic")
}
// SetExtNic sets the state of vagrant EXTNIC value
func SetExtNic(nic string) {
setValue("environment", "extnic", nic)
}