-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
68 lines (61 loc) · 1.68 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
package fangs
import (
"os"
"reflect"
"regexp"
"runtime"
"strings"
)
// Flatten takes multiple entries and creates a "flattened" list by further splitting
// comma-separated entries and removing empty entries
func Flatten(commaSeparatedEntries ...string) []string {
var out []string
for _, v := range commaSeparatedEntries {
for _, s := range strings.Split(v, ",") {
s = strings.TrimSpace(s)
if s == "" {
continue
}
out = append(out, s)
}
}
return out
}
func contains(parts []string, value string) bool {
for _, v := range parts {
if v == value {
return true
}
}
return false
}
var envVarRegex = regexp.MustCompile("[^a-zA-Z0-9_]")
func envVar(appName string, parts ...string) string {
v := strings.Join(parts, "_")
if appName != "" {
v = appName + "_" + v
}
v = envVarRegex.ReplaceAllString(v, "_")
return strings.ToUpper(v)
}
func fileExists(name string) bool {
fi, err := os.Stat(name)
return err == nil && !fi.IsDir()
}
// isPromotedMethod returns true if the method with the given name is promoted from
// an embedded struct.
// NOTE: this is currently _not_ a completely reliable method to identify this information,
// as there is no way using standard go or reflection to identify this. the method currently
// uses some undefined behavior of the go runtime that may change or may be unreliable when
// used by structs created with reflection or if debug information is not present
func isPromotedMethod(o any, method string) bool {
v := reflect.ValueOf(o)
t := v.Type()
m, ok := t.MethodByName(method)
if !ok {
return false
}
f := runtime.FuncForPC(m.Func.Pointer())
fileName, _ := f.FileLine(f.Entry())
return fileName == "<autogenerated>"
}