blob: c5aaeb7ed7822af922c562c88ef9ea9d110e1381 (
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
|
package config
import (
"os"
"path/filepath"
"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 ""
}
found := configPB.FindByKey(flag)
if found == nil {
return ""
}
return found.Value
}
func GetPanic(flag string) string {
saveMu.Lock()
defer saveMu.Unlock()
if configPB == nil {
configPanic(flag)
}
found := configPB.FindByKey(flag)
if found == nil {
configPanic(flag)
}
return found.Value
}
func configPanic(varname string) {
saveMu.Lock()
defer saveMu.Unlock()
if configPB == nil {
panic("config file is nil")
}
panic("config name '" + varname + "' not found")
}
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
}
|