Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(image): list does not parse "type" flag correctly #578

Merged
merged 1 commit into from
Oct 24, 2023
Merged
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
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