summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhaircommander <pehunt@redhat.com>2018-06-11 16:46:16 -0400
committerAtomic Bot <atomic-devel@projectatomic.io>2018-06-14 10:26:21 +0000
commitb868470238ee60032a50f6ed23db3c09b475842b (patch)
tree4fb678f9fbbae7b90cc7933c1ec8226b36ea7f87
parent65033b586fb353734d29dac1dfb9f342d5eeaa21 (diff)
downloadpodman-b868470238ee60032a50f6ed23db3c09b475842b.tar.gz
podman-b868470238ee60032a50f6ed23db3c09b475842b.tar.bz2
podman-b868470238ee60032a50f6ed23db3c09b475842b.zip
Added --sort flag to podman image
Signed-off-by: haircommander <pehunt@redhat.com> Closes: #937 Approved by: rhatdan
-rw-r--r--cmd/podman/images.go63
-rw-r--r--completions/bash/podman1
-rw-r--r--docs/podman-images.1.md13
-rw-r--r--test/e2e/images_test.go26
4 files changed, 95 insertions, 8 deletions
diff --git a/cmd/podman/images.go b/cmd/podman/images.go
index a54620c72..7b906178d 100644
--- a/cmd/podman/images.go
+++ b/cmd/podman/images.go
@@ -3,6 +3,7 @@ package main
import (
"context"
"reflect"
+ "sort"
"strings"
"time"
@@ -14,7 +15,6 @@ import (
"github.com/projectatomic/libpod/libpod"
"github.com/projectatomic/libpod/libpod/image"
"github.com/urfave/cli"
- "sort"
)
type imagesTemplateParams struct {
@@ -42,14 +42,42 @@ type imagesOptions struct {
digests bool
format string
outputformat string
+ sort string
}
-// Type declaration and functions for sorting the PS output by time
+// Type declaration and functions for sorting the images output
type imagesSorted []imagesTemplateParams
-func (a imagesSorted) Len() int { return len(a) }
-func (a imagesSorted) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
-func (a imagesSorted) Less(i, j int) bool { return a[i].CreatedTime.After(a[j].CreatedTime) }
+func (a imagesSorted) Len() int { return len(a) }
+func (a imagesSorted) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
+
+type imagesSortedTime struct{ imagesSorted }
+
+func (a imagesSortedTime) Less(i, j int) bool {
+ return a.imagesSorted[i].CreatedTime.After(a.imagesSorted[j].CreatedTime)
+}
+
+type imagesSortedID struct{ imagesSorted }
+
+func (a imagesSortedID) Less(i, j int) bool { return a.imagesSorted[i].ID < a.imagesSorted[j].ID }
+
+type imagesSortedTag struct{ imagesSorted }
+
+func (a imagesSortedTag) Less(i, j int) bool { return a.imagesSorted[i].Tag < a.imagesSorted[j].Tag }
+
+type imagesSortedRepository struct{ imagesSorted }
+
+func (a imagesSortedRepository) Less(i, j int) bool {
+ return a.imagesSorted[i].Repository < a.imagesSorted[j].Repository
+}
+
+type imagesSortedSize struct{ imagesSorted }
+
+func (a imagesSortedSize) Less(i, j int) bool {
+ size1, _ := units.FromHumanSize(a.imagesSorted[i].Size)
+ size2, _ := units.FromHumanSize(a.imagesSorted[j].Size)
+ return size1 < size2
+}
var (
imagesFlags = []cli.Flag{
@@ -81,6 +109,11 @@ var (
Name: "quiet, q",
Usage: "display only image IDs",
},
+ cli.StringFlag{
+ Name: "sort",
+ Usage: "Sort by size, time, id, repository or tag",
+ Value: "time",
+ },
}
imagesDescription = "lists locally stored images."
@@ -144,6 +177,7 @@ func imagesCmd(c *cli.Context) error {
noTrunc: c.Bool("no-trunc"),
digests: c.Bool("digests"),
format: c.String("format"),
+ sort: c.String("sort"),
}
opts.outputformat = opts.setOutputFormat()
@@ -204,6 +238,23 @@ func imagesToGeneric(templParams []imagesTemplateParams, JSONParams []imagesJSON
return
}
+func sortImagesOutput(sortBy string, imagesOutput imagesSorted) imagesSorted {
+ switch sortBy {
+ case "id":
+ sort.Sort(imagesSortedID{imagesOutput})
+ case "size":
+ sort.Sort(imagesSortedSize{imagesOutput})
+ case "tag":
+ sort.Sort(imagesSortedTag{imagesOutput})
+ case "repository":
+ sort.Sort(imagesSortedRepository{imagesOutput})
+ default:
+ // default is time
+ sort.Sort(imagesSortedTime{imagesOutput})
+ }
+ return imagesOutput
+}
+
// getImagesTemplateOutput returns the images information to be printed in human readable format
func getImagesTemplateOutput(ctx context.Context, runtime *libpod.Runtime, images []*image.Image, opts imagesOptions) (imagesOutput imagesSorted) {
for _, img := range images {
@@ -235,7 +286,7 @@ func getImagesTemplateOutput(ctx context.Context, runtime *libpod.Runtime, image
}
// Sort images by created time
- sort.Sort(imagesSorted(imagesOutput))
+ sortImagesOutput(opts.sort, imagesOutput)
return
}
diff --git a/completions/bash/podman b/completions/bash/podman
index d28108867..7e09670ca 100644
--- a/completions/bash/podman
+++ b/completions/bash/podman
@@ -1143,6 +1143,7 @@ _podman_images() {
"
local options_with_args="
--format
+ --sort
"
local all_options="$options_with_args $boolean_options"
diff --git a/docs/podman-images.1.md b/docs/podman-images.1.md
index 4c0073425..eb9712588 100644
--- a/docs/podman-images.1.md
+++ b/docs/podman-images.1.md
@@ -40,6 +40,9 @@ Do not truncate output.
Lists only the image IDs.
+**--sort**
+
+Sort by id, repository, size, tag or time (default: time)
## EXAMPLE
@@ -120,6 +123,16 @@ REPOSITORY TAG IMAGE ID CREATED SIZE
]
```
+```
+# podman images --sort repository
+REPOSITORY TAG IMAGE ID CREATED SIZE
+<none> <none> 2460217d76fc About a minute ago 4.41MB
+docker.io/library/alpine latest 3fd9065eaf02 5 months ago 4.41MB
+localhost/myapp latest b2e0ad03474a About a minute ago 4.41MB
+registry.access.redhat.com/rhel7 latest 7a840db7f020 2 weeks ago 211MB
+registry.fedoraproject.org/fedora 27 801894bc0e43 6 weeks ago 246MB
+```
+
## SEE ALSO
podman(1)
diff --git a/test/e2e/images_test.go b/test/e2e/images_test.go
index 8b9b889aa..5e30e95ec 100644
--- a/test/e2e/images_test.go
+++ b/test/e2e/images_test.go
@@ -1,9 +1,11 @@
package integration
import (
+ "fmt"
"os"
+ "sort"
- "fmt"
+ "github.com/docker/go-units"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
@@ -28,7 +30,6 @@ var _ = Describe("Podman images", func() {
podmanTest.Cleanup()
})
-
It("podman images", func() {
session := podmanTest.Podman([]string{"images"})
session.WaitWithDefaultTimeout()
@@ -143,4 +144,25 @@ var _ = Describe("Podman images", func() {
Expect(result.ExitCode()).To(Equal(0))
})
+ It("podman images sort by tag", func() {
+ session := podmanTest.Podman([]string{"images", "--sort", "tag", "--format={{.Tag}}"})
+ session.WaitWithDefaultTimeout()
+ Expect(session.ExitCode()).To(Equal(0))
+
+ sortedArr := session.OutputToStringArray()
+ Expect(sort.SliceIsSorted(sortedArr, func(i, j int) bool { return sortedArr[i] < sortedArr[j] })).To(BeTrue())
+ })
+
+ It("podman images sort by size", func() {
+ session := podmanTest.Podman([]string{"images", "--sort", "size", "--format={{.Size}}"})
+ session.WaitWithDefaultTimeout()
+ Expect(session.ExitCode()).To(Equal(0))
+
+ sortedArr := session.OutputToStringArray()
+ Expect(sort.SliceIsSorted(sortedArr, func(i, j int) bool {
+ size1, _ := units.FromHumanSize(sortedArr[i])
+ size2, _ := units.FromHumanSize(sortedArr[j])
+ return size1 < size2
+ })).To(BeTrue())
+ })
})