From 00ccca3dc1428ae913d12980669b96fb8161ea7a Mon Sep 17 00:00:00 2001 From: Kadu Artur Prussek Date: Wed, 23 Sep 2020 17:10:46 -0300 Subject: [PATCH 1/2] Add required field to Input struct Signed-off-by: Kadu Artur Prussek --- pkg/formula/formula.go | 1 + pkg/formula/runner/inputs.go | 8 +++++++- pkg/formula/runner/inputs_test.go | 6 ++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pkg/formula/formula.go b/pkg/formula/formula.go index f32710e7f..f56cd3752 100644 --- a/pkg/formula/formula.go +++ b/pkg/formula/formula.go @@ -50,6 +50,7 @@ type ( Cache Cache `json:"cache"` Condition Condition `json:"condition"` Tutorial string `json:"tutorial"` + Required *bool `json:"required"` } Cache struct { diff --git a/pkg/formula/runner/inputs.go b/pkg/formula/runner/inputs.go index e529ea3dc..bf66775b0 100644 --- a/pkg/formula/runner/inputs.go +++ b/pkg/formula/runner/inputs.go @@ -134,7 +134,13 @@ func (in InputManager) fromPrompt(cmd *exec.Cmd, setup formula.Setup) error { if items != nil { inputVal, err = in.loadInputValList(items, input) } else { - validate := input.Default == "" + var validate bool + if input.Required == nil { + validate = input.Default == "" + } else { + validate = *input.Required + } + inputVal, err = in.Text(input.Label, validate, input.Tutorial) if inputVal == "" { diff --git a/pkg/formula/runner/inputs_test.go b/pkg/formula/runner/inputs_test.go index d0bd4e434..b99fc0127 100644 --- a/pkg/formula/runner/inputs_test.go +++ b/pkg/formula/runner/inputs_test.go @@ -51,6 +51,12 @@ func TestInputManager_Inputs(t *testing.T) { "type": "text", "label": "Type : ", "default": "test" + }, + { + "name": "sample_text_2", + "type": "text", + "label": "Type : ", + "required": true }, { "name": "sample_list", From 8d675547f39b865086a945cb90ca0e5aab49e283 Mon Sep 17 00:00:00 2001 From: Kadu Artur Prussek Date: Wed, 23 Sep 2020 17:22:07 -0300 Subject: [PATCH 2/2] Create isRequired function Signed-off-by: Kadu Artur Prussek --- pkg/formula/runner/inputs.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/pkg/formula/runner/inputs.go b/pkg/formula/runner/inputs.go index bf66775b0..d5e9a1203 100644 --- a/pkg/formula/runner/inputs.go +++ b/pkg/formula/runner/inputs.go @@ -134,13 +134,7 @@ func (in InputManager) fromPrompt(cmd *exec.Cmd, setup formula.Setup) error { if items != nil { inputVal, err = in.loadInputValList(items, input) } else { - var validate bool - if input.Required == nil { - validate = input.Default == "" - } else { - validate = *input.Required - } - + validate := isRequired(input) inputVal, err = in.Text(input.Label, validate, input.Tutorial) if inputVal == "" { @@ -215,7 +209,7 @@ func (in InputManager) loadInputValList(items []string, input formula.Input) (st } inputVal, err := in.List(input.Label, items, input.Tutorial) if inputVal == newLabel { - validate := len(input.Default) == 0 + validate := isRequired(input) inputVal, err = in.Text(input.Label, validate, input.Tutorial) if len(inputVal) == 0 { inputVal = input.Default @@ -262,6 +256,14 @@ func (in InputManager) resolveIfReserved(input formula.Input) (string, error) { return "", nil } +func isRequired(input formula.Input) bool { + if input.Required == nil { + return input.Default == "" + } + + return *input.Required +} + func (in InputManager) verifyConditional(cmd *exec.Cmd, input formula.Input) (bool, error) { if input.Condition.Variable == "" { return true, nil