-
Notifications
You must be signed in to change notification settings - Fork 55
/
utils.go
80 lines (67 loc) · 1.51 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
package unleash
import (
"fmt"
"math/rand"
"os"
"os/user"
"reflect"
"sync"
"time"
)
func getTmpDirPath() string {
return os.TempDir()
}
func generateInstanceId() string {
prefix := ""
if user, err := user.Current(); err == nil && user.Username != "" {
prefix = user.Username
} else {
rand.Seed(time.Now().Unix())
prefix = fmt.Sprintf("generated-%d-%d", rand.Intn(1000000), os.Getpid())
}
if hostname, err := os.Hostname(); err == nil && hostname != "" {
return fmt.Sprintf("%s-%s", prefix, hostname)
}
return prefix
}
func getFetchURLPath(projectName string) string {
if projectName != "" {
return fmt.Sprintf("./client/features?project=%s", projectName)
}
return "./client/features"
}
func contains(arr []string, str string) bool {
for _, item := range arr {
if item == str {
return true
}
}
return false
}
// WarnOnce is a type for handling warnings that should only be displayed once.
type WarnOnce struct {
once sync.Once
}
// Warn logs the warning message once.
func (wo *WarnOnce) Warn(message string) {
wo.once.Do(func() {
fmt.Println("Warning:", message)
})
}
func every(slice interface{}, condition func(interface{}) bool) bool {
sliceValue := reflect.ValueOf(slice)
if sliceValue.Kind() != reflect.Slice {
fmt.Println("Input is not a slice returning false")
return false
}
if sliceValue.Len() == 0 {
return false
}
for i := 0; i < sliceValue.Len(); i++ {
element := sliceValue.Index(i).Interface()
if !condition(element) {
return false
}
}
return true
}