-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
131 lines (112 loc) · 2.78 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
package main
import (
"errors"
"fmt"
"net/url"
"os"
"regexp"
"strings"
"time"
"github.com/tactycal/agent/stubUtils"
)
const (
DefaultConfigurationFile = "/etc/tactycal/agent.conf"
DefaultApiUri = "https://api.tactycal.com"
)
type Config struct {
Token string
Uri string
Proxy *url.URL
Labels []string
StatePath string
ClientTimeout time.Duration
}
func NewConfig(file, statePath string, clientTimeout time.Duration) (*Config, error) {
cfg, err := readConfigurationFromFile(file)
if err != nil {
return nil, errors.New("configuration: " + err.Error())
}
// set defaults
if cfg["uri"] == "" {
cfg["uri"] = DefaultApiUri
} else {
// trim trailing slashes
cfg["uri"] = strings.TrimRight(cfg["uri"], "/")
}
// @todo fix proxy handling
urlProxy, err := url.Parse(cfg["proxy"])
if err != nil {
return nil, errors.New("configuration: unable to parse proxy URL, reason:" + err.Error())
}
if urlProxy.String() == "" {
urlProxy = nil
}
// check token is set
if cfg["token"] == "" {
return nil, errors.New("configuration: No token provided.")
}
// set client timeout
timeout := clientTimeout
if cfg["timeout"] != "" {
d, err := time.ParseDuration(cfg["timeout"])
if err != nil {
return nil, fmt.Errorf("Failed to parse timeout; reason: %s", err.Error())
}
timeout = d
}
// set state
if cfg["state"] == "" {
cfg["state"] = statePath
}
// finally! :)
config := &Config{
Token: cfg["token"],
Uri: cfg["uri"],
Proxy: urlProxy,
Labels: cleanSplit(cfg["labels"]),
ClientTimeout: timeout,
StatePath: cfg["state"],
}
return config, nil
}
func readConfigurationFromFile(file string) (map[string]string, error) {
data, err := stubUtils.ReadFile(file)
if err != nil {
return nil, err
}
matches := make(map[string]string)
// no major restrictions for now
// matches are trimmed later on, no extra check are done at this time
pattern := "(.*)=(.*)"
m := regexp.MustCompile(pattern).FindAllStringSubmatch(string(data), -1)
for _, v := range m {
key := strings.TrimSpace(v[1])
value := strings.TrimSpace(v[2])
matches[key] = strings.Trim(value, `"`)
}
return matches, nil
}
func cleanSplit(str string) []string {
if str == "" {
return []string{}
}
// split string by ,
arr := strings.Split(str, ",")
// trim all values
for key, val := range arr {
arr[key] = strings.TrimSpace(val)
// check if we need to substitute any of the values
if arr[key][:1] == `$` {
arr[key] = Getenv(arr[key][1:], arr[key][1:])
}
}
return arr
}
// Retuns value of environment variable `name`. If env. variable is empty the
// defaultValue is returned.
func Getenv(name, defaultValue string) string {
if v := os.Getenv(name); v != "" {
return v
}
return defaultValue
}