-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
60 lines (50 loc) · 1.4 KB
/
storage.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
package sider
import (
"encoding/json"
"fmt"
"io/ioutil"
)
var path = "/tmp"
func SaveFile(file string, content interface{}) bool {
parse, err := json.Marshal(content)
if err != nil {
return false
} else {
ioutil.WriteFile(file, parse, 0644)
return true
}
}
func ImportData(customPath ...string) bool {
if len(customPath) > 0 {
if len(customPath[0]) > 0 {
path = customPath[0]
}
}
listsContent, errLists := ioutil.ReadFile(fmt.Sprintf("%s/lists.json", path))
keysContent, errKeys := ioutil.ReadFile(fmt.Sprintf("%s/keys.json", path))
countersContent, errCounters := ioutil.ReadFile(fmt.Sprintf("%s/counters.json", path))
if errLists != nil || errKeys != nil || errCounters != nil{
return false
}
json_lists := make(map[string][]string)
json_keys := make(map[string]string)
json_counters := make(map[string]int64)
json.Unmarshal(listsContent, &json_lists)
json.Unmarshal(keysContent, &json_keys)
json.Unmarshal(countersContent, &json_counters)
lists = json_lists
keys = json_keys
counters = json_counters
return true
}
func SaveData(customPath ...string) { // execute as goroutine
if len(customPath) > 0 {
if len(customPath[0]) > 0 {
path = customPath[0]
}
}
fmt.Printf("Store data at folder '%s'\n", path)
SaveFile(fmt.Sprintf("%s/lists.json", path), lists)
SaveFile(fmt.Sprintf("%s/keys.json", path), keys)
SaveFile(fmt.Sprintf("%s/counters.json", path), counters)
}