Skip to content

Commit 84e2bb2

Browse files
LKaemmerlingthcyron
authored andcommitted
Add filter for images (#65)
1 parent a80f242 commit 84e2bb2

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes
22

3+
## master
4+
5+
* Add type filter flag `-t` / `--type` to `image list` command
6+
37
## v1.6.1
48

59
* Fix invalid formatting of integers in `hcloud * list` commands

cli/image_list.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import (
55
"strconv"
66
"strings"
77

8-
humanize "github.com/dustin/go-humanize"
8+
"github.com/dustin/go-humanize"
99
"github.com/hetznercloud/hcloud-go/hcloud"
1010
"github.com/spf13/cobra"
1111
)
1212

1313
var imageListTableOutput *tableOutput
14+
var typeFilter string
1415

1516
func init() {
1617
imageListTableOutput = newTableOutput().
@@ -79,6 +80,7 @@ func newImageListCommand(cli *CLI) *cobra.Command {
7980
RunE: cli.wrap(runImageList),
8081
}
8182
addListOutputFlag(cmd, imageListTableOutput.Columns())
83+
cmd.Flags().StringVarP(&typeFilter, "type", "t", "", "Only show images of given type")
8284
return cmd
8385
}
8486

@@ -93,7 +95,15 @@ func runImageList(cli *CLI, cmd *cobra.Command, args []string) error {
9395
if err != nil {
9496
return err
9597
}
96-
98+
if typeFilter != "" {
99+
var _images []*hcloud.Image
100+
for _, image := range images {
101+
if string(image.Type) == typeFilter {
102+
_images = append(_images, image)
103+
}
104+
}
105+
images = _images
106+
}
97107
cols := []string{"id", "type", "name", "description", "image_size", "disk_size", "created"}
98108
if outOpts.IsSet("columns") {
99109
cols = outOpts["columns"]

0 commit comments

Comments
 (0)