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)
}
|