-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
67 lines (62 loc) · 1.38 KB
/
utils.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
package main
import (
"strconv"
"time"
"github.com/larspensjo/config"
)
func GenerateId() string {
return strconv.FormatInt(time.Now().UnixNano(), 10)
}
func float2Int(input interface{}) interface{} {
if m, ok := input.([]interface{}); ok {
for k, v := range m {
switch v.(type) {
case float64:
m[k] = int(v.(float64))
case []interface{}:
m[k] = float2Int(m[k])
case map[string]interface{}:
m[k] = float2Int(m[k])
}
}
} else if m, ok := input.(map[string]interface{}); ok {
for k, v := range m {
switch v.(type) {
case float64:
m[k] = int(v.(float64))
case []interface{}:
m[k] = float2Int(m[k])
case map[string]interface{}:
m[k] = float2Int(m[k])
}
}
} else {
return false
}
return input
}
func getConfig(sec string) (map[string]string, error) {
targetConfig := make(map[string]string)
cfg, err := config.ReadDefault("config.ini")
if err != nil {
return targetConfig, Error("unable to open config file or wrong fomart")
}
sections := cfg.Sections()
if len(sections) == 0 {
return targetConfig, Error("no " + sec + " config")
}
for _, section := range sections {
if section != sec {
continue
}
sectionData, _ := cfg.SectionOptions(section)
for _, key := range sectionData {
value, err := cfg.String(section, key)
if err == nil {
targetConfig[key] = value
}
}
break
}
return targetConfig, nil
}