-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
84 lines (74 loc) · 1.72 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
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"sync"
)
const (
CONFIG_FILE = "config.json"
KB_INDEX = "Index"
KB_PAGE = "Page"
)
type NotebookConfig struct {
Environment map[string]string `json:"environment"`
filename string
mutex sync.Mutex
}
func getValue(kmap map[string]string, action, defkey string) string {
if kmap == nil {
return defkey
}
if value, ok := kmap[action]; ok && len(value) > 0 {
return value
}
return defkey
}
func NewNotebookConfig(folder string) *NotebookConfig {
var config = &NotebookConfig{}
config.filename = path.Join(folder, CONFIG_FILE)
bytes, err := ioutil.ReadFile(config.filename)
if err == nil {
json.Unmarshal(bytes, config)
}
if config.Environment == nil {
config.Environment = map[string]string{}
}
if value, ok := config.Environment["RIZIN_PATH"]; !ok || len(value) < 1 {
config.Environment["RIZIN_PATH"] = os.Getenv("RIZIN_PATH")
}
return config
}
func (nc *NotebookConfig) UpdateEnvironment() {
nc.mutex.Lock()
defer nc.mutex.Unlock()
for key, value := range nc.Environment {
os.Setenv(key, value)
}
}
func (nc *NotebookConfig) DelEnvironment(key string) {
nc.mutex.Lock()
defer nc.mutex.Unlock()
key = strings.TrimSpace(key)
delete(nc.Environment, key)
os.Unsetenv(key)
}
func (nc *NotebookConfig) SetEnvironment(key, value string) {
nc.mutex.Lock()
defer nc.mutex.Unlock()
value = strings.TrimSpace(value)
key = strings.TrimSpace(key)
os.Setenv(key, value)
nc.Environment[key] = value
}
func (nc *NotebookConfig) Save() {
nc.mutex.Lock()
defer nc.mutex.Unlock()
bytes, _ := json.MarshalIndent(nc, "", "\t")
if err := ioutil.WriteFile(nc.filename, bytes, 0644); err != nil {
fmt.Println(err)
}
}