Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ jobs:
- stage: unit-test
script: make unit-test
- stage: deploy
script: make deploy
if: branch = master
script: make deploy

env:
global:
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1
1.2
25 changes: 16 additions & 9 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type propsStruct struct {

var (
globalProps *propsStruct
propEqRegex = regexp.MustCompile(`^--([\w\-]+)=(.*)$`)
propRegex = regexp.MustCompile(`^--([\w\-]+)`)
propEqRegex = regexp.MustCompile(`^--([\w\-\.]+)=(.*)$`)
propRegex = regexp.MustCompile(`^--([\w\-\.]+)`)
propFlagRegex = regexp.MustCompile(`^-(\w+)`)
)

Expand All @@ -25,7 +25,7 @@ func init() {
// 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[name]; found {
if val, found := globalProps.props[toPropName(name)]; found {
return val
}

Expand All @@ -36,6 +36,13 @@ func GetProp(name string) string {
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)
Expand All @@ -45,7 +52,7 @@ func toEnvName(name string) string {

// IsSet returns true if property is set, otherwise it returns false
func IsSet(name string) bool {
_, found := globalProps.props[name]
_, found := globalProps.props[toPropName(name)]
return found
}

Expand All @@ -66,29 +73,29 @@ func parseArgs(args []string) *propsStruct {
//for _, arg := range args {
if arr := propEqRegex.FindStringSubmatch(arg); arr != nil {
// prop: --some-prop=xyz
res.props[arr[1]] = arr[2]
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[arr[1]] = args[i+1]
res.props[toPropName(arr[1])] = args[i+1]
i++
} else {
// if next argument is a property/flag, then set to empty
res.props[arr[1]] = ""
res.props[toPropName(arr[1])] = ""
}
} else {
// property is the last parameter, set to empty
res.props[arr[1]] = ""
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[letter] = ""
res.props[toPropName(letter)] = ""
}
} else {
// arg
Expand Down
26 changes: 25 additions & 1 deletion lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestInvalidProps(t *testing.T) {
if a != b {
t.Errorf("Expected `%d` arguments, but got `%d`", b, a)
} else {
var i int = 0
var i int
for _, v := range args {
if v != resultArgs[i] {
t.Errorf("arg `%s` should match %s", v, resultArgs[i])
Expand All @@ -82,3 +82,27 @@ func TestInvalidProps(t *testing.T) {
}

}

func TestPropNames(t *testing.T) {
// given
args := []string{
"--prop-with-dashes=val1",
"--prop.with.periods=val2",
"--prop_with_underscores=val3",
}

// when
globalProps = parseArgs(args)

// then
if IsSet("prop-with-dashes") != true {
t.Error("Expected prop-with-dashes to be set")
}
if IsSet("prop.with.periods") != true {
t.Error("Expected prop.with.periods to be set")
}
if IsSet("prop_with_underscores") != true {
t.Errorf("Expected prop_with_underscores to be set `true`")
}

}