|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "strings" |
| 7 | + "text/tabwriter" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/deislabs/duffle/pkg/claim" |
| 11 | + |
| 12 | + "github.com/docker/app/internal/store" |
| 13 | + "github.com/docker/cli/cli" |
| 14 | + "github.com/docker/cli/cli/command" |
| 15 | + "github.com/docker/cli/cli/config" |
| 16 | + units "github.com/docker/go-units" |
| 17 | + "github.com/spf13/cobra" |
| 18 | +) |
| 19 | + |
| 20 | +type listOptions struct { |
| 21 | + targetContext string |
| 22 | +} |
| 23 | + |
| 24 | +var ( |
| 25 | + listColumns = []struct { |
| 26 | + header string |
| 27 | + value func(c *claim.Claim) string |
| 28 | + }{ |
| 29 | + {"INSTALLATION", func(c *claim.Claim) string { return c.Name }}, |
| 30 | + {"LAST ACTION", func(c *claim.Claim) string { return c.Result.Action }}, |
| 31 | + {"RESULT", func(c *claim.Claim) string { return c.Result.Status }}, |
| 32 | + {"CREATED", func(c *claim.Claim) string { return units.HumanDuration(time.Since(c.Created)) }}, |
| 33 | + {"MODIFIED", func(c *claim.Claim) string { return units.HumanDuration(time.Since(c.Modified)) }}} |
| 34 | +) |
| 35 | + |
| 36 | +func listCmd(dockerCli command.Cli) *cobra.Command { |
| 37 | + var opts listOptions |
| 38 | + |
| 39 | + cmd := &cobra.Command{ |
| 40 | + Use: "list [OPTIONS]", |
| 41 | + Short: "List the installations and their last known installation result", |
| 42 | + Aliases: []string{"ls"}, |
| 43 | + Args: cli.NoArgs, |
| 44 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 45 | + return runList(dockerCli, opts) |
| 46 | + }, |
| 47 | + } |
| 48 | + cmd.Flags().StringVar(&opts.targetContext, "target-context", "", "List installations on this context") |
| 49 | + |
| 50 | + return cmd |
| 51 | +} |
| 52 | + |
| 53 | +func runList(dockerCli command.Cli, opts listOptions) error { |
| 54 | + targetContext := getTargetContext(opts.targetContext, dockerCli.CurrentContext()) |
| 55 | + |
| 56 | + appstore, err := store.NewApplicationStore(config.Dir()) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + installationStore, err := appstore.InstallationStore(targetContext) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + installations, err := installationStore.List() |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + w := tabwriter.NewWriter(dockerCli.Out(), 0, 0, 1, ' ', 0) |
| 70 | + printHeaders(w) |
| 71 | + |
| 72 | + for _, c := range installations { |
| 73 | + installation, err := installationStore.Read(c) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + printValues(w, &installation) |
| 78 | + } |
| 79 | + return w.Flush() |
| 80 | +} |
| 81 | + |
| 82 | +func printHeaders(w io.Writer) { |
| 83 | + var headers []string |
| 84 | + for _, column := range listColumns { |
| 85 | + headers = append(headers, column.header) |
| 86 | + } |
| 87 | + fmt.Fprintln(w, strings.Join(headers, "\t")) |
| 88 | +} |
| 89 | + |
| 90 | +func printValues(w io.Writer, installation *claim.Claim) { |
| 91 | + var values []string |
| 92 | + for _, column := range listColumns { |
| 93 | + values = append(values, column.value(installation)) |
| 94 | + } |
| 95 | + fmt.Fprintln(w, strings.Join(values, "\t")) |
| 96 | +} |
0 commit comments