-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutil.go
More file actions
140 lines (121 loc) · 2.71 KB
/
Copy pathutil.go
File metadata and controls
140 lines (121 loc) · 2.71 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
140
package tc
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"os"
"strconv"
"strings"
"unicode"
)
// TrimSpace 删除头尾的空字符,空格,换行之类的东西, 具体在unicode.IsSpac.
func TrimSpace(raw string) string {
s := strings.TrimLeftFunc(raw, unicode.IsSpace)
if s != "" {
s = strings.TrimRightFunc(s, unicode.IsSpace)
}
return s
}
// TrimSplit 按sep拆分,并去掉空字符.
func TrimSplit(raw, sep string) []string {
var ss []string
s := TrimSpace(raw)
if s == "" {
return ss
}
ss = strings.Split(s, sep)
i := 0
for _, s := range ss {
s = TrimSpace(s)
if s != "" {
ss[i] = s
i++
}
}
return ss[:i]
}
// TruncateID returns a shorthand version of a string identifier for convenience.
// A collision with other shorthands is very unlikely, but possible.
// In case of a collision a lookup with TruncIndex.Get() will fail, and the caller
// will need to use a langer prefix, or the full-length Id.
func truncateID(id string) string {
shortLen := 12
if len(id) < shortLen {
shortLen = len(id)
}
return id[:shortLen]
}
// GenUID - returns an unique id
func GenUID() string {
for {
id := make([]byte, 16)
if _, err := io.ReadFull(rand.Reader, id); err != nil {
// This shouldn't happen
panic(err)
}
value := hex.EncodeToString(id)
// if we try to parse the truncated for as an int and we don't have
// an error then the value is all numberic and causes issues when
// used as a hostname. ref #3869
if _, err := strconv.ParseInt(truncateID(value), 10, 32); err == nil {
continue
}
return value
}
}
// JoinMap
func JoinMap(params map[string]interface{}, sep string) []byte {
if params == nil {
return nil
}
var v string
for key, value := range params {
v += fmt.Sprintf("%v=%v%v", key, value, sep)
}
v = strings.TrimRight(v, sep)
return []byte(v)
}
// EncodeJSON
func EncodeJSON(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
// DecodeJSON
func DecodeJSON(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
// InterfaceToJson
// marshal data to json data
// if success return string with json format
// if not return empty string
func InterfaceToJson(data interface{}) string {
enc, err := json.Marshal(data)
if err != nil {
return ""
}
return string(enc)
}
// Exists - check whether the file or path exist
func Exists(path string) bool {
_, err := os.Stat(path)
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}
// IsDir - check whether the path is dir
func IsDir(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.IsDir()
}
// IsFile - check whether the path is file
func IsFile(path string) bool {
return !IsDir(path)
}