-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
139 lines (121 loc) · 2.62 KB
/
util.go
File metadata and controls
139 lines (121 loc) · 2.62 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
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"encoding/base64"
"os/exec"
"runtime"
"strings"
)
func isTrue(str string) bool {
yesMap := map[string]struct{}{
"yes": struct{}{},
"y": struct{}{},
"true": struct{}{},
"1": struct{}{},
}
_, ok := yesMap[strings.ToLower(str)]
return ok
}
// changeFile creates a file or makes changes to an existing file.
func changeFile(path string, logCh chan string, fn func(lMapping) error) error {
newFile, err := createFile(path, logCh)
if err != nil {
return err
}
mapping := make(lMapping)
if !newFile {
err = readEncryptedFile(path, &mapping, logCh)
if err != nil {
err = deleteFile(path, logCh)
if err != nil {
return err
}
_, err := createFile(path, logCh)
if err != nil {
return err
}
}
}
err = fn(mapping)
if err != nil {
return err
}
err = writeEncryptedFile(path, mapping, logCh)
if err != nil {
return err
}
return nil
}
// convertFloatToInt converts float value to integer if possible (only interfaces will be converted).
func convertFloatToInt(arg interface{}) interface{} {
switch v := arg.(type) {
case []interface{}:
arr := make([]interface{}, 0)
for i := range v {
r := convertFloatToInt(v[i])
arr = append(arr, r)
}
return arr
case map[string]interface{}:
dict := make(map[string]interface{})
for k := range v {
r := convertFloatToInt(v[k])
dict[k] = r
}
return dict
case float64:
i := int(v)
f := float64(i)
if v == f {
return i
}
return v
}
return arg
}
// decodeBase64 decodes base64 strings
func decodeBase64(arg interface{}) (interface{}, error) {
switch v := arg.(type) {
case []interface{}:
arr := make([]interface{}, 0)
for i := range v {
r, err := decodeBase64(v[i])
if err != nil {
return nil, err
}
arr = append(arr, r)
}
return arr, nil
case map[string]interface{}:
dict := make(map[string]interface{})
for k := range v {
r, err := decodeBase64(v[k])
if err != nil {
return nil, err
}
dict[k] = r
}
return dict, nil
case string:
return base64.StdEncoding.DecodeString(v)
}
return arg, nil
}
func open(url string) error { //https://stackoverflow.com/questions/39320371/how-start-web-server-to-open-page-in-browser-in-golang
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
case "darwin":
cmd = "open"
default: // "linux", "freebsd", "openbsd", "netbsd"
cmd = "xdg-open"
}
args = append(args, url)
return exec.Command(cmd, args...).Run()
}
func internalError(err error) (int, interface{}, message) {
message := failedMsg(err)
return message.Status, nil, message
}