/
common.go
237 lines (188 loc) · 3.63 KB
/
common.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package common
import (
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"unicode"
)
var (
onceShutdownHooks sync.Once
onceTitle sync.Once
shutdownHooks []func()
title string
)
const (
MaxUint = ^uint(0)
MinUint = 0
MaxInt = int(MaxUint >> 1)
MinInt = -MaxInt - 1
)
type MultiValueFlag []string
func (this *MultiValueFlag) String() string {
if this == nil {
return ""
}
return strings.Join(*this, ",")
}
func (this *MultiValueFlag) Set(value string) error {
splits := strings.Split(value, ",")
*this = append(*this, splits...)
return nil
}
func init() {
shutdownHooks = make([]func(), 0)
}
type ChannelError struct {
m sync.Mutex
l []error
}
func (c *ChannelError) Add(err error) {
c.m.Lock()
c.l = append(c.l, err)
c.m.Unlock()
}
func (c *ChannelError) Get() error {
c.m.Lock()
defer c.m.Unlock()
if len(c.l) > 0 {
return c.l[0]
} else {
return nil
}
}
func (c *ChannelError) GetAll() []error {
return c.l
}
func (c *ChannelError) Exists() bool {
c.m.Lock()
defer c.m.Unlock()
return len(c.l) > 0
}
// Exit exist app and run all registered shutdown hooks
func Done() {
onceShutdownHooks.Do(func() {
for _, f := range shutdownHooks {
f()
}
})
closeLogFile()
}
func AddShutdownHook(f func()) {
shutdownHooks = append(shutdownHooks, nil)
copy(shutdownHooks[1:], shutdownHooks[0:])
shutdownHooks[0] = f
}
func AppFilename(newExt string) string {
filename := Title()
ext := filepath.Ext(filename)
if len(ext) > 0 {
filename = string(filename[:len(filename)-len(ext)])
}
return filename + newExt
}
func Title() string {
onceTitle.Do(func() {
path, err := os.Executable()
if err != nil {
path = os.Args[0]
}
path = filepath.Base(path)
path = path[0:(len(path) - len(filepath.Ext(path)))]
runes := []rune(path)
for len(runes) > 0 && !unicode.IsLetter(runes[0]) {
runes = runes[1:]
}
title = string(runes)
})
DebugFunc(title)
return title
}
func Version(major bool, minor bool, patch bool) string {
if strings.Count(app.Version, ".") == 2 {
s := strings.Split(app.Version, ".")
sb := strings.Builder{}
if major {
sb.WriteString(s[0])
}
if minor {
if sb.Len() > 0 {
sb.WriteString(".")
}
sb.WriteString(s[1])
}
if patch {
if sb.Len() > 0 {
sb.WriteString(".")
}
sb.WriteString(s[2])
}
return sb.String()
}
return ""
}
func TitleVersion(major bool, minor bool, patch bool) string {
return Title() + "-" + Version(major, minor, patch)
}
// IsWindowsOS reports true if underlying OS is MS Windows
func IsWindowsOS() bool {
return runtime.GOOS == "windows"
}
// IsLinuxOS reports true if underlying OS is Linux
func IsLinuxOS() bool {
return runtime.GOOS == "linux"
}
// IsMacOS reports true if underlying OS is MacOS
func IsMacOS() bool {
return runtime.GOOS == "darwin"
}
// ToBool reports if value indicates "true"
func ToBool(s string) bool {
if len(s) == 0 {
return false
}
s = strings.ToLower(s)
return s == "true" || s == "1" || strings.HasPrefix(s, "t") || strings.HasPrefix(s, "y") || strings.HasPrefix(s, "j")
}
func Eval(b bool, trueFunc interface{}, falseFunc interface{}) interface{} {
if b {
if f, ok := trueFunc.(func() interface{}); ok {
return f()
} else {
return trueFunc
}
} else {
if f, ok := falseFunc.(func() interface{}); ok {
return f()
} else {
return falseFunc
}
}
}
func Min(v ...int) int {
var r int
for i, vi := range v {
if i == 0 {
r = vi
} else {
if vi < r {
r = vi
}
}
}
return r
}
func Max(v ...int) int {
var r int
for i, vi := range v {
if i == 0 {
r = vi
} else {
if vi > r {
r = vi
}
}
}
return r
}