-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.go
56 lines (45 loc) · 1.05 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
package speed
import (
"bufio"
"os"
"path/filepath"
"regexp"
)
// rootPath stores path to the pcp root installation
var rootPath string
// confPath stores path to pcp.conf
var confPath string
// config stores the configuration as defined in current PCP environment
var config map[string]string
// pat stores a valid key-value pattern line
var pat = "([A-Z0-9_]+)=(.*)"
// initConfig initializes the config constants
func initConfig() error {
re, _ := regexp.Compile(pat)
r, ok := os.LookupEnv("PCP_DIR")
if !ok {
r = "/"
}
rootPath = r
c, ok := os.LookupEnv("PCP_CONF")
if !ok {
c = filepath.Join(rootPath, "etc", "pcp.conf")
}
confPath = c
f, err := os.Open(confPath)
if err != nil {
return err
}
// if we reach at this point, it means we have a valid config
// that can be read, so we can make the map non-nil
config = make(map[string]string)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
t := scanner.Text()
if re.MatchString(t) {
matches := re.FindStringSubmatch(t)
config[matches[1]] = matches[2]
}
}
return nil
}