diff --git a/.travis.yml b/.travis.yml index 910a184..7991456 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: diff --git a/VERSION b/VERSION index 9459d4b..5625e59 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.1 +1.2 diff --git a/lib.go b/lib.go index 553ac5e..bf24c0a 100644 --- a/lib.go +++ b/lib.go @@ -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+)`) ) @@ -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 } @@ -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) @@ -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 } @@ -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 diff --git a/lib_test.go b/lib_test.go index 2196d89..e4cee22 100644 --- a/lib_test.go +++ b/lib_test.go @@ -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]) @@ -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`") + } + +}