File: main_list.go

package info (click to toggle)
incus 6.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 23,864 kB
  • sloc: sh: 16,015; ansic: 3,121; python: 456; makefile: 321; ruby: 51; sql: 50; lisp: 6
file content (76 lines) | stat: -rw-r--r-- 1,880 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main

import (
	"os"
	"sort"

	"github.com/spf13/cobra"

	cli "github.com/lxc/incus/v6/internal/cmd"
	"github.com/lxc/incus/v6/shared/simplestreams"
)

type cmdList struct {
	global *cmdGlobal

	flagFormat string
}

// Command generates the command definition.
func (c *cmdList) Command() *cobra.Command {
	cmd := &cobra.Command{}
	cmd.Use = "list"
	cmd.Short = "List all images on the server"
	cmd.Long = cli.FormatSection("Description",
		`List all image on the server

This renders a table with all images currently published on the server.
`)
	cmd.RunE = c.Run
	cmd.Flags().StringVarP(&c.flagFormat, "format", "f", "table", `Format (csv|json|table|yaml|compact), use suffix ",noheader" to disable headers and ",header" to enable it if missing, e.g. csv,header`+"``")

	cmd.PreRunE = func(cmd *cobra.Command, _ []string) error {
		return cli.ValidateFlagFormatForListOutput(cmd.Flag("format").Value.String())
	}

	return cmd
}

// Run runs the actual command logic.
func (c *cmdList) Run(cmd *cobra.Command, args []string) error {
	// Quick checks.
	exit, err := c.global.CheckArgs(cmd, args, 0, 0)
	if exit {
		return err
	}

	// Get a simplestreams client.
	ss := simplestreams.NewLocalClient("")

	// Get all the images.
	images, err := ss.ListImages()
	if err != nil {
		return err
	}

	// Generate the table.
	data := [][]string{}
	for _, image := range images {
		data = append(data, []string{image.Fingerprint, image.Properties["description"], image.Properties["os"], image.Properties["release"], image.Properties["variant"], image.Architecture, image.Type, image.CreatedAt.Format("2006/01/02 15:04 MST")})
	}

	sort.Sort(cli.SortColumnsNaturally(data))

	header := []string{
		"FINGERPRINT",
		"DESCRIPTION",
		"OS",
		"RELEASE",
		"VARIANT",
		"ARCHITECTURE",
		"TYPE",
		"CREATED",
	}

	return cli.RenderTable(os.Stdout, c.flagFormat, header, data, images)
}