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

Commit

Permalink
Add validation with regex in the config.json inputs (#512)
Browse files Browse the repository at this point in the history
* adding regex type config.json

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

* fixing textValidatorMock

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

* adding textValidatorMock to runner_test

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

* adding textValidatorMock to local/runner_test

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

* adding tests

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

* adding validation function to check if there is regex

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

* adding tests

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

* checking error on textRegexValidator

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

* simplifying code

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

* adding table test

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

* removing comments

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

* fixing regex when required is false

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

* simplifying the code

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

* simplifying the code

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

* fixing regex when there is default value

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

* dynamic parameter to inputValidatorMock

Signed-off-by: JoaoDanielRufino <[email protected]>
  • Loading branch information
JoaoDanielRufino authored Oct 5, 2020
1 parent 5855b88 commit 48f1baa
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 110 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func buildCommands() *cobra.Command {
formulaLocalBuilder := builder.NewBuildLocal(ritchieHomeDir, dirManager, fileManager, treeGen)

postRunner := runner.NewPostRunner(fileManager, dirManager)
inputManager := runner.NewInput(envResolvers, fileManager, inputList, inputText, inputBool, inputPassword)
inputManager := runner.NewInput(envResolvers, fileManager, inputList, inputText, inputTextValidator, inputBool, inputPassword)

formulaLocalPreRun := local.NewPreRun(ritchieHomeDir, formBuildMake, formBuildBat, formBuildSh, dirManager, fileManager)
formulaLocalRun := local.NewRunner(postRunner, inputManager, formulaLocalPreRun, fileManager, ctxFinder, userHomeDir)
Expand Down
5 changes: 5 additions & 0 deletions pkg/formula/formula.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,15 @@ type (
Items []string `json:"items"`
Cache Cache `json:"cache"`
Condition Condition `json:"condition"`
Pattern Pattern `json:"pattern"`
Tutorial string `json:"tutorial"`
Required *bool `json:"required"`
}

Pattern struct {
Regex string `json:"regex"`
MismatchText string `json:"mismatchText"`
}
Cache struct {
Active bool `json:"active"`
Qty int `json:"qty"`
Expand Down
10 changes: 9 additions & 1 deletion pkg/formula/runner/docker/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestRun(t *testing.T) {
ctxFinder := rcontext.NewFinder(ritHome, fileManager)
preRunner := NewPreRun(ritHome, dockerBuilder, dirManager, fileManager)
postRunner := runner.NewPostRunner(fileManager, dirManager)
inputRunner := runner.NewInput(env.Resolvers{"CREDENTIAL": envResolverMock{in: "test"}}, fileManager, inputMock{}, inputMock{}, inputMock{}, inputMock{})
inputRunner := runner.NewInput(env.Resolvers{"CREDENTIAL": envResolverMock{in: "test"}}, fileManager, inputMock{}, inputMock{}, inputTextValidatorMock{str: "test"}, inputMock{}, inputMock{})

type in struct {
def formula.Definition
Expand Down Expand Up @@ -220,6 +220,14 @@ func (e envResolverMock) Resolve(string) (string, error) {
return e.in, e.err
}

type inputTextValidatorMock struct {
str string
}

func (i inputTextValidatorMock) Text(name string, validate func(interface{}) error, helper ...string) (string, error) {
return i.str, nil
}

type inputMock struct {
text string
boolean bool
Expand Down
65 changes: 48 additions & 17 deletions pkg/formula/runner/inputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package runner

import (
"encoding/json"
"errors"
"fmt"
"os/exec"
"regexp"
"strconv"
"strings"

Expand All @@ -45,6 +47,7 @@ type InputManager struct {
file stream.FileWriteReadExister
prompt.InputList
prompt.InputText
prompt.InputTextValidator
prompt.InputBool
prompt.InputPassword
}
Expand All @@ -54,16 +57,18 @@ func NewInput(
file stream.FileWriteReadExister,
inList prompt.InputList,
inText prompt.InputText,
inTextValidator prompt.InputTextValidator,
inBool prompt.InputBool,
inPass prompt.InputPassword,
) formula.InputRunner {
return InputManager{
envResolvers: env,
file: file,
InputList: inList,
InputText: inText,
InputBool: inBool,
InputPassword: inPass,
envResolvers: env,
file: file,
InputList: inList,
InputText: inText,
InputTextValidator: inTextValidator,
InputBool: inBool,
InputPassword: inPass,
}
}

Expand Down Expand Up @@ -134,12 +139,7 @@ func (in InputManager) fromPrompt(cmd *exec.Cmd, setup formula.Setup) error {
if items != nil {
inputVal, err = in.loadInputValList(items, input)
} else {
validate := isRequired(input)
inputVal, err = in.Text(input.Label, validate, input.Tutorial)

if inputVal == "" {
inputVal = input.Default
}
inputVal, err = in.textValidator(input)
}
case "bool":
valBool, err = in.Bool(input.Label, items, input.Tutorial)
Expand Down Expand Up @@ -207,14 +207,12 @@ func (in InputManager) loadInputValList(items []string, input formula.Input) (st
}
items = append(items, newLabel)
}

inputVal, err := in.List(input.Label, items, input.Tutorial)
if inputVal == newLabel {
validate := isRequired(input)
inputVal, err = in.Text(input.Label, validate, input.Tutorial)
if len(inputVal) == 0 {
inputVal = input.Default
}
return in.textValidator(input)
}

return inputVal, err
}

Expand Down Expand Up @@ -256,6 +254,24 @@ func (in InputManager) resolveIfReserved(input formula.Input) (string, error) {
return "", nil
}

func (in InputManager) textValidator(input formula.Input) (string, error) {
required := isRequired(input)
var inputVal string
var err error

if in.hasRegex(input) {
inputVal, err = in.textRegexValidator(input, required)
} else {
inputVal, err = in.InputText.Text(input.Label, required, input.Tutorial)
}

if inputVal == "" {
inputVal = input.Default
}

return inputVal, err
}

func isRequired(input formula.Input) bool {
if input.Required == nil {
return input.Default == ""
Expand Down Expand Up @@ -305,3 +321,18 @@ func (in InputManager) verifyConditional(cmd *exec.Cmd, input formula.Input) (bo
)
}
}

func (in InputManager) hasRegex(input formula.Input) bool {
return len(input.Pattern.Regex) > 0
}

func (in InputManager) textRegexValidator(input formula.Input, required bool) (string, error) {
return in.InputTextValidator.Text(input.Label, func(text interface{}) error {
re := regexp.MustCompile(input.Pattern.Regex)
if re.MatchString(text.(string)) || (!required && text.(string) == "") {
return nil
}

return errors.New(input.Pattern.MismatchText)
})
}
Loading

0 comments on commit 48f1baa

Please sign in to comment.