Skip to content
This repository has been archived by the owner on Jun 13, 2021. It is now read-only.

Sort app image ls by created #756

Merged
merged 1 commit into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions e2e/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func assertImageListOutput(t *testing.T, cmd icmd.Cmd, expected string) {
stdout := result.Stdout()
match, err := regexp.MatchString(expected, stdout)
assert.NilError(t, err)
assert.Assert(t, match)
assert.Assert(t, match, expected, stdout)
}

func expectImageListOutput(t *testing.T, cmd icmd.Cmd, output string) {
Expand Down Expand Up @@ -222,24 +222,24 @@ c-simple-app latest [a-f0-9]{12} simple
cmd.Command = dockerCli.Command("app", "build", "--tag", "push-pull", filepath.Join("testdata", "push-pull"))
icmd.RunCmd(cmd).Assert(t, icmd.Success)
expectImageListOutput(t, cmd, `REPOSITORY TAG APP IMAGE ID APP NAME CREATED[ ]*
push-pull latest [a-f0-9]{12} push-pull [La-z0-9 ]+ ago[ ]*
a-simple-app 0.1 [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
a-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
b-simple-app 0.2 [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
b-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
c-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
push-pull latest [a-f0-9]{12} push-pull [La-z0-9 ]+ ago[ ]*
`)

// can be tagged to an existing tag
dockerAppImageTag("push-pull", "b-simple-app:0.2")
icmd.RunCmd(cmd).Assert(t, icmd.Success)
expectImageListOutput(t, cmd, `REPOSITORY TAG APP IMAGE ID APP NAME CREATED[ ]*
b-simple-app 0.2 [a-f0-9]{12} push-pull [La-z0-9 ]+ ago[ ]*
push-pull latest [a-f0-9]{12} push-pull [La-z0-9 ]+ ago[ ]*
a-simple-app 0.1 [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
a-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
b-simple-app 0.2 [a-f0-9]{12} push-pull [La-z0-9 ]+ ago[ ]*
b-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
c-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
push-pull latest [a-f0-9]{12} push-pull [La-z0-9 ]+ ago[ ]*
`)
})
}
14 changes: 14 additions & 0 deletions internal/commands/image/list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package image

import (
"sort"
"time"

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

sortImages(images)
return Write(ctx, images)
}

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

func sortImages(images []imageDesc) {
sort.SliceStable(images, func(i, j int) bool {
if images[i].Created.IsZero() {
return false
}
if images[j].Created.IsZero() {
return true
}
return images[i].Created.After(images[j].Created)
})
}

type imageDesc struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Expand Down
17 changes: 17 additions & 0 deletions internal/commands/image/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"fmt"
"testing"
"time"

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

Expand Down Expand Up @@ -126,6 +127,22 @@ a855ac937f2e
}
}

func TestSortImages(t *testing.T) {
images := []imageDesc{
{ID: "1", Created: time.Date(2016, time.August, 15, 0, 0, 0, 0, time.UTC)},
{ID: "2"},
{ID: "3"},
{ID: "4", Created: time.Date(2018, time.August, 15, 0, 0, 0, 0, time.UTC)},
{ID: "5", Created: time.Date(2017, time.August, 15, 0, 0, 0, 0, time.UTC)},
}
sortImages(images)
assert.Equal(t, "4", images[0].ID)
assert.Equal(t, "5", images[1].ID)
assert.Equal(t, "1", images[2].ID)
assert.Equal(t, "2", images[3].ID)
assert.Equal(t, "3", images[4].ID)
}

func parseReference(t *testing.T, s string) reference.Reference {
ref, err := reference.Parse(s)
assert.NilError(t, err)
Expand Down