-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
86 lines (78 loc) · 1.79 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
package letswatch
import (
"fmt"
"os"
"github.com/gobwas/glob"
"github.com/rs/zerolog/log"
)
// ValidateEnv takes a list of strings, and returns an error if they do not
// exist as ENV vars. Also returns a map of the values that are valid
func ValidateEnv(envs ...string) (map[string]string, error) {
var missingEnvs []string
ret := map[string]string{}
for _, item := range envs {
e := os.Getenv(item)
if e == "" {
missingEnvs = append(missingEnvs, item)
} else {
ret[item] = e
}
}
if len(missingEnvs) > 0 {
return ret, fmt.Errorf("Missing the following env vars: %v", missingEnvs)
}
return ret, nil
}
func inBetween(i, min, max int) bool {
if (i >= min) && (i <= max) {
return true
} else {
return false
}
}
// MatchesGlobOf returns true if an item matches any of the given globs
func MatchesGlobOf(item string, globs []string) bool {
for _, matchGlob := range globs {
g := glob.MustCompile(matchGlob)
got := g.Match(item)
if got {
return true
}
}
log.Debug().Str("title", item).Msg("Skipping because it matches no globs")
return false
}
func ContainsString(ss []string, s string) bool {
for _, item := range ss {
if s == item {
return true
}
}
return false
}
// Remove dups from slice.
func removeDups(elements []string) (nodups []string) {
encountered := make(map[string]bool)
for _, element := range elements {
if !encountered[element] {
nodups = append(nodups, element)
encountered[element] = true
}
}
return
}
func Intersection(s1, s2 []string) (inter []string) {
hash := make(map[string]bool)
for _, e := range s1 {
hash[e] = true
}
for _, e := range s2 {
// If elements present in the hashmap then append intersection list.
if hash[e] {
inter = append(inter, e)
}
}
// Remove dups from slice.
inter = removeDups(inter)
return
}