Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Adding feature containsAny, containsAll, containsOnly, notContainsAny…
Browse files Browse the repository at this point in the history
… and notContainsAll in conditional options (#874)

* Adding feature containsAny, containsAll, containsOnly and notContains on conditional options

Signed-off-by: Finzi <[email protected]>

* Adding conditional feature notContainsAny and notContainsAll

Signed-off-by: Finzi <[email protected]>

* Correcting conditional tests

Signed-off-by: Finzi <[email protected]>

* fix to handle string with conditional contains

Signed-off-by: Finzi <[email protected]>

* adding tests to conditional input for strigs

Signed-off-by: Finzi <[email protected]>

* adding type in envVal for input flag when using contains conditional

Signed-off-by: Finzi <[email protected]>

* fixing lint errors

Signed-off-by: Finzi <[email protected]>

* fixing tests to contemplate type on envVal

Signed-off-by: Finzi <[email protected]>

* fixing errors after resolve conflicts with master

Signed-off-by: Finzi <[email protected]>

* fixing lint erros

Signed-off-by: Finzi <[email protected]>

* fixing errors after merge with master

Signed-off-by: Finzi <[email protected]>

* fixing string comparison

Signed-off-by: Finzi <[email protected]>

* fix lint errors

Signed-off-by: Finzi <[email protected]>

* simplification suggested in code review

Signed-off-by: Finzi <[email protected]>

* change _type to __type

Signed-off-by: Finzi <[email protected]>

* corretions after code review

Signed-off-by: Finzi <[email protected]>
  • Loading branch information
andressaabreuzup authored Mar 17, 2021
1 parent 5d65893 commit f0e6b60
Show file tree
Hide file tree
Showing 5 changed files with 947 additions and 13 deletions.
2 changes: 2 additions & 0 deletions pkg/formula/input/flag/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (

const (
errInvalidInputItemsMsg = "the value [%v] is not valid, only these input items [%s] are accepted in the %q flag"
TypeSuffix = "__type"
)

type InputManager struct {
Expand Down Expand Up @@ -89,6 +90,7 @@ func (in InputManager) Inputs(cmd *exec.Cmd, setup formula.Setup, flags *pflag.F

if len(inputVal) != 0 {
input.AddEnv(cmd, i.Name, inputVal)
input.AddEnv(cmd, i.Name+TypeSuffix, i.Type)
} else {
emptyInputs = append(emptyInputs, i)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/formula/input/flag/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestInputs(t *testing.T) {
in: in{
operator: "eq",
},
want: errors.New("config.json: conditional operator eq not valid. Use any of (==, !=, >, >=, <, <=)"),
want: errors.New("config.json: conditional operator eq not valid. Use any of (==, !=, >, >=, <, <=, containsAny, containsAll, containsOnly, notContainsAny, notContainsAll)"),
},
{
name: "mismatch error operator",
Expand Down
103 changes: 92 additions & 11 deletions pkg/formula/input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ import (
"strings"

"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/slice/sliceutil"
)

type InputTextDefault interface {
Text(input formula.Input) (string, error)
}

const (
TextType = "text"
ListType = "list"
BoolType = "bool"
PassType = "password"
PathType = "path"
DynamicType = "dynamic"
MultiselectType = "multiselect"
MultiselectSeparator = "|"
TextType = "text"
ListType = "list"
BoolType = "bool"
PassType = "password"
PathType = "path"
DynamicType = "dynamic"
MultiselectType = "multiselect"
MultiselectSeparator = "|"
InputConditionalSeparator = "|"
TypeSuffix = "__type"
)

// addEnv Add environment variable to run formulas.
Expand Down Expand Up @@ -51,7 +54,68 @@ func inputConditionVariableExistsOnInputList(variable string, inputList formula.
return false
}

func containsSubstring(s string, substr string) bool {
return strings.Contains(s, substr)
}

func valueContainsAny(inputType string, value string, input string) bool {
splitInput := strings.Split(input, InputConditionalSeparator)
if inputType == MultiselectType {
splitValue := strings.Split(value, MultiselectSeparator)
for _, i := range splitInput {
if sliceutil.Contains(splitValue, i) {
return true
}
}
} else {
for _, i := range splitInput {
if containsSubstring(value, i) {
return true
}
}
}
return false
}

func valueContainsAll(inputType string, value string, input string) bool {
splitInput := strings.Split(input, InputConditionalSeparator)
if inputType == MultiselectType {
splitValue := strings.Split(value, MultiselectSeparator)
for _, v := range splitInput {
if !sliceutil.Contains(splitValue, v) {
return false
}
}
} else {
for _, v := range splitInput {
if !containsSubstring(value, v) {
return false
}
}
}
return true
}

func valueContainsOnly(inputType string, value string, input string) bool {
if inputType == MultiselectType {
splitInput := strings.Split(input, InputConditionalSeparator)
splitValue := strings.Split(value, MultiselectSeparator)
if len(splitValue) != len(splitInput) {
return false
}
for _, v := range splitInput {
if !sliceutil.Contains(splitValue, v) {
return false
}
}
} else {
return strings.EqualFold(value, input)
}
return true
}

func VerifyConditional(cmd *exec.Cmd, input formula.Input, inputList formula.Inputs) (bool, error) {

if input.Condition.Variable == "" {
return true, nil
}
Expand All @@ -62,19 +126,26 @@ func VerifyConditional(cmd *exec.Cmd, input formula.Input, inputList formula.Inp
return false, fmt.Errorf("config.json: conditional variable %s not found", variable)
}

var typeValue string
var value string

for _, envVal := range cmd.Env {
components := strings.Split(envVal, "=")
if strings.ToLower(components[0]) == variable {
if strings.EqualFold(components[0], variable) {
value = components[1]
break
} else if strings.EqualFold(components[0], variable+TypeSuffix) {
typeValue = components[1]
}
}

if value == "" {
return false, nil
}

if typeValue == "" {
return false, fmt.Errorf("config.json: conditional variable %s has no type", variable)
}

// Currently using case implementation to avoid adding a dependency module or exposing
// the code to the risks of running an eval function on a user-defined variable
// optimizations are welcome, being mindful of the points above
Expand All @@ -91,9 +162,19 @@ func VerifyConditional(cmd *exec.Cmd, input formula.Input, inputList formula.Inp
return value < input.Condition.Value, nil
case "<=":
return value <= input.Condition.Value, nil
case "containsAny":
return valueContainsAny(typeValue, value, input.Condition.Value), nil
case "containsAll":
return valueContainsAll(typeValue, value, input.Condition.Value), nil
case "containsOnly":
return valueContainsOnly(typeValue, value, input.Condition.Value), nil
case "notContainsAny":
return !valueContainsAny(typeValue, value, input.Condition.Value), nil
case "notContainsAll":
return !valueContainsAll(typeValue, value, input.Condition.Value), nil
default:
return false, fmt.Errorf(
"config.json: conditional operator %s not valid. Use any of (==, !=, >, >=, <, <=)",
"config.json: conditional operator %s not valid. Use any of (==, !=, >, >=, <, <=, containsAny, containsAll, containsOnly, notContainsAny, notContainsAll)",
input.Condition.Operator,
)
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/formula/input/prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
DefaultCacheNewLabel = "Type new value?"
DefaultCacheQty = 5
EmptyItems = "no items were provided. Please insert a list of items for the input %s in the config.json file of your formula"
TypeSuffix = "__type"
)

type InputManager struct {
Expand Down Expand Up @@ -113,8 +114,12 @@ func (in InputManager) Inputs(cmd *exec.Cmd, setup formula.Setup, f *pflag.FlagS
in.persistCache(setup.FormulaPath, inputVal, i, items)
checkForSameEnv(i.Name)
input.AddEnv(cmd, i.Name, inputVal)
checkForSameEnv(i.Name + TypeSuffix)
input.AddEnv(cmd, i.Name+TypeSuffix, i.Type)
}

}

return nil
}

Expand Down
Loading

0 comments on commit f0e6b60

Please sign in to comment.