diff --git a/cmd/admin/v2/component.go b/cmd/admin/v2/component.go index e731c59..cd0d836 100644 --- a/cmd/admin/v2/component.go +++ b/cmd/admin/v2/component.go @@ -8,6 +8,7 @@ import ( adminv2 "github.com/metal-stack/api/go/metalstack/admin/v2" apiv2 "github.com/metal-stack/api/go/metalstack/api/v2" "github.com/metal-stack/cli/cmd/config" + "github.com/metal-stack/cli/cmd/sorters" "github.com/metal-stack/metal-lib/pkg/genericcli" "github.com/metal-stack/metal-lib/pkg/genericcli/printers" "github.com/metal-stack/metal-lib/pkg/pointer" @@ -32,6 +33,7 @@ func newComponentCmd(c *config.Config) *cobra.Command { Description: "list status of components, e.g. microservices connected to the metal-apiserver", DescribePrinter: func() printers.Printer { return c.DescribePrinter }, ListPrinter: func() printers.Printer { return c.ListPrinter }, + Sorter: sorters.ComponentSorter(), OnlyCmds: genericcli.OnlyCmds(genericcli.DescribeCmd, genericcli.ListCmd, genericcli.DeleteCmd), ListCmdMutateFn: func(cmd *cobra.Command) { cmd.Flags().String("uuid", "", "lists only component with this uuid") diff --git a/cmd/sorters/component.go b/cmd/sorters/component.go new file mode 100644 index 0000000..d71e75d --- /dev/null +++ b/cmd/sorters/component.go @@ -0,0 +1,23 @@ +package sorters + +import ( + apiv2 "github.com/metal-stack/api/go/metalstack/api/v2" + "github.com/metal-stack/metal-lib/pkg/multisort" +) + +func ComponentSorter() *multisort.Sorter[*apiv2.Component] { + return multisort.New(multisort.FieldMap[*apiv2.Component]{ + "type": func(a, b *apiv2.Component, descending bool) multisort.CompareResult { + return multisort.Compare(a.Type, b.Type, descending) + }, + "identifier": func(a, b *apiv2.Component, descending bool) multisort.CompareResult { + return multisort.Compare(a.Identifier, b.Identifier, descending) + }, + "started": func(a, b *apiv2.Component, descending bool) multisort.CompareResult { + return multisort.Compare(a.StartedAt.AsTime().String(), b.StartedAt.AsTime().String(), descending) + }, + "expiration": func(a, b *apiv2.Component, descending bool) multisort.CompareResult { + return multisort.Compare(a.Token.Expires.String(), b.Token.Expires.String(), descending) + }, + }, multisort.Keys{{ID: "type", Descending: true}, {ID: "identifier", Descending: true}}) +} diff --git a/docs/admin/metalctlv2_admin_component_list.md b/docs/admin/metalctlv2_admin_component_list.md index 268c020..197f61c 100644 --- a/docs/admin/metalctlv2_admin_component_list.md +++ b/docs/admin/metalctlv2_admin_component_list.md @@ -11,6 +11,7 @@ metalctlv2 admin component list [flags] ``` -h, --help help for list --identifier string lists only component with this identifier + --sort-by strings sort by (comma separated) column(s), sort direction can be changed by appending :asc or :desc behind the column identifier. possible values: identifier|started|type --type string lists only component of this type --uuid string lists only component with this uuid ```