-
Notifications
You must be signed in to change notification settings - Fork 0
/
Properties.go
59 lines (50 loc) · 1001 Bytes
/
Properties.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
package properties
import (
"bufio"
"errors"
"os"
"strings"
)
var propertie *Propertie
var path string
type Propertie struct {
Pairs []PropertiePair
}
type PropertiePair struct {
Key string
Value string
}
func SetPropertiePath(s string) {
path = s
}
func (p *Propertie) GetProperty(key string) string {
for _, s := range p.Pairs {
if s.Key == key {
return s.Value
}
}
return ""
}
func GetPropertyFile() *Propertie {
return propertie
}
func LoadProperty() (*Propertie, error) {
if propertie == nil {
var p Propertie
file, err := os.Open(path)
if err != nil {
return nil, errors.New("Not able to open Properties")
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
s := strings.Split(scanner.Text(), ": ")
p.Pairs = append(p.Pairs, PropertiePair{Key: s[0], Value: s[1]})
}
propertie = &p
if err := scanner.Err(); err != nil {
return nil, errors.New("Propertie file not well formed")
}
}
return propertie, nil
}