Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 9249440

Browse files
authored
Merge pull request #756 from zappy-shu/APP-344-sort-images-by-creat-date
Sort app image ls by created
2 parents 1ec8e49 + 0a3e85a commit 9249440

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

e2e/images_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func assertImageListOutput(t *testing.T, cmd icmd.Cmd, expected string) {
2626
stdout := result.Stdout()
2727
match, err := regexp.MatchString(expected, stdout)
2828
assert.NilError(t, err)
29-
assert.Assert(t, match)
29+
assert.Assert(t, match, expected, stdout)
3030
}
3131

3232
func expectImageListOutput(t *testing.T, cmd icmd.Cmd, output string) {
@@ -222,24 +222,24 @@ c-simple-app latest [a-f0-9]{12} simple
222222
cmd.Command = dockerCli.Command("app", "build", "--tag", "push-pull", filepath.Join("testdata", "push-pull"))
223223
icmd.RunCmd(cmd).Assert(t, icmd.Success)
224224
expectImageListOutput(t, cmd, `REPOSITORY TAG APP IMAGE ID APP NAME CREATED[ ]*
225+
push-pull latest [a-f0-9]{12} push-pull [La-z0-9 ]+ ago[ ]*
225226
a-simple-app 0.1 [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
226227
a-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
227228
b-simple-app 0.2 [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
228229
b-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
229230
c-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
230-
push-pull latest [a-f0-9]{12} push-pull [La-z0-9 ]+ ago[ ]*
231231
`)
232232

233233
// can be tagged to an existing tag
234234
dockerAppImageTag("push-pull", "b-simple-app:0.2")
235235
icmd.RunCmd(cmd).Assert(t, icmd.Success)
236236
expectImageListOutput(t, cmd, `REPOSITORY TAG APP IMAGE ID APP NAME CREATED[ ]*
237+
b-simple-app 0.2 [a-f0-9]{12} push-pull [La-z0-9 ]+ ago[ ]*
238+
push-pull latest [a-f0-9]{12} push-pull [La-z0-9 ]+ ago[ ]*
237239
a-simple-app 0.1 [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
238240
a-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
239-
b-simple-app 0.2 [a-f0-9]{12} push-pull [La-z0-9 ]+ ago[ ]*
240241
b-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
241242
c-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
242-
push-pull latest [a-f0-9]{12} push-pull [La-z0-9 ]+ ago[ ]*
243243
`)
244244
})
245245
}

internal/commands/image/list.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package image
22

33
import (
4+
"sort"
45
"time"
56

67
"github.com/docker/app/internal/packager"
@@ -60,6 +61,7 @@ func runList(dockerCli command.Cli, options imageListOption, bundleStore store.B
6061
Format: NewImageFormat(options.format, options.quiet, options.digests),
6162
}
6263

64+
sortImages(images)
6365
return Write(ctx, images)
6466
}
6567

@@ -92,6 +94,18 @@ func getImageID(bundle *relocated.Bundle, ref reference.Reference) (string, erro
9294
return stringid.TruncateID(id.String()), nil
9395
}
9496

97+
func sortImages(images []imageDesc) {
98+
sort.SliceStable(images, func(i, j int) bool {
99+
if images[i].Created.IsZero() {
100+
return false
101+
}
102+
if images[j].Created.IsZero() {
103+
return true
104+
}
105+
return images[i].Created.After(images[j].Created)
106+
})
107+
}
108+
95109
type imageDesc struct {
96110
ID string `json:"id,omitempty"`
97111
Name string `json:"name,omitempty"`

internal/commands/image/list_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"bytes"
66
"fmt"
77
"testing"
8+
"time"
89

910
"github.com/docker/app/internal/relocated"
1011

@@ -126,6 +127,22 @@ a855ac937f2e
126127
}
127128
}
128129

130+
func TestSortImages(t *testing.T) {
131+
images := []imageDesc{
132+
{ID: "1", Created: time.Date(2016, time.August, 15, 0, 0, 0, 0, time.UTC)},
133+
{ID: "2"},
134+
{ID: "3"},
135+
{ID: "4", Created: time.Date(2018, time.August, 15, 0, 0, 0, 0, time.UTC)},
136+
{ID: "5", Created: time.Date(2017, time.August, 15, 0, 0, 0, 0, time.UTC)},
137+
}
138+
sortImages(images)
139+
assert.Equal(t, "4", images[0].ID)
140+
assert.Equal(t, "5", images[1].ID)
141+
assert.Equal(t, "1", images[2].ID)
142+
assert.Equal(t, "2", images[3].ID)
143+
assert.Equal(t, "3", images[4].ID)
144+
}
145+
129146
func parseReference(t *testing.T, s string) reference.Reference {
130147
ref, err := reference.Parse(s)
131148
assert.NilError(t, err)

0 commit comments

Comments
 (0)