From e593fcb0a295cb10287fc32725e87491c5caa52b Mon Sep 17 00:00:00 2001 From: Finzi Date: Wed, 10 Mar 2021 09:47:37 -0300 Subject: [PATCH 1/4] Changing verifyConditional to only return an error when the conditional input variable does not exist in the config.json variable list Signed-off-by: Finzi --- pkg/formula/input/flag/flag.go | 2 +- pkg/formula/input/input.go | 22 +++++++++++++++++++--- pkg/formula/input/prompt/prompt.go | 2 +- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/pkg/formula/input/flag/flag.go b/pkg/formula/input/flag/flag.go index 0b69c3836..c3ee658a3 100644 --- a/pkg/formula/input/flag/flag.go +++ b/pkg/formula/input/flag/flag.go @@ -50,7 +50,7 @@ func (in InputManager) Inputs(cmd *exec.Cmd, setup formula.Setup, flags *pflag.F var inputVal string var err error - conditionPass, err := input.VerifyConditional(cmd, i) + conditionPass, err := input.VerifyConditional(cmd, i, inputs) if err != nil { return err } diff --git a/pkg/formula/input/input.go b/pkg/formula/input/input.go index 7de3d3d65..9cc322f2b 100644 --- a/pkg/formula/input/input.go +++ b/pkg/formula/input/input.go @@ -40,13 +40,28 @@ func HasRegex(input formula.Input) bool { return len(input.Pattern.Regex) > 0 } -func VerifyConditional(cmd *exec.Cmd, input formula.Input) (bool, error) { + +func inputConditionVariableExistsOnInputList(variable string, inputList formula.Inputs) (bool) { + for _, inputListElement := range inputList { + if inputListElement.Name == variable { + return true + } + } + return false +} + +func VerifyConditional(cmd *exec.Cmd, input formula.Input, inputList formula.Inputs) (bool, error) { if input.Condition.Variable == "" { return true, nil } - var value string variable := input.Condition.Variable + + if !inputConditionVariableExistsOnInputList(variable, inputList) { + return false, fmt.Errorf("config.json: conditional variable %s not found", variable) + } + + var value string for _, envVal := range cmd.Env { components := strings.Split(envVal, "=") if strings.ToLower(components[0]) == variable { @@ -54,8 +69,9 @@ func VerifyConditional(cmd *exec.Cmd, input formula.Input) (bool, error) { break } } + if value == "" { - return false, fmt.Errorf("config.json: conditional variable %s not found", variable) + return true, nil } // Currently using case implementation to avoid adding a dependency module or exposing diff --git a/pkg/formula/input/prompt/prompt.go b/pkg/formula/input/prompt/prompt.go index 7a77cafd7..e433cce3e 100644 --- a/pkg/formula/input/prompt/prompt.go +++ b/pkg/formula/input/prompt/prompt.go @@ -89,7 +89,7 @@ func (in InputManager) Inputs(cmd *exec.Cmd, setup formula.Setup, f *pflag.FlagS if err != nil { return err } - conditionPass, err := input.VerifyConditional(cmd, i) + conditionPass, err := input.VerifyConditional(cmd, i, config.Inputs) if err != nil { return err } From 508aca65ba62c0a7517ec0eddda9d2d85c7daf0f Mon Sep 17 00:00:00 2001 From: Finzi Date: Wed, 10 Mar 2021 13:43:29 -0300 Subject: [PATCH 2/4] Adding test case for nested conditional inputs with alternative flow Signed-off-by: Finzi --- pkg/formula/input/input.go | 2 +- pkg/formula/input/prompt/prompt_test.go | 31 +++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/pkg/formula/input/input.go b/pkg/formula/input/input.go index 9cc322f2b..7f9f13fe2 100644 --- a/pkg/formula/input/input.go +++ b/pkg/formula/input/input.go @@ -71,7 +71,7 @@ func VerifyConditional(cmd *exec.Cmd, input formula.Input, inputList formula.Inp } if value == "" { - return true, nil + return false, nil } // Currently using case implementation to avoid adding a dependency module or exposing diff --git a/pkg/formula/input/prompt/prompt_test.go b/pkg/formula/input/prompt/prompt_test.go index 2339931fb..60d025260 100644 --- a/pkg/formula/input/prompt/prompt_test.go +++ b/pkg/formula/input/prompt/prompt_test.go @@ -274,6 +274,33 @@ func TestConditionalInputs(t *testing.T) { "operator": "%s", "value": "in_list1" } + }, + { + "name": "sample_bool", + "type": "bool", + "default": "false", + "items": [ + "false", + "true" + ], + "condition": { + "variable": "sample_list", + "operator": "==", + "value": "in_list2" + }, + "label": "Pick: ", + "tutorial": "Select true or false for this field." + }, + { + "name": "sample_password", + "type": "password", + "condition": { + "variable": "sample_bool", + "operator": "==", + "value": "true" + }, + "label": "Pick: ", + "tutorial": "Add a secret password for this field." } ]` @@ -366,9 +393,9 @@ func TestConditionalInputs(t *testing.T) { } iTextDefault := &mocks.InputDefaultTextMock{} - iTextDefault.On("Text", mock.Anything, mock.Anything, mock.Anything).Return("default value", nil) + iTextDefault.On("Text", mock.Anything, mock.Anything, mock.Anything).Return("default", nil) iList := &mocks.InputListMock{} - iList.On("List", mock.Anything, mock.Anything, mock.Anything).Return("list value", nil) + iList.On("List", mock.Anything, mock.Anything, mock.Anything).Return("in_list1", nil) inputManager := NewInputManager( &mocks.CredResolverMock{}, From 033beb8c65c6e11119470f55814ac08e0f8c9af0 Mon Sep 17 00:00:00 2001 From: Finzi Date: Thu, 11 Mar 2021 14:58:38 -0300 Subject: [PATCH 3/4] fix lint error Signed-off-by: Finzi --- pkg/formula/input/input.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/formula/input/input.go b/pkg/formula/input/input.go index 7f9f13fe2..d7a0c1160 100644 --- a/pkg/formula/input/input.go +++ b/pkg/formula/input/input.go @@ -40,7 +40,6 @@ func HasRegex(input formula.Input) bool { return len(input.Pattern.Regex) > 0 } - func inputConditionVariableExistsOnInputList(variable string, inputList formula.Inputs) (bool) { for _, inputListElement := range inputList { if inputListElement.Name == variable { From e2e78f8ed65dd26c4a2bb5267bfd84ef1942d617 Mon Sep 17 00:00:00 2001 From: Finzi Date: Thu, 11 Mar 2021 15:07:50 -0300 Subject: [PATCH 4/4] fix lint error formater Signed-off-by: Finzi --- pkg/formula/input/input.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/formula/input/input.go b/pkg/formula/input/input.go index d7a0c1160..cc1bdc09e 100644 --- a/pkg/formula/input/input.go +++ b/pkg/formula/input/input.go @@ -40,7 +40,7 @@ func HasRegex(input formula.Input) bool { return len(input.Pattern.Regex) > 0 } -func inputConditionVariableExistsOnInputList(variable string, inputList formula.Inputs) (bool) { +func inputConditionVariableExistsOnInputList(variable string, inputList formula.Inputs) bool { for _, inputListElement := range inputList { if inputListElement.Name == variable { return true