-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathlist.go
More file actions
84 lines (72 loc) · 2.34 KB
/
list.go
File metadata and controls
84 lines (72 loc) · 2.34 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
package profile
import (
"errors"
"io"
"github.com/fastly/cli/pkg/argparser"
"github.com/fastly/cli/pkg/config"
fsterr "github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/text"
)
// ListCommand represents a Kingpin command.
type ListCommand struct {
argparser.Base
argparser.JSONOutput
}
// NewListCommand returns a usable command registered under the parent.
func NewListCommand(parent argparser.Registerer, g *global.Data) *ListCommand {
var c ListCommand
c.Globals = g
c.CmdClause = parent.Command("list", "List user profiles (deprecated: use 'fastly auth list' instead)")
c.RegisterFlagBool(c.JSONFlag()) // --json
return &c
}
// 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, "This command will be removed in a future release. Use 'fastly auth list' instead.\n\n")
}
if c.Globals.Verbose() && c.JSONOutput.Enabled {
return fsterr.ErrInvalidVerboseJSONCombo
}
if ok, err := c.WriteJSON(out, c.Globals.Config.Auth.Tokens); ok {
return err
}
if len(c.Globals.Config.Auth.Tokens) == 0 {
msg := "no profiles available"
return fsterr.RemediationError{
Inner: errors.New(msg),
Remediation: fsterr.ProfileRemediation(),
}
}
defaultName := c.Globals.Config.Auth.Default
if defaultName != "" {
if at := c.Globals.Config.Auth.Tokens[defaultName]; at != nil {
if c.Globals.Verbose() {
text.Break(out)
}
text.Info(out, "Default profile highlighted in red.\n\n")
display(defaultName, at, true, out, text.BoldRed)
}
}
for name, at := range c.Globals.Config.Auth.Tokens {
if name != defaultName {
text.Break(out)
display(name, at, false, out, text.Bold)
}
}
return nil
}
func display(name string, at *config.AuthToken, isDefault bool, out io.Writer, style func(a ...any) string) {
text.Output(out, style(name))
text.Break(out)
text.Output(out, "%s: %t", style("Default"), isDefault)
text.Output(out, "%s: %s", style("Email"), at.Email)
text.Output(out, "%s: %s", style("Token"), at.Token)
isSSO := at.Type == config.AuthTokenTypeSSO
text.Output(out, "%s: %t", style("SSO"), isSSO)
if isSSO {
text.Output(out, "%s: %s", style("Account ID"), at.AccountID)
text.Output(out, "%s: %s", style("Label"), at.Label)
}
}