-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.go
More file actions
127 lines (119 loc) · 2.64 KB
/
utils.go
File metadata and controls
127 lines (119 loc) · 2.64 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package gwork
import (
"encoding/json"
"fmt"
"github.com/larspensjo/config"
"strconv"
"strings"
"time"
)
func GenerateUnixNanoId() string {
return strconv.FormatInt(time.Now().UnixNano(), 10)
}
func JsonEncode(nodes interface{}) string {
body, err := json.Marshal(nodes)
if err != nil {
panic(err.Error())
}
return string(body)
}
func JsonDecode(jsonStr string) interface{} {
jsonStr = strings.Replace(jsonStr, "\n", "", -1)
var f interface{}
err := json.Unmarshal([]byte(jsonStr), &f)
if err != nil {
panic(err)
}
return f
// return float2Int(f)
}
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(configFile string, sec string) (map[string]string, error) {
targetConfig := make(map[string]string)
cfg, err := config.ReadDefault(configFile)
if err != nil {
fmt.Println(err)
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
}
func UptimeFormat(secs uint32, section int) string {
timeUnits := map[uint32][2]string{
1: [2]string{"second", "seconds"},
60: [2]string{"minute", "minutes"},
3600: [2]string{"hour", "hours"},
86400: [2]string{"day", "days"},
}
timeSeq := [4]uint32{86400, 3600, 60, 1}
timeSeqLen := len(timeSeq)
result := make([]string, timeSeqLen)
if section < 1 {
section = 1
} else if section > timeSeqLen {
section = timeSeqLen
}
i := 0
for _, index := range timeSeq {
if v, prs := timeUnits[index]; prs {
if secs >= index {
num := secs / index
secs = secs % index
unit := v[0]
if num > 1 {
unit = v[1]
}
result[i] = fmt.Sprintf("%d %s", num, unit)
i++
}
}
}
sliceLen := i
if sliceLen > section {
sliceLen = section
}
return strings.Join(result[0:sliceLen], ", ")
}