-
Notifications
You must be signed in to change notification settings - Fork 4
/
get_fleet.go
171 lines (146 loc) · 4.99 KB
/
get_fleet.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package fleet
import (
"encoding/json"
"fmt"
"io"
"strings"
"text/tabwriter"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"github.com/calyptia/api/types"
"github.com/calyptia/cli/completer"
cfg "github.com/calyptia/cli/config"
"github.com/calyptia/cli/formatters"
)
func NewCmdGetFleets(config *cfg.Config) *cobra.Command {
var name, before string
var tags []string
var last uint
var showIDs bool
var outputFormat, goTemplate string
cmd := &cobra.Command{
Use: "fleets", // calyptia get fleets
Short: "Fleets",
Long: "List all the fleets from the current project",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
var in types.FleetsParams
fs := cmd.Flags()
if fs.Changed("name") {
in.Name = &name
}
if fs.Changed("tags") {
in.SetTags(tags)
}
if fs.Changed("last") {
in.Last = &last
}
if fs.Changed("before") {
in.Before = &before
}
in.ProjectID = config.ProjectID
fleets, err := config.Cloud.Fleets(ctx, in)
if err != nil {
return err
}
if strings.HasPrefix(outputFormat, "go-template") {
return formatters.ApplyGoTemplate(cmd.OutOrStdout(), outputFormat, goTemplate, fleets)
}
switch outputFormat {
case "table":
return renderFleetsTable(cmd.OutOrStdout(), fleets, showIDs)
case "json":
return json.NewEncoder(cmd.OutOrStdout()).Encode(fleets)
case "yml", "yaml":
return yaml.NewEncoder(cmd.OutOrStdout()).Encode(fleets)
default:
return fmt.Errorf("unknown output format %q", outputFormat)
}
},
}
fs := cmd.Flags()
fs.StringVar(&name, "name", "", "Filter fleets by name")
fs.StringSliceVar(&tags, "tags", nil, "Filter fleets by tags")
fs.UintVar(&last, "last", 0, "Paginate and retrieve only the last N fleets")
fs.StringVar(&before, "before", "", "Paginate and retrieve the fleets before the given cursor")
fs.BoolVar(&showIDs, "show-ids", false, "Show fleets IDs. Only applies when output format is table")
fs.StringVarP(&outputFormat, "output-format", "o", "table", "Output format. Allowed: table, json, yaml, go-template, go-template-file")
fs.StringVar(&goTemplate, "template", "", "Template string or path to use when -o=go-template, -o=go-template-file. The template format is golang templates\n[http://golang.org/pkg/text/template/#pkg-overview]")
_ = cmd.RegisterFlagCompletionFunc("output-format", formatters.CompleteOutputFormat)
return cmd
}
func NewCmdGetFleet(config *cfg.Config) *cobra.Command {
var showIDs bool
var outputFormat, goTemplate string
completer := completer.Completer{Config: config}
cmd := &cobra.Command{
Use: "fleet FLEET", // calyptia get fleets
Args: cobra.ExactArgs(1),
ValidArgsFunction: completer.CompleteFleets,
Short: "Display a Fleet Fleet",
Long: "Display a Fleet by ID or name",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
fleetKey := args[0]
fleetID, err := completer.LoadFleetID(fleetKey)
if err != nil {
return err
}
fleet, err := config.Cloud.Fleet(ctx, types.FleetParams{FleetID: fleetID})
if err != nil {
return err
}
if strings.HasPrefix(outputFormat, "go-template") {
return formatters.ApplyGoTemplate(cmd.OutOrStdout(), outputFormat, goTemplate, fleet)
}
switch outputFormat {
case "table":
return renderFleetsTable(cmd.OutOrStdout(),
types.Fleets{Items: []types.Fleet{fleet}}, showIDs)
case "json":
return json.NewEncoder(cmd.OutOrStdout()).Encode(fleet)
case "yml", "yaml":
return yaml.NewEncoder(cmd.OutOrStdout()).Encode(fleet)
default:
return fmt.Errorf("unknown output format %q", outputFormat)
}
},
}
fs := cmd.Flags()
fs.BoolVar(&showIDs, "show-ids", false, "Show fleets IDs. Only applies when output format is table")
fs.StringVarP(&outputFormat, "output-format", "o", "table", "Output format. Allowed: table, json, yaml, go-template, go-template-file")
fs.StringVar(&goTemplate, "template", "", "Template string or path to use when -o=go-template, -o=go-template-file. The template format is golang templates\n[http://golang.org/pkg/text/template/#pkg-overview]")
_ = cmd.RegisterFlagCompletionFunc("output-format", formatters.CompleteOutputFormat)
return cmd
}
func renderFleetsTable(w io.Writer, fleets types.Fleets, showIDs bool) error {
tw := tabwriter.NewWriter(w, 0, 4, 1, ' ', 0)
if showIDs {
if _, err := fmt.Fprint(tw, "ID\t"); err != nil {
return err
}
}
fmt.Fprintln(tw, "NAME\tTAGS\tAGE")
for _, fleet := range fleets.Items {
if showIDs {
_, err := fmt.Fprintf(tw, "%s\t", fleet.ID)
if err != nil {
return err
}
}
_, err := fmt.Fprintf(tw, "%s\t%s\t%s\n", fleet.Name, strings.Join(fleet.Tags, ", "), formatters.FmtTime(fleet.CreatedAt))
if err != nil {
return err
}
}
if err := tw.Flush(); err != nil {
return err
}
if fleets.EndCursor != nil {
_, err := fmt.Fprintf(w, "\n\n# Previous page:\n\tcalyptia get fleets --before %s\n", *fleets.EndCursor)
if err != nil {
return err
}
}
return nil
}