-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
funcs.go
135 lines (124 loc) · 2.7 KB
/
funcs.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
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
package immortal
import (
"bytes"
"crypto/md5"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"os/user"
"path/filepath"
"time"
)
// GetJSON unix socket web client
func GetJSON(spath, path string, target interface{}) error {
// http socket client
tr := &http.Transport{
Dial: func(proto, addr string) (net.Conn, error) {
return net.Dial("unix", spath)
},
}
client := &http.Client{Transport: tr}
r, err := client.Get(fmt.Sprintf("http://socket/%s", path))
if err != nil {
return err
}
defer r.Body.Close()
return json.NewDecoder(r.Body).Decode(target)
}
// AbsSince return time since in format [days]d[hours]h[minutes]m[seconds.decisecond]s
func AbsSince(t time.Time) string {
const (
Decisecond = 100 * time.Millisecond
Day = 24 * time.Hour
)
ts := time.Since(t) + Decisecond/2
d := ts / Day
ts = ts % Day
h := ts / time.Hour
ts = ts % time.Hour
m := ts / time.Minute
ts = ts % time.Minute
s := ts / time.Second
ts = ts % time.Second
f := ts / Decisecond
var buffer bytes.Buffer
if d > 0 {
buffer.WriteString(fmt.Sprintf("%dd", d))
}
if h > 0 {
buffer.WriteString(fmt.Sprintf("%dh", h))
}
if m > 0 {
buffer.WriteString(fmt.Sprintf("%dm", m))
}
buffer.WriteString(fmt.Sprintf("%d.%ds", s, f))
return buffer.String()
}
// md5sum return md5 checksum of given file
func md5sum(filePath string) (string, error) {
f, err := os.Open(filePath)
if err != nil {
return "", err
}
defer f.Close()
h := md5.New()
if _, err := io.Copy(h, f); err != nil {
return "", err
}
return fmt.Sprintf("%x", h.Sum(nil)), nil
}
// inSlice find if item is in slice
func inSlice(s []string, item string) bool {
for _, i := range s {
if i == item {
return true
}
}
return false
}
// GetSdir return the main supervise directory, defaults to /var/run/immortal
func GetSdir() string {
// if IMMORTAL_SDIR env is set, use it as default sdir
sdir := os.Getenv("IMMORTAL_SDIR")
if sdir == "" {
sdir = "/var/run/immortal"
}
return sdir
}
// GetUserSdir returns the $HOME/.immortal
func GetUserSdir() (string, error) {
home := os.Getenv("HOME")
if home == "" {
usr, err := user.Current()
if err != nil {
return "", fmt.Errorf("error getting user home: %s", err)
}
home = usr.HomeDir
}
return filepath.Join(home, ".immortal"), nil
}
// isDir return true if path is a dir
func isDir(path string) bool {
f, err := os.Stat(path)
if err != nil {
return false
}
if m := f.Mode(); m.IsDir() && m&400 != 0 {
return true
}
return false
}
// isFile return true if path is a regular file
func isFile(path string) bool {
f, err := os.Stat(path)
if err != nil {
return false
}
if m := f.Mode(); !m.IsDir() && m.IsRegular() && m&400 != 0 {
return true
}
return false
}