Skip to content

Commit

Permalink
Ensure inputs are validated
Browse files Browse the repository at this point in the history
  • Loading branch information
evilmarty committed Jun 13, 2024
1 parent c9889b4 commit 1ccb66d
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 5 deletions.
19 changes: 19 additions & 0 deletions command_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,25 @@ func (cs CommandSet) ParseArgs(values *map[string]any) error {
return nil
}

func (cs CommandSet) Validate(values map[string]any) error {
for _, input := range cs.Inputs() {
found := false
for k, value := range values {
if input.Name == k {
found = true
if !input.Valid(value) {
return fmt.Errorf("invalid input: %s", input.Name)
}
break
}
}
if !found {
return fmt.Errorf("missing input: %s", input.Name)
}
}
return nil
}

func (cs CommandSet) AskInputs(values *map[string]any) error {
for _, input := range cs.Inputs() {
found := false
Expand Down
61 changes: 61 additions & 0 deletions command_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,67 @@ func TestCommandSetParseEnv(t *testing.T) {
assertDeepEqual(t, expected, actual, "CommandSet.ParseEnv() returned unexpected results")
}

func TestCommandSetValidate_Empty(t *testing.T) {
cs := CommandSet{
Commands: []ConfigCommand{
{},
{},
},
Args: []string{},
}
actual := cs.Validate(map[string]any{})
assertEqual(t, nil, actual, "CommandSet.Validate() returned unexpected error")
}

func TestCommandSetValidate_Missing(t *testing.T) {
cs := CommandSet{
Commands: []ConfigCommand{
{
Inputs: []ConfigInput{
{Name: "A", DefaultValue: ""},
},
},
},
Args: []string{},
}
err := cs.Validate(map[string]any{})
expected := "missing input: A"
actual := fmt.Sprintf("%s", err)
assertEqual(t, expected, actual, "CommandSet.Validate() returned unexpected error")
}

func TestCommandSetValidate_Invalid(t *testing.T) {
cs := CommandSet{
Commands: []ConfigCommand{
{
Inputs: []ConfigInput{
{Name: "A", Pattern: "[a-z]+"},
},
},
},
Args: []string{},
}
err := cs.Validate(map[string]any{"A": "123"})
expected := "invalid input: A"
actual := fmt.Sprintf("%s", err)
assertEqual(t, expected, actual, "CommandSet.Validate() returned unexpected error")
}

func TestCommandSetValidate_Valid(t *testing.T) {
cs := CommandSet{
Commands: []ConfigCommand{
{
Inputs: []ConfigInput{
{Name: "A"},
},
},
},
Args: []string{},
}
actual := cs.Validate(map[string]any{"A": "123"})
assertEqual(t, nil, actual, "CommandSet.Validate() returned unexpected error")
}

func TestCommandSetRunnable_True(t *testing.T) {
cs := CommandSet{
Commands: []ConfigCommand{
Expand Down
9 changes: 4 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,14 @@ func (input *ConfigInput) Selectable() bool {
return input.Options.Len() > 0
}

func (input *ConfigInput) Valid(value string) bool {
func (input *ConfigInput) Valid(value any) bool {
if input.Selectable() {
return input.Options.Contains(value)
} else if input.Pattern != "" {
matched, _ := regexp.MatchString(input.Pattern, value)
return matched
} else {
} else if input.Pattern == "" {
return true
}
matched, _ := regexp.MatchString(input.Pattern, fmt.Sprintf("%v", value))
return matched
}

type ConfigInputs []ConfigInput
Expand Down
3 changes: 3 additions & 0 deletions runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func (r *Runner) Run() error {
return fmt.Errorf("failed getting input values: %v", err)
}
}
if err = cs.Validate(values); err != nil {
return err
}
data := NewTemplateData(values, r.Env)
cmd, err := cs.Cmd(data, r.Env)
if err != nil {
Expand Down

0 comments on commit 1ccb66d

Please sign in to comment.