mirror of
https://github.com/containers/podman.git
synced 2025-05-22 17:46:52 +08:00
Merge pull request #4973 from rhatdan/sort
Throw error on invalid sort value
This commit is contained in:
@ -159,6 +159,21 @@ func imagesCmd(c *cliconfig.ImagesValues) error {
|
|||||||
filters = append(filters, fmt.Sprintf("reference=%s", image))
|
filters = append(filters, fmt.Sprintf("reference=%s", image))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var sortValues = map[string]bool{
|
||||||
|
"created": true,
|
||||||
|
"id": true,
|
||||||
|
"repository": true,
|
||||||
|
"size": true,
|
||||||
|
"tag": true,
|
||||||
|
}
|
||||||
|
if !sortValues[c.Sort] {
|
||||||
|
keys := make([]string, 0, len(sortValues))
|
||||||
|
for k := range sortValues {
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
return errors.Errorf("invalid sort value %q, required values: %s", c.Sort, strings.Join(keys, ", "))
|
||||||
|
}
|
||||||
|
|
||||||
opts := imagesOptions{
|
opts := imagesOptions{
|
||||||
quiet: c.Quiet,
|
quiet: c.Quiet,
|
||||||
noHeading: c.Noheading,
|
noHeading: c.Noheading,
|
||||||
|
@ -270,26 +270,36 @@ RUN apk update && apk add man
|
|||||||
Expect(result.ExitCode()).To(Equal(0))
|
Expect(result.ExitCode()).To(Equal(0))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("podman images sort by tag", func() {
|
It("podman images sort by values", func() {
|
||||||
session := podmanTest.Podman([]string{"images", "--sort", "tag", "--format={{.Tag}}"})
|
sortValueTest := func(value string, result int, format string) []string {
|
||||||
|
f := fmt.Sprintf("{{.%s}}", format)
|
||||||
|
session := podmanTest.Podman([]string{"images", "--sort", value, "--format", f})
|
||||||
session.WaitWithDefaultTimeout()
|
session.WaitWithDefaultTimeout()
|
||||||
Expect(session.ExitCode()).To(Equal(0))
|
Expect(session.ExitCode()).To(Equal(result))
|
||||||
|
|
||||||
sortedArr := session.OutputToStringArray()
|
return session.OutputToStringArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
sortedArr := sortValueTest("created", 0, "CreatedTime")
|
||||||
|
Expect(sort.SliceIsSorted(sortedArr, func(i, j int) bool { return sortedArr[i] > sortedArr[j] })).To(BeTrue())
|
||||||
|
|
||||||
|
sortedArr = sortValueTest("id", 0, "ID")
|
||||||
Expect(sort.SliceIsSorted(sortedArr, func(i, j int) bool { return sortedArr[i] < sortedArr[j] })).To(BeTrue())
|
Expect(sort.SliceIsSorted(sortedArr, func(i, j int) bool { return sortedArr[i] < sortedArr[j] })).To(BeTrue())
|
||||||
})
|
|
||||||
|
|
||||||
It("podman images sort by size", func() {
|
sortedArr = sortValueTest("repository", 0, "Repository")
|
||||||
session := podmanTest.Podman([]string{"images", "--sort", "size", "--format={{.Size}}"})
|
Expect(sort.SliceIsSorted(sortedArr, func(i, j int) bool { return sortedArr[i] < sortedArr[j] })).To(BeTrue())
|
||||||
session.WaitWithDefaultTimeout()
|
|
||||||
Expect(session.ExitCode()).To(Equal(0))
|
|
||||||
|
|
||||||
sortedArr := session.OutputToStringArray()
|
sortedArr = sortValueTest("size", 0, "Size")
|
||||||
Expect(sort.SliceIsSorted(sortedArr, func(i, j int) bool {
|
Expect(sort.SliceIsSorted(sortedArr, func(i, j int) bool {
|
||||||
size1, _ := units.FromHumanSize(sortedArr[i])
|
size1, _ := units.FromHumanSize(sortedArr[i])
|
||||||
size2, _ := units.FromHumanSize(sortedArr[j])
|
size2, _ := units.FromHumanSize(sortedArr[j])
|
||||||
return size1 < size2
|
return size1 < size2
|
||||||
})).To(BeTrue())
|
})).To(BeTrue())
|
||||||
|
sortedArr = sortValueTest("tag", 0, "Tag")
|
||||||
|
Expect(sort.SliceIsSorted(sortedArr, func(i, j int) bool { return sortedArr[i] < sortedArr[j] })).To(BeTrue())
|
||||||
|
|
||||||
|
sortValueTest("badvalue", 125, "Tag")
|
||||||
|
sortValueTest("id", 125, "badvalue")
|
||||||
})
|
})
|
||||||
|
|
||||||
It("podman images --all flag", func() {
|
It("podman images --all flag", func() {
|
||||||
|
Reference in New Issue
Block a user