Skip to content

Commit

Permalink
add filtering options for list firmware (#39)
Browse files Browse the repository at this point in the history
* add filtering options for `list firmware`, allowing you to filter on any available field

* fix for firmware list filtering, using cobras built in slice command line argument rather than custom

* fix spelling mistake

* removing unnecessary code
  • Loading branch information
jakeschuurmans authored Sep 11, 2023
1 parent 17fd475 commit f300bbf
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
43 changes: 42 additions & 1 deletion cmd/list/firmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ import (
"github.com/metal-toolbox/mctl/internal/app"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
serverservice "go.hollow.sh/serverservice/pkg/api/v1"
)

type listFirmwareFlags struct {
server string // server UUID
vendor string
model []string
component string
version string
}

var (
flagsDefinedListFirmware *listFirmwareFlags
)

// List
Expand All @@ -27,7 +40,13 @@ var listFirmware = &cobra.Command{
log.Fatal(err)
}

firmware, _, err := client.ListServerComponentFirmware(ctx, nil)
filterParams := serverservice.ComponentFirmwareVersionListParams{
Vendor: flagsDefinedListFirmware.vendor,
Model: flagsDefinedListFirmware.model,
Version: flagsDefinedListFirmware.version,
}

firmware, _, err := client.ListServerComponentFirmware(ctx, &filterParams)
if err != nil {
log.Fatal("serverservice client returned error: ", err)
}
Expand All @@ -37,6 +56,18 @@ var listFirmware = &cobra.Command{
os.Exit(0)
}

// the built in filter only filters out vendor, model, and version, will have to filter out the other columns manually
if flagsDefinedListFirmware.server != "" || flagsDefinedListFirmware.component != "" {
filteredFirmware := make([]serverservice.ComponentFirmwareVersion, 0)
for _, f := range firmware {
if (flagsDefinedListFirmware.server == "" || f.UUID.String() == flagsDefinedListFirmware.server) &&
(flagsDefinedListFirmware.component == "" || f.Component == flagsDefinedListFirmware.component) {
filteredFirmware = append(filteredFirmware, f)
}
}
firmware = filteredFirmware
}

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"UUID", "Vendor", "Model", "Component", "Version"})
for _, f := range firmware {
Expand All @@ -45,3 +76,13 @@ var listFirmware = &cobra.Command{
table.Render()
},
}

func init() {
flagsDefinedListFirmware = &listFirmwareFlags{}

listFirmware.PersistentFlags().StringVar(&flagsDefinedListFirmware.server, "server", "", "server UUID")
listFirmware.PersistentFlags().StringVar(&flagsDefinedListFirmware.vendor, "vendor", "", "vendor name")
listFirmware.PersistentFlags().StringSliceVar(&flagsDefinedListFirmware.model, "model", nil, "list of models separated by commas")
listFirmware.PersistentFlags().StringVar(&flagsDefinedListFirmware.component, "component", "", "component type")
listFirmware.PersistentFlags().StringVar(&flagsDefinedListFirmware.version, "version", "", "version number")
}
7 changes: 6 additions & 1 deletion docs/mctl_list_firmware.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ mctl list firmware [flags]
### Options

```
-h, --help help for firmware
--component string component type
-h, --help help for firmware
--model strings list of models separated by commas
--server string server UUID
--vendor string vendor name
--version string version number
```

### Options inherited from parent commands
Expand Down

0 comments on commit f300bbf

Please sign in to comment.