Skip to content

Commit

Permalink
feat(iso): allow to filter list by type (public, private) (#573)
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Oct 24, 2023
1 parent 19385c7 commit 140cbc3
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions internal/cmd/iso/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package iso

import (
"context"
"fmt"
"slices"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand All @@ -23,11 +26,29 @@ var ListCmd = base.ListCmd{
cmd.RegisterFlagCompletionFunc("architecture", cmpl.SuggestCandidates(string(hcloud.ArchitectureX86), string(hcloud.ArchitectureARM)))

cmd.Flags().Bool("include-architecture-wildcard", false, "Include ISOs with unknown architecture, only required if you want so show custom ISOs and still filter for architecture.")

cmd.Flags().StringSlice("type", []string{"public", "private"}, "Types to include (public, private)")
cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates("public", "private"))
},

Fetch: func(ctx context.Context, client hcapi2.Client, flags *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
opts := hcloud.ISOListOpts{ListOpts: listOpts}

types, _ := flags.GetStringSlice("type")

var unknown []string
for _, t := range types {
switch t {
case string(hcloud.ISOTypePublic), string(hcloud.ISOTypePrivate):
break
default:
unknown = append(unknown, t)
}
}
if len(unknown) > 0 {
return nil, fmt.Errorf("unknown ISO types %s\n", strings.Join(unknown, ", "))
}

architecture, _ := flags.GetStringSlice("architecture")
if len(architecture) > 0 {
for _, arch := range architecture {
Expand All @@ -47,8 +68,10 @@ var ListCmd = base.ListCmd{
isos, err := client.ISO().AllWithOpts(ctx, opts)

var resources []interface{}
for _, n := range isos {
resources = append(resources, n)
for _, iso := range isos {
if slices.Contains(types, string(iso.Type)) {
resources = append(resources, iso)
}
}
return resources, err
},
Expand Down

0 comments on commit 140cbc3

Please sign in to comment.