forked from b3scale/b3scale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpropfile.go
75 lines (62 loc) · 1.43 KB
/
propfile.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
package config
import (
"bufio"
"os"
"regexp"
"strings"
)
/*
Read a BBB .properties file
*/
var rePropertyRef *regexp.Regexp = regexp.MustCompile(`\${(.*)}`)
// Properties is a map of BBB properties.
// The map stores the raw data. Retriving values should
// be done through the accessor which will resolve refs.
type Properties map[string]string
// Get retrievs a value from the properties map and
// will resolve a reference.
func (p Properties) Get(key string) (string, bool) {
var (
val string
ok bool
)
val, ok = p[key]
if !ok {
return "", false
}
// Resolve ref if any
val = rePropertyRef.ReplaceAllStringFunc(val, func(ref string) string {
var rval string
rkey := ref[2 : len(ref)-1]
rval, ok = p[rkey]
return rval
})
return val, ok
}
// ReadPropertiesFile consumes a BBB properties file
func ReadPropertiesFile(filename string) (Properties, error) {
// This is pretty much linewise key=value
f, err := os.Open(filename)
if err != nil {
return nil, err
}
props := make(Properties)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "#") {
continue // Skip comments
}
if !strings.ContainsRune(line, '=') {
continue // Skip non assignments
}
tokens := strings.SplitN(line, "=", 2)
if len(tokens) < 2 {
continue // Huh.
}
k := strings.TrimSpace(tokens[0])
v := strings.TrimSpace(tokens[1])
props[k] = v
}
return props, nil
}