The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/lxd/pull/3415

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===
Output is now sorted in column order.

I also renamed SortImage since it can now be used to sort any list of list of strings.

Closes #3413 
From 4080e481f45ec14ec1b339664145f50af680ecb6 Mon Sep 17 00:00:00 2001
From: Alberto Donato <[email protected]>
Date: Thu, 15 Jun 2017 10:05:17 +0200
Subject: [PATCH] Fix crash when sorting images with custom columns.

Signed-off-by: Alberto Donato <[email protected]>
---
 lxc/config.go     |  2 +-
 lxc/image.go      |  4 ++--
 lxc/utils.go      | 28 +++++++++++-----------------
 lxc/utils_test.go | 37 +++++++++++++++++++++++++++++++++++++
 4 files changed, 51 insertions(+), 20 deletions(-)
 create mode 100644 lxc/utils_test.go

diff --git a/lxc/config.go b/lxc/config.go
index 7be59aca8..884736828 100644
--- a/lxc/config.go
+++ b/lxc/config.go
@@ -310,7 +310,7 @@ func (c *configCmd) run(config *lxd.Config, args []string) 
error {
                                i18n.G("COMMON NAME"),
                                i18n.G("ISSUE DATE"),
                                i18n.G("EXPIRY DATE")})
-                       sort.Sort(SortImage(data))
+                       sort.Sort(StringList(data))
                        table.AppendBulk(data)
                        table.Render()
 
diff --git a/lxc/image.go b/lxc/image.go
index 377cfaf63..bc2526124 100644
--- a/lxc/image.go
+++ b/lxc/image.go
@@ -744,7 +744,7 @@ func (c *imageCmd) showImages(images []api.Image, filters 
[]string, columns []im
                        data = append(data, row)
                }
 
-               sort.Sort(SortImage(data))
+               sort.Sort(StringList(data))
                return data
        }
 
@@ -814,7 +814,7 @@ func (c *imageCmd) showAliases(aliases 
[]api.ImageAliasesEntry, filters []string
                i18n.G("ALIAS"),
                i18n.G("FINGERPRINT"),
                i18n.G("DESCRIPTION")})
-       sort.Sort(SortImage(data))
+       sort.Sort(StringList(data))
        table.AppendBulk(data)
        table.Render()
 
diff --git a/lxc/utils.go b/lxc/utils.go
index 96d74d7a3..24e64a0ce 100644
--- a/lxc/utils.go
+++ b/lxc/utils.go
@@ -74,39 +74,33 @@ func (p *ProgressRenderer) UpdateOp(op api.Operation) {
        }
 }
 
-// Image fingerprint and alias sorting
-type SortImage [][]string
+type StringList [][]string
 
-func (a SortImage) Len() int {
+func (a StringList) Len() int {
        return len(a)
 }
 
-func (a SortImage) Swap(i, j int) {
+func (a StringList) Swap(i, j int) {
        a[i], a[j] = a[j], a[i]
 }
 
-func (a SortImage) Less(i, j int) bool {
-       if a[i][0] == a[j][0] {
-               if a[i][3] == "" {
-                       return false
-               }
-
-               if a[j][3] == "" {
-                       return true
+func (a StringList) Less(i, j int) bool {
+       x := 0
+       for x, _ = range a[i] {
+               if a[i][x] != a[j][x] {
+                       break
                }
-
-               return a[i][3] < a[j][3]
        }
 
-       if a[i][0] == "" {
+       if a[i][x] == "" {
                return false
        }
 
-       if a[j][0] == "" {
+       if a[j][x] == "" {
                return true
        }
 
-       return a[i][0] < a[j][0]
+       return a[i][x] < a[j][x]
 }
 
 // Container name sorting
diff --git a/lxc/utils_test.go b/lxc/utils_test.go
new file mode 100644
index 000000000..cd11ad829
--- /dev/null
+++ b/lxc/utils_test.go
@@ -0,0 +1,37 @@
+package main
+
+import (
+       "sort"
+       "testing"
+
+       "github.com/stretchr/testify/suite"
+)
+
+type utilsTestSuite struct {
+       suite.Suite
+}
+
+func TestUtilsTestSuite(t *testing.T) {
+       suite.Run(t, new(utilsTestSuite))
+}
+
+// StringList can be used to sort a list of strings.
+func (s *utilsTestSuite) Test_StringList() {
+       data := [][]string{{"foo", "bar"}, {"baz", "bza"}}
+       sort.Sort(StringList(data))
+       s.Equal([][]string{{"baz", "bza"}, {"foo", "bar"}}, data)
+}
+
+// The first different string is used in sorting.
+func (s *utilsTestSuite) Test_StringList_sort_by_column() {
+       data := [][]string{{"foo", "baz"}, {"foo", "bar"}}
+       sort.Sort(StringList(data))
+       s.Equal([][]string{{"foo", "bar"}, {"foo", "baz"}}, data)
+}
+
+// Empty strings are sorted last.
+func (s *utilsTestSuite) Test_StringList_empty_strings() {
+       data := [][]string{{"", "bar"}, {"foo", "baz"}}
+       sort.Sort(StringList(data))
+       s.Equal([][]string{{"foo", "baz"}, {"", "bar"}}, data)
+}
_______________________________________________
lxc-devel mailing list
[email protected]
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to