This repository has been archived by the owner on Jun 13, 2021. It is now read-only.
/
list.go
113 lines (100 loc) · 3.15 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package commands
import (
"fmt"
"io"
"sort"
"strings"
"text/tabwriter"
"time"
"github.com/docker/app/internal/store"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/config"
units "github.com/docker/go-units"
"github.com/spf13/cobra"
)
type listOptions struct {
targetContext string
}
var (
listColumns = []struct {
header string
value func(i *store.Installation) string
}{
{"INSTALLATION", func(i *store.Installation) string { return i.Name }},
{"APPLICATION", func(i *store.Installation) string { return fmt.Sprintf("%s (%s)", i.Bundle.Name, i.Bundle.Version) }},
{"LAST ACTION", func(i *store.Installation) string { return i.Result.Action }},
{"RESULT", func(i *store.Installation) string { return i.Result.Status }},
{"CREATED", func(i *store.Installation) string { return units.HumanDuration(time.Since(i.Created)) }},
{"MODIFIED", func(i *store.Installation) string { return units.HumanDuration(time.Since(i.Modified)) }},
{"REFERENCE", func(i *store.Installation) string { return i.Reference }},
}
)
func listCmd(dockerCli command.Cli) *cobra.Command {
var opts listOptions
cmd := &cobra.Command{
Use: "list [OPTIONS]",
Short: "List the installations and their last known installation result",
Aliases: []string{"ls"},
Args: cli.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return runList(dockerCli, opts)
},
}
cmd.Flags().StringVar(&opts.targetContext, "target-context", "", "List installations on this context")
return cmd
}
func runList(dockerCli command.Cli, opts listOptions) error {
targetContext := getTargetContext(opts.targetContext, dockerCli.CurrentContext())
installations, err := getInstallations(targetContext, config.Dir())
if err != nil {
return err
}
w := tabwriter.NewWriter(dockerCli.Out(), 0, 0, 1, ' ', 0)
printHeaders(w)
for _, installation := range installations {
printValues(w, installation)
}
return w.Flush()
}
func printHeaders(w io.Writer) {
var headers []string
for _, column := range listColumns {
headers = append(headers, column.header)
}
fmt.Fprintln(w, strings.Join(headers, "\t"))
}
func printValues(w io.Writer, installation *store.Installation) {
var values []string
for _, column := range listColumns {
values = append(values, column.value(installation))
}
fmt.Fprintln(w, strings.Join(values, "\t"))
}
func getInstallations(targetContext, configDir string) ([]*store.Installation, error) {
appstore, err := store.NewApplicationStore(configDir)
if err != nil {
return nil, err
}
installationStore, err := appstore.InstallationStore(targetContext)
if err != nil {
return nil, err
}
installationNames, err := installationStore.List()
if err != nil {
return nil, err
}
installations := make([]*store.Installation, len(installationNames))
for i, name := range installationNames {
installation, err := installationStore.Read(name)
if err != nil {
return nil, err
}
installations[i] = installation
}
// Sort installations with last modified first
sort.Slice(installations, func(i, j int) bool {
return installations[i].Modified.After(installations[j].Modified)
})
return installations, nil
}