-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
94 lines (82 loc) · 1.98 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
package config
import (
"errors"
"fmt"
"os"
"sort"
"text/tabwriter"
"github.com/docker/swarmkit/api"
"github.com/docker/swarmkit/cmd/swarmctl/common"
"github.com/dustin/go-humanize"
gogotypes "github.com/gogo/protobuf/types"
"github.com/spf13/cobra"
)
type configSorter []*api.Config
func (k configSorter) Len() int { return len(k) }
func (k configSorter) Swap(i, j int) { k[i], k[j] = k[j], k[i] }
func (k configSorter) Less(i, j int) bool {
iTime, err := gogotypes.TimestampFromProto(k[i].Meta.CreatedAt)
if err != nil {
panic(err)
}
jTime, err := gogotypes.TimestampFromProto(k[j].Meta.CreatedAt)
if err != nil {
panic(err)
}
return jTime.Before(iTime)
}
var (
listCmd = &cobra.Command{
Use: "ls",
Short: "List configs",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return errors.New("ls command takes no arguments")
}
flags := cmd.Flags()
quiet, err := flags.GetBool("quiet")
if err != nil {
return err
}
client, err := common.Dial(cmd)
if err != nil {
return err
}
resp, err := client.ListConfigs(common.Context(cmd), &api.ListConfigsRequest{})
if err != nil {
return err
}
var output func(*api.Config)
if !quiet {
w := tabwriter.NewWriter(os.Stdout, 0, 4, 4, ' ', 0)
defer func() {
// Ignore flushing errors - there's nothing we can do.
_ = w.Flush()
}()
common.PrintHeader(w, "ID", "Name", "Created")
output = func(s *api.Config) {
created, err := gogotypes.TimestampFromProto(s.Meta.CreatedAt)
if err != nil {
panic(err)
}
fmt.Fprintf(w, "%s\t%s\t%s\n",
s.ID,
s.Spec.Annotations.Name,
humanize.Time(created),
)
}
} else {
output = func(s *api.Config) { fmt.Println(s.ID) }
}
sorted := configSorter(resp.Configs)
sort.Sort(sorted)
for _, s := range sorted {
output(s)
}
return nil
},
}
)
func init() {
listCmd.Flags().BoolP("quiet", "q", false, "Only display config IDs")
}