[lxc-devel] [lxd/master] Add an option to list all aliases in "lxc image list" output

albertodonato on Github lxc-bot at linuxcontainers.org
Thu Jun 8 17:59:40 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 353 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170608/28bc659a/attachment.bin>
-------------- next part --------------
From 812d380aac2546b87286b4d4b572fb8c8636dfaf Mon Sep 17 00:00:00 2001
From: Alberto Donato <alberto.donato at gmail.com>
Date: Thu, 8 Jun 2017 11:55:54 -0600
Subject: [PATCH] Add an option to list all aliases in "lxc image list" output

Signed-off-by: Alberto Donato <alberto.donato at gmail.com>
---
 lxc/image.go | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)

diff --git a/lxc/image.go b/lxc/image.go
index 4814563c2..8209ca817 100644
--- a/lxc/image.go
+++ b/lxc/image.go
@@ -38,11 +38,12 @@ func (f *aliasList) Set(value string) error {
 }
 
 type imageCmd struct {
-	addAliases  aliasList
-	publicImage bool
-	copyAliases bool
-	autoUpdate  bool
-	format      string
+	addAliases     aliasList
+	publicImage    bool
+	copyAliases    bool
+	autoUpdate     bool
+	format         string
+	verboseAliases bool
 }
 
 func (c *imageCmd) showByDefault() bool {
@@ -110,10 +111,11 @@ lxc image export [<remote>:]<image> [target]
 lxc image info [<remote>:]<image>
     Print everything LXD knows about a given image.
 
-lxc image list [<remote>:] [filter] [--format csv|json|table|yaml]
+lxc image list [<remote>:] [filter] [--format csv|json|table|yaml] [--aliases]
     List images in the LXD image store. Filters may be of the
     <key>=<value> form for property based filtering, or part of the image
     hash or part of the image alias name.
+    In tabular output, --aliaes can be passed to list all aliases.
 
 lxc image show [<remote>:]<image>
     Yaml output of the user modifiable properties of an image.
@@ -139,6 +141,7 @@ func (c *imageCmd) flags() {
 	gnuflag.BoolVar(&c.autoUpdate, "auto-update", false, i18n.G("Keep the image up to date after initial copy"))
 	gnuflag.Var(&c.addAliases, "alias", i18n.G("New alias to define at target"))
 	gnuflag.StringVar(&c.format, "format", "table", i18n.G("Format (csv|json|table|yaml)"))
+	gnuflag.BoolVar(&c.verboseAliases, "aliases", false, i18n.G("List all aliases"))
 }
 
 func (c *imageCmd) doImageAlias(config *lxd.Config, args []string) error {
@@ -618,15 +621,25 @@ func (c *imageCmd) findDescription(props map[string]string) string {
 
 func (c *imageCmd) showImages(images []api.Image, filters []string) error {
 	tableData := func() [][]string {
+		aliasText := ""
 		data := [][]string{}
 		for _, image := range images {
 			if !c.imageShouldShow(filters, &image) {
 				continue
 			}
 
-			shortest := c.shortestAlias(image.Aliases)
-			if len(image.Aliases) > 1 {
-				shortest = fmt.Sprintf(i18n.G("%s (%d more)"), shortest, len(image.Aliases)-1)
+			if c.verboseAliases && c.format == listFormatTable {
+				aliases := []string{}
+				for _, alias := range image.Aliases {
+					aliases = append(aliases, alias.Name)
+				}
+				sort.Strings(aliases)
+				aliasText = strings.Join(aliases, "\n")
+			} else {
+				aliasText = c.shortestAlias(image.Aliases)
+				if len(image.Aliases) > 1 {
+					aliasText = fmt.Sprintf(i18n.G("%s (%d more)"), aliasText, len(image.Aliases)-1)
+				}
 			}
 			fp := image.Fingerprint[0:12]
 			public := i18n.G("no")
@@ -639,7 +652,7 @@ func (c *imageCmd) showImages(images []api.Image, filters []string) error {
 			const layout = "Jan 2, 2006 at 3:04pm (MST)"
 			uploaded := image.UploadedAt.UTC().Format(layout)
 			size := fmt.Sprintf("%.2fMB", float64(image.Size)/1024.0/1024.0)
-			data = append(data, []string{shortest, fp, public, description, image.Architecture, size, uploaded})
+			data = append(data, []string{aliasText, fp, public, description, image.Architecture, size, uploaded})
 		}
 
 		sort.Sort(SortImage(data))


More information about the lxc-devel mailing list