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

Commit cb2effa

Browse files
Merge pull request #522 from silvin-lubecki/list-per-date
List installations per last modification date
2 parents 8a0547e + 464c5e2 commit cb2effa

File tree

2 files changed

+74
-15
lines changed

2 files changed

+74
-15
lines changed

internal/commands/list.go

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package commands
33
import (
44
"fmt"
55
"io"
6+
"sort"
67
"strings"
78
"text/tabwriter"
89
"time"
@@ -53,28 +54,15 @@ func listCmd(dockerCli command.Cli) *cobra.Command {
5354

5455
func runList(dockerCli command.Cli, opts listOptions) error {
5556
targetContext := getTargetContext(opts.targetContext, dockerCli.CurrentContext())
56-
57-
appstore, err := store.NewApplicationStore(config.Dir())
58-
if err != nil {
59-
return err
60-
}
61-
installationStore, err := appstore.InstallationStore(targetContext)
57+
installations, err := getInstallations(targetContext, config.Dir())
6258
if err != nil {
6359
return err
6460
}
6561

66-
installations, err := installationStore.List()
67-
if err != nil {
68-
return err
69-
}
7062
w := tabwriter.NewWriter(dockerCli.Out(), 0, 0, 1, ' ', 0)
7163
printHeaders(w)
7264

73-
for _, name := range installations {
74-
installation, err := installationStore.Read(name)
75-
if err != nil {
76-
return err
77-
}
65+
for _, installation := range installations {
7866
printValues(w, installation)
7967
}
8068
return w.Flush()
@@ -95,3 +83,31 @@ func printValues(w io.Writer, installation *store.Installation) {
9583
}
9684
fmt.Fprintln(w, strings.Join(values, "\t"))
9785
}
86+
87+
func getInstallations(targetContext, configDir string) ([]*store.Installation, error) {
88+
appstore, err := store.NewApplicationStore(configDir)
89+
if err != nil {
90+
return nil, err
91+
}
92+
installationStore, err := appstore.InstallationStore(targetContext)
93+
if err != nil {
94+
return nil, err
95+
}
96+
installationNames, err := installationStore.List()
97+
if err != nil {
98+
return nil, err
99+
}
100+
installations := make([]*store.Installation, len(installationNames))
101+
for i, name := range installationNames {
102+
installation, err := installationStore.Read(name)
103+
if err != nil {
104+
return nil, err
105+
}
106+
installations[i] = installation
107+
}
108+
// Sort installations with last modified first
109+
sort.Slice(installations, func(i, j int) bool {
110+
return installations[i].Modified.After(installations[j].Modified)
111+
})
112+
return installations, nil
113+
}

internal/commands/list_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package commands
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/deislabs/duffle/pkg/claim"
8+
"github.com/docker/app/internal/store"
9+
"gotest.tools/assert"
10+
"gotest.tools/fs"
11+
)
12+
13+
func TestGetInstallationsSorted(t *testing.T) {
14+
tmpDir := fs.NewDir(t, "")
15+
defer tmpDir.Remove()
16+
appstore, err := store.NewApplicationStore(tmpDir.Path())
17+
assert.NilError(t, err)
18+
installationStore, err := appstore.InstallationStore("my-context")
19+
assert.NilError(t, err)
20+
now := time.Now()
21+
22+
oldInstallation := &store.Installation{
23+
Claim: claim.Claim{
24+
Name: "old-installation",
25+
Modified: now.Add(-1 * time.Hour),
26+
},
27+
}
28+
newInstallation := &store.Installation{
29+
Claim: claim.Claim{
30+
Name: "new-installation",
31+
Modified: now,
32+
},
33+
}
34+
assert.NilError(t, installationStore.Store(newInstallation))
35+
assert.NilError(t, installationStore.Store(oldInstallation))
36+
37+
installations, err := getInstallations("my-context", tmpDir.Path())
38+
assert.NilError(t, err)
39+
assert.Equal(t, len(installations), 2)
40+
// First installation is the last modified
41+
assert.Equal(t, installations[0].Name, "new-installation")
42+
assert.Equal(t, installations[1].Name, "old-installation")
43+
}

0 commit comments

Comments
 (0)