forked from fastly/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
describe.go
117 lines (100 loc) · 3.38 KB
/
describe.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
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/go-fastly/v4/fastly"
)
// NewDescribeCommand returns a usable command registered under the parent.
func NewDescribeCommand(parent argparser.Registerer, globals *config.Data, data manifest.Data) *DescribeCommand {
var c DescribeCommand
c.CmdClause = parent.Command("describe", "<...>").Alias("get")
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
}
// DescribeCommand calls the Fastly API to describe an appropriate resource.
type DescribeCommand struct {
argparser.Base
manifest manifest.Data
serviceName argparser.OptionalServiceNameID
serviceVersion argparser.OptionalServiceVersion
}
// Exec invokes the application logic for the command.
func (c *DescribeCommand) 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)
r, err := c.Globals.Client.Get${CLI_API}(input)
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]interface{}{
"Service ID": serviceID,
"Service Version": serviceVersion.Number,
})
return err
}
c.print(out, r)
return nil
}
// constructInput transforms values parsed from CLI flags into an object to be used by the API client library.
func (c *DescribeCommand) constructInput(serviceID string, serviceVersion int) *fastly.Get${CLI_API}Input {
var input fastly.Get${CLI_API}Input
input.ACLID = c.aclID
input.ID = c.id
input.ServiceID = serviceID
return &input
}
// print displays the information returned from the API.
func (c *DescribeCommand) print(out io.Writer, r *fastly.${CLI_API}) {
fmt.Fprintf(out, "\nService ID: %s\n", r.ServiceID)
fmt.Fprintf(out, "Service Version: %d\n\n", r.ServiceVersion)
fmt.Fprintf(out, "<...>: %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)
}
}