/
util.go
110 lines (97 loc) · 1.91 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
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
package u
import (
"crypto/sha1"
"fmt"
"io"
"os"
"os/user"
"runtime"
"strings"
"time"
)
func PanicIf(cond bool, args ...interface{}) {
if !cond {
return
}
msg := "invalid state"
if len(args) > 0 {
s, ok := args[0].(string)
if ok {
msg = s
if len(s) > 1 {
msg = fmt.Sprintf(msg, args[1:]...)
}
}
}
panic(msg)
}
func PanicIfErr(err error) {
if err != nil {
panic(err.Error())
}
}
func IsLinux() bool {
return runtime.GOOS == "linux"
}
func IsMac() bool {
return runtime.GOOS == "darwin"
}
func UserHomeDir() string {
// user.Current() returns nil if cross-compiled e.g. on mac for linux
if usr, _ := user.Current(); usr != nil {
return usr.HomeDir
}
return os.Getenv("HOME")
}
func ExpandTildeInPath(s string) string {
if strings.HasPrefix(s, "~") {
return UserHomeDir() + s[1:]
}
return s
}
func Sha1OfFile(path string) ([]byte, error) {
f, err := os.Open(path)
if err != nil {
//fmt.Printf("os.Open(%s) failed with %s\n", path, err.Error())
return nil, err
}
defer f.Close()
h := sha1.New()
_, err = io.Copy(h, f)
if err != nil {
//fmt.Printf("io.Copy() failed with %s\n", err.Error())
return nil, err
}
return h.Sum(nil), nil
}
func Sha1HexOfFile(path string) (string, error) {
sha1, err := Sha1OfFile(path)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", sha1), nil
}
func Sha1HexOfBytes(data []byte) string {
return fmt.Sprintf("%x", Sha1OfBytes(data))
}
func Sha1OfBytes(data []byte) []byte {
h := sha1.New()
h.Write(data)
return h.Sum(nil)
}
func DurationToString(d time.Duration) string {
minutes := int(d.Minutes()) % 60
hours := int(d.Hours())
days := hours / 24
hours = hours % 24
if days > 0 {
return fmt.Sprintf("%dd %dhr", days, hours)
}
if hours > 0 {
return fmt.Sprintf("%dhr %dm", hours, minutes)
}
return fmt.Sprintf("%dm", minutes)
}
func TimeSinceNowAsString(t time.Time) string {
return DurationToString(time.Now().Sub(t))
}