blob: 1f1736f17279bb010cacce41a19768f0b3d9dd70 (
plain)
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
|
package config
import (
"os"
"path/filepath"
"strings"
"sync"
)
// an experiment to see if this is useful
var saveMu sync.RWMutex
// returns true if config is working okay
func InitValid() bool {
saveMu.Lock()
defer saveMu.Unlock()
if configPB == nil {
// todo: try to re-init it here
return false
}
return true
}
// saves your applications config file
func Save() error {
saveMu.Lock()
defer saveMu.Unlock()
basedir, _ := filepath.Split(configPB.Filename)
if err := os.MkdirAll(basedir, os.ModePerm); err != nil {
return err
}
err := SavePB(configPB)
return err
}
func Get(flag string) string {
saveMu.Lock()
defer saveMu.Unlock()
if configPB == nil {
return ""
}
c := findByLower(flag)
if c == nil {
return ""
}
return c.Value
}
func findByLower(lookingFor string) *Config {
for c := range configPB.IterAll() {
if strings.ToLower(c.Key) == strings.ToLower(lookingFor) {
return c
}
}
return nil
}
func True(flag string) bool {
saveMu.Lock()
defer saveMu.Unlock()
if configPB == nil {
return false
}
found := configPB.FindByKey(flag)
if found == nil {
return false
}
if strings.ToLower(found.Value) == "true" {
return true
}
return false
}
func Set(key string, newValue string) error {
saveMu.Lock()
defer saveMu.Unlock()
if configPB == nil {
return NotInitialized
}
found := configPB.FindByKey(key)
if found != nil {
found.Value = newValue
}
newvar := new(Config)
newvar.Key = key
newvar.Value = newValue
configPB.Append(newvar)
return nil
}
|