Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion docs/stackit_image_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,23 @@ stackit image list [flags]
### Examples

```
List all images
List images in your project
$ stackit image list

List images with label
$ stackit image list --label-selector ARM64,dev

List the first 10 images
$ stackit image list --limit=10

List all images
$ stackit image list --all
```

### Options

```
--all List all images available
-h, --help Help for "stackit image list"
--label-selector string Filter by label
--limit int Limit the output to the first n elements
Expand Down
27 changes: 25 additions & 2 deletions internal/cmd/image/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ type inputModel struct {
*globalflags.GlobalFlagModel
LabelSelector *string
Limit *int64
All *bool
}

const (
labelSelectorFlag = "label-selector"
limitFlag = "limit"
allFlag = "all"
)

func NewCmd(params *types.CmdParams) *cobra.Command {
Expand All @@ -40,7 +42,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
Args: args.NoArgs,
Example: examples.Build(
examples.NewExample(
`List all images`,
`List images in your project`,
`$ stackit image list`,
),
examples.NewExample(
Expand All @@ -51,6 +53,10 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
`List the first 10 images`,
`$ stackit image list --limit=10`,
),
examples.NewExample(
`List all images`,
`$ stackit image list --all`,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
Expand Down Expand Up @@ -103,6 +109,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
func configureFlags(cmd *cobra.Command) {
cmd.Flags().String(labelSelectorFlag, "", "Filter by label")
cmd.Flags().Int64(limitFlag, 0, "Limit the output to the first n elements")
cmd.Flags().Bool(allFlag, false, "List all images available")
}

func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) {
Expand All @@ -123,6 +130,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
GlobalFlagModel: globalFlags,
LabelSelector: flags.FlagToStringPointer(p, cmd, labelSelectorFlag),
Limit: limit,
All: flags.FlagToBoolPointer(p, cmd, allFlag),
}

p.DebugInputModel(model)
Expand All @@ -134,20 +142,26 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
if model.LabelSelector != nil {
request = request.LabelSelector(*model.LabelSelector)
}
if model.All != nil {
request = request.All(*model.All)
}

return request
}

func outputResult(p *print.Printer, outputFormat string, items []iaas.Image) error {
return p.OutputResult(outputFormat, items, func() error {
table := tables.NewTable()
table.SetHeader("ID", "NAME", "OS", "ARCHITECTURE", "DISTRIBUTION", "VERSION", "LABELS")
table.SetHeader("ID", "NAME", "OS", "ARCHITECTURE", "DISTRIBUTION", "VERSION", "SCOPE", "OWNER", "LABELS")
for i := range items {
item := items[i]
var (
architecture = "n/a"
os = "n/a"
distro = "n/a"
version = "n/a"
owner = "n/a"
scope = "n/a"
)
if cfg := item.Config; cfg != nil {
if v := cfg.Architecture; v != nil {
Expand All @@ -163,12 +177,21 @@ func outputResult(p *print.Printer, outputFormat string, items []iaas.Image) err
version = *v.Get()
}
}
if v := item.GetOwner(); v != "" {
owner = v
}
if v := item.GetScope(); v != "" {
scope = v
}

table.AddRow(utils.PtrString(item.Id),
utils.PtrString(item.Name),
os,
architecture,
distro,
version,
scope,
owner,
utils.JoinStringKeysPtr(*item.Labels, ","))
}
err := table.Display(p)
Expand Down
Loading