-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.go
106 lines (93 loc) · 2.57 KB
/
lib.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
package props
import (
"os"
"regexp"
"strings"
)
type propsStruct struct {
props map[string]string
args []string
}
var (
globalProps *propsStruct
propEqRegex = regexp.MustCompile(`^--([\w\-\.]+)=(.*)$`)
propRegex = regexp.MustCompile(`^--([\w\-\.]+)`)
propFlagRegex = regexp.MustCompile(`^-(\w+)`)
)
func init() {
globalProps = parseArgs(os.Args[1:])
}
// GetProp returns the value of a property, if not set it returns
// an empty string
func GetProp(name string) string {
if val, found := globalProps.props[toPropName(name)]; found {
return val
}
if val, found := os.LookupEnv(toEnvName(name)); found {
return val
}
return ""
}
func toPropName(name string) string {
propName := strings.ToLower(name)
propName = strings.Replace(propName, "_", ".", -1)
propName = strings.Replace(propName, "-", ".", -1)
return propName
}
func toEnvName(name string) string {
envName := strings.ToUpper(name)
envName = strings.Replace(envName, ".", "_", -1)
envName = strings.Replace(envName, "-", "_", -1)
return envName
}
// IsSet returns true if property is set, otherwise it returns false
func IsSet(name string) bool {
_, found := globalProps.props[toPropName(name)]
return found
}
// GetArgs regurns any argument that is not a property name, flag or property value
func GetArgs() []string {
return globalProps.args
}
func parseArgs(args []string) *propsStruct {
res := &propsStruct{
props: make(map[string]string),
args: make([]string, 0),
}
numArgs := len(args)
for i := 0; i < numArgs; i++ {
arg := args[i]
//for _, arg := range args {
if arr := propEqRegex.FindStringSubmatch(arg); arr != nil {
// prop: --some-prop=xyz
res.props[toPropName(arr[1])] = arr[2]
} else if arr := propRegex.FindStringSubmatch(arg); arr != nil {
// prop: --some-prop xyz
if i < (numArgs - 1) {
nextArg := args[i+1]
if !propEqRegex.MatchString(nextArg) && !propRegex.MatchString(nextArg) {
// next argument is not a property/flag --prop-name
res.props[toPropName(arr[1])] = args[i+1]
i++
} else {
// if next argument is a property/flag, then set to empty
res.props[toPropName(arr[1])] = ""
}
} else {
// property is the last parameter, set to empty
res.props[toPropName(arr[1])] = ""
}
} else if arr := propFlagRegex.FindStringSubmatch(arg); arr != nil {
// flag -p, -a -x
flagsArr := []rune(arr[1]) // -abc : -a -b -c
for i := 0; i < len(flagsArr); i++ {
letter := string(flagsArr[i])
res.props[toPropName(letter)] = ""
}
} else {
// arg
res.args = append(res.args, arg)
}
}
return res
}