-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathdescribe.go
More file actions
78 lines (65 loc) · 2.15 KB
/
describe.go
File metadata and controls
78 lines (65 loc) · 2.15 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
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"
)
// NewDescribeCommand returns a usable command registered under the parent.
func NewDescribeCommand(parent argparser.Registerer, g *global.Data) *DescribeCommand {
c := DescribeCommand{
Base: argparser.Base{
Globals: g,
},
}
c.CmdClause = parent.Command("describe", "Get the current API token (deprecated: use the Fastly API directly)").Alias("get")
c.RegisterFlagBool(c.JSONFlag()) // --json
return &c
}
// DescribeCommand calls the Fastly API to describe an appropriate resource.
type DescribeCommand struct {
argparser.Base
argparser.JSONOutput
}
// Exec invokes the application logic for the command.
func (c *DescribeCommand) 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
}
o, err := c.Globals.APIClient.GetTokenSelf(context.TODO())
if err != nil {
c.Globals.ErrLog.Add(err)
return err
}
if ok, err := c.WriteJSON(out, o); ok {
return err
}
return c.print(out, o)
}
// print displays the information returned from the API.
func (c *DescribeCommand) print(out io.Writer, t *fastly.Token) error {
fmt.Fprintf(out, "\nID: %s\n", fastly.ToValue(t.TokenID))
fmt.Fprintf(out, "Name: %s\n", fastly.ToValue(t.Name))
fmt.Fprintf(out, "User ID: %s\n", fastly.ToValue(t.UserID))
fmt.Fprintf(out, "Services: %s\n", strings.Join(t.Services, ", "))
fmt.Fprintf(out, "Scope: %s\n", fastly.ToValue(t.Scope))
fmt.Fprintf(out, "IP: %s\n\n", fastly.ToValue(t.IP))
if t.CreatedAt != nil {
fmt.Fprintf(out, "Created at: %s\n", t.CreatedAt)
}
if t.LastUsedAt != nil {
fmt.Fprintf(out, "Last used at: %s\n", t.LastUsedAt)
}
if t.ExpiresAt != nil {
fmt.Fprintf(out, "Expires at: %s\n", t.ExpiresAt)
}
return nil
}