Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for cobra StringArray #73

Merged
merged 1 commit into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 72 additions & 18 deletions sensu/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func TestNewGoHandler(t *testing.T) {
assert.Equal(t, os.Stdin, goHandler.framework.eventReader)
}

func allowRestrictTestUtil(t *testing.T, options []ConfigOption, args []string) (int, error) {
func simpleTestUtil(t *testing.T, options []ConfigOption, args []string) (int, error) {
t.Helper()

noOp := func(*corev2.Event) error { return nil }
Expand All @@ -169,6 +169,60 @@ func allowRestrictTestUtil(t *testing.T, options []ConfigOption, args []string)
return exitStatus, err
}

func TestUseCobraStringArray(t *testing.T) {
var value []string
opt := SlicePluginConfigOption[string]{
Argument: "array",
Env: "ENV",
Value: &value,
UseCobraStringArray: true,
}

options := []ConfigOption{&opt}
status, err := simpleTestUtil(t, options, []string{"--array", "hello world,hello"})
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if got, want := status, 0; got != want {
t.Errorf("unexpected status: got %d, want %d", got, want)
}
if got, want := value, []string{"hello world,hello"}; !cmp.Equal(got, want) {
t.Error(cmp.Diff(got, want))
}
status, err = simpleTestUtil(t, options, []string{"--array", "hello", "--array", "world"})
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if got, want := status, 0; got != want {
t.Errorf("unexpected status: got %d, want %d", got, want)
}
if got, want := value, []string{"hello", "world"}; !cmp.Equal(got, want) {
t.Error(cmp.Diff(got, want))
}
}

func TestUseCobraStringArrayWithoutString(t *testing.T) {
var value []int
opt := SlicePluginConfigOption[int]{
Argument: "array",
Env: "ENV",
Value: &value,
UseCobraStringArray: true,
}

options := []ConfigOption{&opt}
status, err := simpleTestUtil(t, options, []string{"--array", "1,2"})
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if got, want := status, 0; got != want {
t.Errorf("unexpected status: got %d, want %d", got, want)
}
if got, want := value, []int{1, 2}; !cmp.Equal(got, want) {
t.Error(cmp.Diff(got, want))
}
}

func TestAllow(t *testing.T) {
var value int
opt := PluginConfigOption[int]{
Expand All @@ -180,7 +234,7 @@ func TestAllow(t *testing.T) {
}

options := []ConfigOption{&opt}
status, err := allowRestrictTestUtil(t, options, []string{"--allowedint", "1234"})
status, err := simpleTestUtil(t, options, []string{"--allowedint", "1234"})
if err != nil {
t.Errorf("unexpected error: %s", err)
}
Expand All @@ -190,7 +244,7 @@ func TestAllow(t *testing.T) {
if got, want := value, 1234; got != want {
t.Errorf("bad value: got %d, want %d", got, want)
}
status, err = allowRestrictTestUtil(t, options, []string{"--allowedint", "42"})
status, err = simpleTestUtil(t, options, []string{"--allowedint", "42"})
if err != nil {
t.Errorf("unexpected error: %s", err)
}
Expand All @@ -200,7 +254,7 @@ func TestAllow(t *testing.T) {
if got, want := value, 42; got != want {
t.Errorf("bad value: got %d, want %d", got, want)
}
status, err = allowRestrictTestUtil(t, options, []string{"--allowedint", "43"})
status, err = simpleTestUtil(t, options, []string{"--allowedint", "43"})
if err == nil {
t.Error("expected non-nil error")
}
Expand All @@ -220,7 +274,7 @@ func TestAllowSlice(t *testing.T) {
}

options := []ConfigOption{&opt}
status, err := allowRestrictTestUtil(t, options, []string{"--allowedintslice", "1234"})
status, err := simpleTestUtil(t, options, []string{"--allowedintslice", "1234"})
if err != nil {
t.Errorf("unexpected error: %s", err)
}
Expand All @@ -230,7 +284,7 @@ func TestAllowSlice(t *testing.T) {
if got, want := value, []int{1234}; !cmp.Equal(got, want) {
t.Error(cmp.Diff(got, want))
}
status, err = allowRestrictTestUtil(t, options, []string{"--allowedintslice", "2345"})
status, err = simpleTestUtil(t, options, []string{"--allowedintslice", "2345"})
if err != nil {
t.Errorf("unexpected error: %s", err)
}
Expand All @@ -240,7 +294,7 @@ func TestAllowSlice(t *testing.T) {
if got, want := value, []int{2345}; !cmp.Equal(got, want) {
t.Error(cmp.Diff(got, want))
}
status, err = allowRestrictTestUtil(t, options, []string{"--allowedintslice", "1234,2345"})
status, err = simpleTestUtil(t, options, []string{"--allowedintslice", "1234,2345"})
if err != nil {
t.Errorf("unexpected error: %s", err)
}
Expand All @@ -250,7 +304,7 @@ func TestAllowSlice(t *testing.T) {
if got, want := value, []int{1234, 2345}; !cmp.Equal(got, want) {
t.Error(cmp.Diff(got, want))
}
status, err = allowRestrictTestUtil(t, options, []string{"--allowedintslice", "1234,42"})
status, err = simpleTestUtil(t, options, []string{"--allowedintslice", "1234,42"})
if err == nil {
t.Error("expected non-nil error")
}
Expand All @@ -269,7 +323,7 @@ func TestAllowMap(t *testing.T) {
Value: &value,
}
options := []ConfigOption{&opt}
status, err := allowRestrictTestUtil(t, options, []string{"--allowedintmap", "1234=1234"})
status, err := simpleTestUtil(t, options, []string{"--allowedintmap", "1234=1234"})
if err != nil {
t.Errorf("unexpected error: %s", err)
}
Expand All @@ -279,7 +333,7 @@ func TestAllowMap(t *testing.T) {
if got, want := value, map[string]int{"1234": 1234}; !cmp.Equal(got, want) {
t.Error(cmp.Diff(got, want))
}
status, err = allowRestrictTestUtil(t, options, []string{"--allowedintmap", "1234=2345"})
status, err = simpleTestUtil(t, options, []string{"--allowedintmap", "1234=2345"})
if err != nil {
t.Errorf("unexpected error: %s", err)
}
Expand All @@ -289,7 +343,7 @@ func TestAllowMap(t *testing.T) {
if got, want := value, map[string]int{"1234": 2345}; !cmp.Equal(got, want) {
t.Error(cmp.Diff(got, want))
}
status, err = allowRestrictTestUtil(t, options, []string{"--allowedintmap", "1234=2345,42=45"})
status, err = simpleTestUtil(t, options, []string{"--allowedintmap", "1234=2345,42=45"})
if err == nil {
t.Error("expected non-nil error")
}
Expand All @@ -309,7 +363,7 @@ func TestRestrict(t *testing.T) {
}

options := []ConfigOption{&opt}
status, err := allowRestrictTestUtil(t, options, []string{"--allowedint", "1234"})
status, err := simpleTestUtil(t, options, []string{"--allowedint", "1234"})
if err != nil {
t.Errorf("unexpected error: %s", err)
}
Expand All @@ -319,7 +373,7 @@ func TestRestrict(t *testing.T) {
if got, want := value, 1234; got != want {
t.Errorf("bad value: got %d, want %d", got, want)
}
status, err = allowRestrictTestUtil(t, options, []string{"--allowedint", "1000"})
status, err = simpleTestUtil(t, options, []string{"--allowedint", "1000"})
if err != nil {
t.Errorf("unexpected error: %s", err)
}
Expand All @@ -329,7 +383,7 @@ func TestRestrict(t *testing.T) {
if got, want := value, 1000; got != want {
t.Errorf("bad value: got %d, want %d", got, want)
}
status, err = allowRestrictTestUtil(t, options, []string{"--allowedint", "42"})
status, err = simpleTestUtil(t, options, []string{"--allowedint", "42"})
if err == nil {
t.Error("expected non-nil error")
}
Expand All @@ -349,7 +403,7 @@ func TestRestrictSlice(t *testing.T) {
}

options := []ConfigOption{&opt}
status, err := allowRestrictTestUtil(t, options, []string{"--allowedintslice", "1234,4321"})
status, err := simpleTestUtil(t, options, []string{"--allowedintslice", "1234,4321"})
if err != nil {
t.Errorf("unexpected error: %s", err)
}
Expand All @@ -359,7 +413,7 @@ func TestRestrictSlice(t *testing.T) {
if got, want := value, []int{1234, 4321}; !cmp.Equal(got, want) {
t.Error(cmp.Diff(got, want))
}
status, err = allowRestrictTestUtil(t, options, []string{"--allowedintslice", "1234,2345"})
status, err = simpleTestUtil(t, options, []string{"--allowedintslice", "1234,2345"})
if err == nil {
t.Error("expected non-nil error")
}
Expand All @@ -378,7 +432,7 @@ func TestRestrictMap(t *testing.T) {
Value: &value,
}
options := []ConfigOption{&opt}
status, err := allowRestrictTestUtil(t, options, []string{"--allowedintmap", "1234=1234"})
status, err := simpleTestUtil(t, options, []string{"--allowedintmap", "1234=1234"})
if err != nil {
t.Errorf("unexpected error: %s", err)
}
Expand All @@ -388,7 +442,7 @@ func TestRestrictMap(t *testing.T) {
if got, want := value, map[string]int{"1234": 1234}; !cmp.Equal(got, want) {
t.Error(cmp.Diff(got, want))
}
status, err = allowRestrictTestUtil(t, options, []string{"--allowedintmap", "1234=2345"})
status, err = simpleTestUtil(t, options, []string{"--allowedintmap", "1234=2345"})
if err == nil {
t.Error("expected non-nil error")
}
Expand Down
16 changes: 15 additions & 1 deletion sensu/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ type SlicePluginConfigOption[T SliceOptionValue] struct {
// Restrict are ignored. If Allow is not set, or is set to an empty map,
// then Restrict is consulted.
Allow []T

// UseCobraStringArray instructs cobra to use the StringArrayVarP call
// instead of the StringSliceVarP call. This enables slightly different
// behaviour: space and comma separated values are considered single values,
// not lists to be split by cobra.
//
// Cobra only supports this mode of operation for lists of strings. If
// the SlicePluginConfigOption is instantiated with a non-string type, then
// this option will have no effect.
UseCobraStringArray bool
}

// MapPluginConfigOption is like PluginConfigOption, but permits using maps.
Expand Down Expand Up @@ -376,7 +386,11 @@ func (p *SlicePluginConfigOption[T]) SetupFlag(cmd *cobra.Command) error {
case *[]float64:
cmd.Flags().Float64SliceVarP(value, p.Argument, p.Shorthand, nil, p.Usage) // FIXME: viper lacks GetFloatSlice function
case *[]string:
cmd.Flags().StringSliceVarP(value, p.Argument, p.Shorthand, viper.GetStringSlice(p.Argument), p.Usage)
if p.UseCobraStringArray {
cmd.Flags().StringArrayVarP(value, p.Argument, p.Shorthand, viper.GetStringSlice(p.Argument), p.Usage)
} else {
cmd.Flags().StringSliceVarP(value, p.Argument, p.Shorthand, viper.GetStringSlice(p.Argument), p.Usage)
}
default:
return errors.New("setup flag: unknown value type")
}
Expand Down