-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.go
78 lines (66 loc) · 1.56 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package util
import (
"crypto/rand"
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"sort"
"time"
)
func SHA1(in string) string {
h := sha1.New()
io.WriteString(h, in)
return hex.EncodeToString(h.Sum(nil))
}
// based on suggestions from,
// https://groups.google.com/d/msg/golang-nuts/d0nF_k4dSx4/rPGgfXv6QCoJ
func UUID() (uuid string, err error) {
b := make([]byte, 16)
_, err = io.ReadFull(rand.Reader, b)
if err != nil {
return
}
uuid = fmt.Sprintf(
"%x-%x-%x-%x-%x",
b[0:4], b[4:6], b[6:8], b[8:10], b[10:],
)
return
}
func Bold(str string) string {
return fmt.Sprintf("\033[1m%s\033[0m", str)
}
func Color(color, str string) string {
return fmt.Sprintf("\033[%sm%s\033[0m", color, str)
}
func TimestampToTime(timestamp *int64) (*time.Time, error) {
if timestamp != nil {
t := time.Unix(*timestamp, 0)
return &t, nil
}
return &time.Time{}, errors.New("Not valid time")
}
type byModTime []os.FileInfo
func (f byModTime) Len() int { return len(f) }
func (f byModTime) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
func (f byModTime) Less(i, j int) bool {
return f[i].ModTime().UnixNano() > f[j].ModTime().UnixNano()
}
// ReadDir reads the directory named by dirname and returns
// a list of sorted(by ModTime) directory entries.
// http://localhost:6060/src/pkg/io/ioutil/ioutil.go?s=3159:3210#L87
func ReadDir(dirname string) ([]os.FileInfo, error) {
f, err := os.Open(dirname)
if err != nil {
return nil, err
}
list, err := f.Readdir(-1)
f.Close()
if err != nil {
return nil, err
}
sort.Sort(byModTime(list))
return list, nil
}