/
config.go
61 lines (51 loc) · 1.14 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
package config
import (
"encoding/json"
"log"
"os"
"github.com/pelletier/go-toml"
"gopkg.in/yaml.v2"
)
// Global config object singleton
var gloablConfigSingleton *GlobalConfig
func InitGlobalConfig(conf *GlobalConfig) {
gloablConfigSingleton = conf
}
// GetProvisionerConfig returns a copy of global config object
func GetGlobalConfig() GlobalConfig {
return *gloablConfigSingleton
}
func PrintConfigTemplate(format string) {
if len(format) < 1 {
format = "yaml"
}
printFile := func(content []byte, format string) {
f, err := os.OpenFile("provision-conf.template."+format, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0755)
defer f.Close()
if err != nil {
log.Fatal(err)
return
}
f.Write(content)
f.Sync()
}
confTemplate := gloablConfigSingleton
if confTemplate == nil {
confTemplate = &GlobalConfig{
LogConfig: LoggerConfig{
Level: "debug",
LogFormat: "text",
},
}
}
var conf []byte
switch format {
case "json":
conf, _ = json.MarshalIndent(&confTemplate, "", " ")
case "yaml":
conf, _ = yaml.Marshal(&confTemplate)
case "toml":
conf, _ = toml.Marshal(&confTemplate)
}
printFile(conf, format)
}