forked from fastly/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
136 lines (117 loc) · 3.83 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package ${CLI_PACKAGE}
import (
"fmt"
"io"
"github.com/fastly/cli/pkg/argparser"
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/config"
"github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/text"
"github.com/fastly/go-fastly/v4/fastly"
)
// NewListCommand returns a usable command registered under the parent.
func NewListCommand(parent argparser.Registerer, globals *config.Data, data manifest.Data) *ListCommand {
var c ListCommand
c.CmdClause = parent.Command("list", "<...>")
c.Globals = globals
c.manifest = data
// Required flags
// c.CmdClause.Flag("<...>", "<...>").Required().StringVar(&c.<...>)
c.RegisterFlag(argparser.StringFlagOpts{
Name: argparser.FlagVersionName,
Description: argparser.FlagVersionDesc,
Dst: &c.serviceVersion.Value,
Required: true,
})
// Optional Flags
// c.CmdClause.Flag("<...>", "<...>").Action(c.<...>.Set).StringVar(&c.<...>.Value)
c.RegisterFlag(argparser.StringFlagOpts{
Name: argparser.FlagServiceIDName,
Description: argparser.FlagServiceIDDesc,
Dst: &c.manifest.Flag.ServiceID,
Short: 's',
})
c.RegisterFlag(argparser.StringFlagOpts{
Action: c.serviceName.Set,
Name: argparser.FlagServiceName,
Description: argparser.FlagServiceDesc,
Dst: &c.serviceName.Value,
})
return &c
}
// ListCommand calls the Fastly API to list appropriate resources.
type ListCommand struct {
argparser.Base
manifest manifest.Data
serviceName argparser.OptionalServiceNameID
serviceVersion argparser.OptionalServiceVersion
}
// Exec invokes the application logic for the command.
func (c *ListCommand) Exec(in io.Reader, out io.Writer) error {
serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{
AllowActiveLocked: true,
Client: c.Globals.Client,
Manifest: c.manifest,
Out: out,
ServiceNameFlag: c.serviceName,
ServiceVersionFlag: c.serviceVersion,
VerboseMode: c.Globals.Flag.Verbose,
})
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]interface{}{
"Service ID": serviceID,
"Service Version": errors.ServiceVersion(serviceVersion),
})
return err
}
input := c.constructInput(serviceID, serviceVersion.Number)
rs, err := c.Globals.Client.List${CLI_API}s(input)
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]interface{}{
"Service ID": serviceID,
"Service Version": serviceVersion.Number,
})
return err
}
if c.Globals.Verbose() {
c.printVerbose(out, serviceID, rs)
} else {
c.printSummary(out, rs)
}
return nil
}
// constructInput transforms values parsed from CLI flags into an object to be used by the API client library.
func (c *ListCommand) constructInput(serviceID string) *fastly.List${CLI_API}sInput {
var input fastly.List${CLI_API}sInput
input.ServiceID = serviceID
input.ServiceVersion = serviceVersion
return &input
}
// printVerbose displays the information returned from the API in a verbose
// format.
func (c *ListCommand) printVerbose(out io.Writer, serviceID string, serviceVersion int, rs []*fastly.${CLI_API}) {
fmt.Fprintf(out, "\nService ID: %s\n", serviceID)
fmt.Fprintf(out, "Service Version: %d\n", serviceVersion)
for _, r := range rs {
fmt.Fprintf(out, "\n<...>: %s\n\n", r.<...>)
if r.CreatedAt != nil {
fmt.Fprintf(out, "Created at: %s\n", r.CreatedAt)
}
if r.UpdatedAt != nil {
fmt.Fprintf(out, "Updated at: %s\n", r.UpdatedAt)
}
if r.DeletedAt != nil {
fmt.Fprintf(out, "Deleted at: %s\n", r.DeletedAt)
}
}
}
// printSummary displays the information returned from the API in a summarised
// format.
func (c *ListCommand) printSummary(out io.Writer, rs []*fastly.${CLI_API}) {
t := text.NewTable(out)
t.AddHeader("SERVICE ID", "<...>")
for _, r := range rs {
t.AddLine(r.ServiceID, r.<...>)
}
t.Print()
}