Skip to content
Closed
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
6 changes: 4 additions & 2 deletions internal/cmd/base/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ func (lc *ListCmd[T, S]) CobraCommand(s state.State) *cobra.Command {
if lc.AdditionalFlags != nil {
lc.AdditionalFlags(cmd, s.Client())
}
cmd.Flags().StringSliceP("sort", "s", []string{}, "Determine the sorting of the result")
if lc.SortOption != nil {
cmd.Flags().StringSliceP("sort", "s", []string{}, "Determine the sorting of the result")
}
if lc.Configure != nil {
cmd = lc.Configure(s, cmd)
}
Expand Down Expand Up @@ -223,4 +225,4 @@ func (lc *ListCmd[T, S]) NewOutputTable(out io.Writer, client hcapi2.Client) *ou
t := output.NewTable[T](out)
lc.OutputTable(t, client)
return t.ToAny()
}
}
21 changes: 21 additions & 0 deletions internal/cmd/base/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package base_test
import (
"cmp"
"slices"
"strings"
"testing"

"github.com/spf13/pflag"
Expand Down Expand Up @@ -141,3 +142,23 @@ func TestList(t *testing.T) {
},
})
}

func TestListNoSortSupport(t *testing.T) {
cmd := &base.ListCmd[*fakeResource, *fakeResource]{
ResourceNamePlural: "Fake resources",
Schema: func(r *fakeResource) *fakeResource { return r },
OutputTable: func(t *output.Table[*fakeResource], _ hcapi2.Client) { t.AddAllowedFields(&fakeResource{}) },
DefaultColumns: []string{"id"},
Fetch: func(_ state.State, _ *pflag.FlagSet, _ hcloud.ListOpts, _ []string) ([]*fakeResource, error) { return nil, nil },
SortOption: nil,
}

c := cmd.CobraCommand(testutil.MockState())
helpText, _ := c.Flags().GetStringShorthand("sort")
if helpText != "" {
t.Errorf("Expected sort flag not to exist, but got shorthand: %s", helpText)
}
if c.Flags().Lookup("sort") != nil {
t.Error("Expected sort flag not to exist, but it was found")
}
}
Loading