github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/authtoken/list.go (about) 1 package authtoken 2 3 import ( 4 "fmt" 5 "io" 6 "strings" 7 8 "github.com/fastly/go-fastly/v9/fastly" 9 10 "github.com/fastly/cli/pkg/argparser" 11 fsterr "github.com/fastly/cli/pkg/errors" 12 "github.com/fastly/cli/pkg/global" 13 "github.com/fastly/cli/pkg/text" 14 ) 15 16 // NewListCommand returns a usable command registered under the parent. 17 func NewListCommand(parent argparser.Registerer, g *global.Data) *ListCommand { 18 c := ListCommand{ 19 Base: argparser.Base{ 20 Globals: g, 21 }, 22 } 23 c.CmdClause = parent.Command("list", "List API tokens") 24 25 c.RegisterFlag(argparser.StringFlagOpts{ 26 Name: argparser.FlagCustomerIDName, 27 Description: argparser.FlagCustomerIDDesc, 28 Dst: &c.customerID.Value, 29 Action: c.customerID.Set, 30 }) 31 c.RegisterFlagBool(c.JSONFlag()) // --json 32 return &c 33 } 34 35 // ListCommand calls the Fastly API to list appropriate resources. 36 type ListCommand struct { 37 argparser.Base 38 argparser.JSONOutput 39 40 customerID argparser.OptionalCustomerID 41 } 42 43 // Exec invokes the application logic for the command. 44 func (c *ListCommand) Exec(_ io.Reader, out io.Writer) error { 45 if c.Globals.Verbose() && c.JSONOutput.Enabled { 46 return fsterr.ErrInvalidVerboseJSONCombo 47 } 48 49 var ( 50 err error 51 o []*fastly.Token 52 ) 53 54 if err = c.customerID.Parse(); err == nil { 55 if !c.customerID.WasSet && !c.Globals.Flags.Quiet { 56 text.Info(out, "Listing customer tokens for the FASTLY_CUSTOMER_ID environment variable\n\n") 57 } 58 input := c.constructInput() 59 o, err = c.Globals.APIClient.ListCustomerTokens(input) 60 if err != nil { 61 c.Globals.ErrLog.Add(err) 62 return err 63 } 64 } else { 65 o, err = c.Globals.APIClient.ListTokens(&fastly.ListTokensInput{}) 66 if err != nil { 67 c.Globals.ErrLog.Add(err) 68 return err 69 } 70 } 71 72 if ok, err := c.WriteJSON(out, o); ok { 73 return err 74 } 75 76 if c.Globals.Verbose() { 77 c.printVerbose(out, o) 78 } else { 79 err = c.printSummary(out, o) 80 if err != nil { 81 return err 82 } 83 } 84 return nil 85 } 86 87 // constructInput transforms values parsed from CLI flags into an object to be used by the API client library. 88 func (c *ListCommand) constructInput() *fastly.ListCustomerTokensInput { 89 var input fastly.ListCustomerTokensInput 90 91 input.CustomerID = c.customerID.Value 92 93 return &input 94 } 95 96 // printVerbose displays the information returned from the API in a verbose 97 // format. 98 func (c *ListCommand) printVerbose(out io.Writer, rs []*fastly.Token) { 99 for _, r := range rs { 100 fmt.Fprintf(out, "\nID: %s\n", fastly.ToValue(r.TokenID)) 101 fmt.Fprintf(out, "Name: %s\n", fastly.ToValue(r.Name)) 102 fmt.Fprintf(out, "User ID: %s\n", fastly.ToValue(r.UserID)) 103 fmt.Fprintf(out, "Services: %s\n", strings.Join(r.Services, ", ")) 104 fmt.Fprintf(out, "Scope: %s\n", fastly.ToValue(r.Scope)) 105 fmt.Fprintf(out, "IP: %s\n\n", fastly.ToValue(r.IP)) 106 107 if r.CreatedAt != nil { 108 fmt.Fprintf(out, "Created at: %s\n", r.CreatedAt) 109 } 110 if r.LastUsedAt != nil { 111 fmt.Fprintf(out, "Last used at: %s\n", r.LastUsedAt) 112 } 113 if r.ExpiresAt != nil { 114 fmt.Fprintf(out, "Expires at: %s\n", r.ExpiresAt) 115 } 116 } 117 fmt.Fprintf(out, "\n") 118 } 119 120 // printSummary displays the information returned from the API in a summarised 121 // format. 122 func (c *ListCommand) printSummary(out io.Writer, ts []*fastly.Token) error { 123 tbl := text.NewTable(out) 124 tbl.AddHeader("NAME", "TOKEN ID", "USER ID", "SCOPE", "SERVICES") 125 for _, t := range ts { 126 tbl.AddLine( 127 fastly.ToValue(t.Name), 128 fastly.ToValue(t.TokenID), 129 fastly.ToValue(t.UserID), 130 fastly.ToValue(t.Scope), 131 strings.Join(t.Services, ", "), 132 ) 133 } 134 tbl.Print() 135 return nil 136 }