-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Moving form keymap settings * Add enum flag * Parse enum * Add enum to form * Add in multi-select * Add more multi select * Add enum tests * Make case insensitive * Use EqualFold Co-authored-by: Max Eshleman <[email protected]> --------- Co-authored-by: Max Eshleman <[email protected]>
- Loading branch information
1 parent
ba104d3
commit 61ac957
Showing
8 changed files
with
344 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
const ( | ||
EnumType = "enum" | ||
) | ||
|
||
type CobraEnum struct { | ||
values []string | ||
selectedIndexes []int | ||
isMultiSelect bool | ||
} | ||
|
||
func NewEnumInput(values []string, isMultiSelect bool) *CobraEnum { | ||
return &CobraEnum{ | ||
values: values, | ||
isMultiSelect: isMultiSelect, | ||
} | ||
} | ||
|
||
func (e *CobraEnum) String() string { | ||
if len(e.values) == 0 && !e.isMultiSelect { | ||
return "Invalid enum value" | ||
} | ||
|
||
values := make([]string, len(e.selectedIndexes)) | ||
for i, index := range e.selectedIndexes { | ||
values[i] = e.values[index] | ||
} | ||
|
||
return strings.Join(values, ", ") | ||
} | ||
|
||
func (e *CobraEnum) Set(v string) error { | ||
values := strings.Split(v, ",") | ||
|
||
for _, splitValue := range values { | ||
for i, value := range e.values { | ||
if strings.EqualFold(splitValue, value) { | ||
e.selectedIndexes = append(e.selectedIndexes, i) | ||
} | ||
} | ||
} | ||
|
||
if len(e.selectedIndexes) != 0 { | ||
return nil | ||
} | ||
|
||
var stringValues []string | ||
|
||
for _, value := range e.values { | ||
stringValues = append(stringValues, fmt.Sprintf("%q", value)) | ||
} | ||
|
||
return fmt.Errorf("must be one of %s", strings.Join(stringValues, ", ")) | ||
} | ||
|
||
func (e *CobraEnum) Type() string { | ||
return EnumType | ||
} | ||
|
||
func (e *CobraEnum) Values() []string { | ||
return e.values | ||
} | ||
|
||
func (e *CobraEnum) SelectedValues() []string { | ||
var selectedValues []string | ||
for _, index := range e.selectedIndexes { | ||
selectedValues = append(selectedValues, e.values[index]) | ||
} | ||
return selectedValues | ||
} | ||
|
||
func (e *CobraEnum) IsMultiSelect() bool { | ||
return e.isMultiSelect | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package command_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/renderinc/cli/pkg/command" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCobraEnum(t *testing.T) { | ||
t.Run("single select", func(t *testing.T) { | ||
t.Run("properties", func(t *testing.T) { | ||
enum := command.NewEnumInput([]string{"a", "b", "c"}, false) | ||
|
||
require.False(t, enum.IsMultiSelect()) | ||
require.Equal(t, "enum", enum.Type()) | ||
require.Equal(t, []string{"a", "b", "c"}, enum.Values()) | ||
}) | ||
|
||
t.Run("can set to valid value", func(t *testing.T) { | ||
enum := command.NewEnumInput([]string{"a", "b", "c"}, false) | ||
|
||
require.NoError(t, enum.Set("b")) | ||
require.Equal(t, []string{"b"}, enum.SelectedValues()) | ||
}) | ||
|
||
t.Run("is case insensitive", func(t *testing.T) { | ||
enum := command.NewEnumInput([]string{"a", "b", "c"}, false) | ||
|
||
require.NoError(t, enum.Set("B")) | ||
require.Equal(t, []string{"b"}, enum.SelectedValues()) | ||
}) | ||
|
||
t.Run("errors when set to invalid value", func(t *testing.T) { | ||
enum := command.NewEnumInput([]string{"a", "b", "c"}, false) | ||
|
||
require.Error(t, enum.Set("d")) | ||
require.Empty(t, enum.SelectedValues()) | ||
}) | ||
}) | ||
|
||
t.Run("multi select", func(t *testing.T) { | ||
t.Run("properties", func(t *testing.T) { | ||
enum := command.NewEnumInput([]string{"a", "b", "c"}, true) | ||
|
||
require.True(t, enum.IsMultiSelect()) | ||
require.Equal(t, "enum", enum.Type()) | ||
require.Equal(t, []string{"a", "b", "c"}, enum.Values()) | ||
}) | ||
|
||
t.Run("can set to valid value", func(t *testing.T) { | ||
enum := command.NewEnumInput([]string{"a", "b", "c"}, true) | ||
|
||
require.NoError(t, enum.Set("b,c")) | ||
require.Equal(t, []string{"b", "c"}, enum.SelectedValues()) | ||
}) | ||
|
||
t.Run("is case insensitive", func(t *testing.T) { | ||
enum := command.NewEnumInput([]string{"a", "b", "c"}, true) | ||
|
||
require.NoError(t, enum.Set("B,C")) | ||
require.Equal(t, []string{"b", "c"}, enum.SelectedValues()) | ||
}) | ||
|
||
t.Run("errors when set to invalid value", func(t *testing.T) { | ||
enum := command.NewEnumInput([]string{"a", "b", "c"}, true) | ||
|
||
require.Error(t, enum.Set("d")) | ||
require.Empty(t, enum.SelectedValues()) | ||
}) | ||
}) | ||
} |
Oops, something went wrong.