Skip to content

Commit

Permalink
fix(image): list does not parse "type" flag correctly (#578)
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Oct 24, 2023
1 parent 140cbc3 commit 9a0487a
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions internal/cmd/image/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var ListCmd = base.ListCmd{
ResourceNamePlural: "images",
DefaultColumns: []string{"id", "type", "name", "description", "architecture", "image_size", "disk_size", "created", "deprecated"},
AdditionalFlags: func(cmd *cobra.Command) {
cmd.Flags().StringP("type", "t", "", "Only show images of given type")
cmd.Flags().StringSliceP("type", "t", []string{}, "Only show images of given type")
cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates("backup", "snapshot", "system", "app"))

cmd.Flags().StringSliceP("architecture", "a", []string{}, "Only show images of given architecture: x86|arm")
Expand All @@ -32,9 +32,20 @@ var ListCmd = base.ListCmd{
Fetch: func(ctx context.Context, client hcapi2.Client, flags *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
opts := hcloud.ImageListOpts{ListOpts: listOpts, IncludeDeprecated: true}

imageType, _ := flags.GetString("type")
if len(imageType) > 0 {
opts.Type = []hcloud.ImageType{hcloud.ImageType(imageType)}
types, _ := flags.GetStringSlice("type")
var (
unknown []string
)
for _, imageType := range types {
switch imageType {
case string(hcloud.ImageTypeBackup), string(hcloud.ImageTypeSnapshot), string(hcloud.ImageTypeSystem), string(hcloud.ImageTypeApp):
opts.Type = append(opts.Type, hcloud.ImageType(imageType))
default:
unknown = append(unknown, imageType)
}
}
if len(unknown) > 0 {
return nil, fmt.Errorf("unknown image type: %s\n", strings.Join(unknown, ", "))
}

architecture, _ := flags.GetStringSlice("architecture")
Expand Down

0 comments on commit 9a0487a

Please sign in to comment.