-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
163 lines (144 loc) · 3.08 KB
/
utils.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package pantry
import (
"errors"
"os"
"path/filepath"
"runtime"
"strings"
"time"
)
func stringInSlice(s string, slice []string) bool {
for _, item := range slice {
if item == s {
return true
}
}
return false
}
// ext returns the file name extension used by path.
// The extension is the suffix beginning after the final dot
// in the final slash-separated element of path;
// it is empty if there is no dot.
func ext(path string) string {
for i := len(path) - 1; i >= 0 && path[i] != '/' && !os.IsPathSeparator(path[i]); i-- {
if path[i] == '.' {
if (i + 1) < len(path) {
return path[i+1:]
}
}
}
return ""
}
func pathExists(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}
func getHomeDir() (string, error) {
var homeDir string
switch runtime.GOOS {
case "windows":
homeDir = os.Getenv("USERPROFILE")
default:
homeDir = os.Getenv("HOME")
}
if homeDir == "" {
return "", errors.New("No home directory found - set $HOME (or the platform equivalent).")
}
return homeDir, nil
}
func getHomeSubDir(subDirs ...string) (dir string, err error) {
if dir, err = getHomeDir(); err != nil {
return "", err
}
dir = filepath.Join(dir, filepath.Join(subDirs...))
return
}
func getConfigDir() (string, error) {
switch runtime.GOOS {
case "windows":
return os.Getenv("LOCALAPPDATA"), nil
case "darwin":
return getHomeSubDir("Library", "Application Support")
default:
return getHomeSubDir(".config")
}
}
func getAppConfigDir(appName string) (dir string, err error) {
if dir, err = getConfigDir(); err != nil {
return "", err
}
dir = filepath.Join(dir, appName)
return
}
func getAppDir() (string, error) {
return filepath.Abs(filepath.Dir(os.Args[0]))
}
func splitTo(s, sep string, parts ...*string) {
ss := strings.Split(s, sep)
for i := 0; i < len(parts) && i < len(ss); i++ {
*(parts[i]) = ss[i]
}
for i := len(ss); i < len(parts); i++ {
*(parts[i]) = ""
}
}
func splitNTo(s, sep string, parts ...*string) {
ss := strings.SplitN(s, sep, len(parts))
for i := 0; i < len(parts) && i < len(ss); i++ {
*(parts[i]) = ss[i]
}
for i := len(ss); i < len(parts); i++ {
*(parts[i]) = ""
}
}
func fieldsTo(s string, parts ...*string) {
ss := strings.Fields(s)
for i := 0; i < len(parts) && i < len(ss); i++ {
*(parts[i]) = ss[i]
}
for i := len(ss); i < len(parts); i++ {
*(parts[i]) = ""
}
}
// accumulate signals
func unjitter(in <-chan bool, d time.Duration) <-chan bool {
out := make(chan bool)
go func() {
defer close(out)
timer := time.NewTimer(time.Hour * 100000)
defer timer.Stop()
for {
changed := false
resetTimer := false
select {
case <-timer.C:
changed = false
out <- true
case _, ok := <-in:
if ok {
if !changed {
changed = true
resetTimer = true
}
} else {
return
}
}
if resetTimer {
timer.Stop()
changed = true
timer.Reset(time.Second)
resetTimer = false
}
}
}()
return out
}
func unjitterFunc(in <-chan bool, d time.Duration, f func()) {
out := unjitter(in, d)
go func() {
for _ = range out {
f()
}
}()
}