-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathlist.go
More file actions
141 lines (122 loc) · 3.75 KB
/
list.go
File metadata and controls
141 lines (122 loc) · 3.75 KB
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
package authtoken
import (
"context"
"fmt"
"io"
"strings"
"github.com/fastly/go-fastly/v13/fastly"
"github.com/fastly/cli/pkg/argparser"
fsterr "github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/text"
)
// NewListCommand returns a usable command registered under the parent.
func NewListCommand(parent argparser.Registerer, g *global.Data) *ListCommand {
c := ListCommand{
Base: argparser.Base{
Globals: g,
},
}
c.CmdClause = parent.Command("list", "List API tokens (deprecated: use the Fastly API directly)")
c.RegisterFlag(argparser.StringFlagOpts{
Name: argparser.FlagCustomerIDName,
Description: argparser.FlagCustomerIDDesc,
Dst: &c.customerID.Value,
Action: c.customerID.Set,
})
c.RegisterFlagBool(c.JSONFlag()) // --json
return &c
}
// ListCommand calls the Fastly API to list appropriate resources.
type ListCommand struct {
argparser.Base
argparser.JSONOutput
customerID argparser.OptionalCustomerID
}
// Exec invokes the application logic for the command.
func (c *ListCommand) Exec(_ io.Reader, out io.Writer) error {
if !c.Globals.Flags.Quiet && !c.JSONOutput.Enabled {
text.Deprecated(out, "The 'auth-token' command tree will be removed in a future release. Use the Fastly API directly to manage API tokens.\n\n")
}
if c.Globals.Verbose() && c.JSONOutput.Enabled {
return fsterr.ErrInvalidVerboseJSONCombo
}
var (
err error
o []*fastly.Token
)
if err = c.customerID.Parse(); err == nil {
if !c.customerID.WasSet && !c.Globals.Flags.Quiet && !c.JSONOutput.Enabled {
text.Info(out, "Listing customer tokens for the FASTLY_CUSTOMER_ID environment variable\n\n")
}
input := c.constructInput()
o, err = c.Globals.APIClient.ListCustomerTokens(context.TODO(), input)
if err != nil {
c.Globals.ErrLog.Add(err)
return err
}
} else {
o, err = c.Globals.APIClient.ListTokens(context.TODO(), &fastly.ListTokensInput{})
if err != nil {
c.Globals.ErrLog.Add(err)
return err
}
}
if ok, err := c.WriteJSON(out, o); ok {
return err
}
if c.Globals.Verbose() {
c.printVerbose(out, o)
} else {
err = c.printSummary(out, o)
if err != nil {
return err
}
}
return nil
}
// constructInput transforms values parsed from CLI flags into an object to be used by the API client library.
func (c *ListCommand) constructInput() *fastly.ListCustomerTokensInput {
var input fastly.ListCustomerTokensInput
input.CustomerID = c.customerID.Value
return &input
}
// printVerbose displays the information returned from the API in a verbose
// format.
func (c *ListCommand) printVerbose(out io.Writer, rs []*fastly.Token) {
for _, r := range rs {
fmt.Fprintf(out, "\nID: %s\n", fastly.ToValue(r.TokenID))
fmt.Fprintf(out, "Name: %s\n", fastly.ToValue(r.Name))
fmt.Fprintf(out, "User ID: %s\n", fastly.ToValue(r.UserID))
fmt.Fprintf(out, "Services: %s\n", strings.Join(r.Services, ", "))
fmt.Fprintf(out, "Scope: %s\n", fastly.ToValue(r.Scope))
fmt.Fprintf(out, "IP: %s\n\n", fastly.ToValue(r.IP))
if r.CreatedAt != nil {
fmt.Fprintf(out, "Created at: %s\n", r.CreatedAt)
}
if r.LastUsedAt != nil {
fmt.Fprintf(out, "Last used at: %s\n", r.LastUsedAt)
}
if r.ExpiresAt != nil {
fmt.Fprintf(out, "Expires at: %s\n", r.ExpiresAt)
}
}
fmt.Fprintf(out, "\n")
}
// printSummary displays the information returned from the API in a summarised
// format.
func (c *ListCommand) printSummary(out io.Writer, ts []*fastly.Token) error {
tbl := text.NewTable(out)
tbl.AddHeader("NAME", "TOKEN ID", "USER ID", "SCOPE", "SERVICES")
for _, t := range ts {
tbl.AddLine(
fastly.ToValue(t.Name),
fastly.ToValue(t.TokenID),
fastly.ToValue(t.UserID),
fastly.ToValue(t.Scope),
strings.Join(t.Services, ", "),
)
}
tbl.Print()
return nil
}