-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
62 lines (56 loc) · 1.14 KB
/
util.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
package storage
import (
"crypto/rand"
"encoding/hex"
"fmt"
"os"
"path"
"strings"
)
func GenUUID() (string, error) {
data := [16]byte{}
n, err := rand.Read(data[:])
if err != nil {
return "", err
}
if n != len(data) {
return "", fmt.Errorf("generate random numbers failed")
}
sb := strings.Builder{}
sb.Grow(36)
sb.WriteString(hex.EncodeToString(data[:4]))
sb.WriteByte('-')
sb.WriteString(hex.EncodeToString(data[4:6]))
sb.WriteByte('-')
sb.WriteString(hex.EncodeToString(data[6:8]))
sb.WriteByte('-')
sb.WriteString(hex.EncodeToString(data[8:10]))
sb.WriteByte('-')
sb.WriteString(hex.EncodeToString(data[10:]))
return sb.String(), nil
}
func IsFile(filename string) bool {
stat, err := os.Stat(filename)
if err != nil {
return false
}
return stat.Mode().IsRegular()
}
func IsDir(filename string) bool {
stat, err := os.Stat(filename)
if err != nil {
return false
}
return stat.Mode().IsDir()
}
func ToAbs(p string) (string, error) {
if !path.IsAbs(p) && !(len(p) > 1 && p[1] == ':') {
cwd, err := os.Getwd()
if err != nil {
return "", err
}
p = path.Join(cwd, p)
}
p = path.Clean(p)
return p, nil
}