-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
86 lines (71 loc) · 1.56 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
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
)
// common config parameters
type Common struct {
Interval int `yaml:"interval"`
}
// jenkins access credentials and other parameters
type Jenkins struct {
Url string `yaml:"url"`
Verify bool `yaml:"verify"`
User string `yaml:"user"`
Password string `yaml:"password"`
}
// access credentials to postgres database
type Datastore struct {
Url string `yaml:"url"`
Verify bool `yaml:"verify"`
Index string `yaml:"index"`
User string `yaml:"user"`
Password string `yaml:"password"`
}
// full configuration represented by config file
type config struct {
Common `yaml:"common"`
Jenkins `yaml:"jenkins"`
Datastore `yaml:"datastore"`
}
// print config sample
func printConfExample() {
c := config{
Common{
Interval: 600,
},
Jenkins{
Url: "https://localhost:8080",
Verify: true,
User: "user",
Password: "password",
},
Datastore{
Url: "https://localhost:9200",
Verify: true,
Index: "jenkins",
User: "user",
Password: "password",
},
}
b, err := yaml.Marshal(c)
logFatal("Print config example", err)
fmt.Println(string(b))
os.Exit(0)
}
// load configuration file
func loadConf(path string) *config {
conf := &config{}
file, err := os.Open(path)
defer file.Close()
logFatal("Load conf: open file", err)
b, err := ioutil.ReadAll(file)
logFatal("Load conf: read file", err)
err = yaml.Unmarshal(b, conf)
logFatal("Load conf: json", err)
log.Printf("Config loaded: %s", path)
return conf
}