github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/tls/custom/privatekey/list.go (about) 1 package privatekey 2 3 import ( 4 "fmt" 5 "io" 6 7 "github.com/fastly/go-fastly/v9/fastly" 8 9 "github.com/fastly/cli/pkg/argparser" 10 fsterr "github.com/fastly/cli/pkg/errors" 11 "github.com/fastly/cli/pkg/global" 12 "github.com/fastly/cli/pkg/text" 13 ) 14 15 // NewListCommand returns a usable command registered under the parent. 16 func NewListCommand(parent argparser.Registerer, g *global.Data) *ListCommand { 17 var c ListCommand 18 c.CmdClause = parent.Command("list", "List all TLS private keys") 19 c.Globals = g 20 21 // Optional. 22 c.CmdClause.Flag("filter-in-use", "Limit the returned keys to those without any matching TLS certificates").HintOptions("false").EnumVar(&c.filterInUse, "false") 23 c.RegisterFlagBool(c.JSONFlag()) // --json 24 c.CmdClause.Flag("page", "Page number of data set to fetch").IntVar(&c.pageNumber) 25 c.CmdClause.Flag("per-page", "Number of records per page").IntVar(&c.pageSize) 26 27 return &c 28 } 29 30 // ListCommand calls the Fastly API to list appropriate resources. 31 type ListCommand struct { 32 argparser.Base 33 argparser.JSONOutput 34 35 filterInUse string 36 pageNumber int 37 pageSize int 38 } 39 40 // Exec invokes the application logic for the command. 41 func (c *ListCommand) Exec(_ io.Reader, out io.Writer) error { 42 if c.Globals.Verbose() && c.JSONOutput.Enabled { 43 return fsterr.ErrInvalidVerboseJSONCombo 44 } 45 46 input := c.constructInput() 47 48 o, err := c.Globals.APIClient.ListPrivateKeys(input) 49 if err != nil { 50 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 51 "Filter In Use": c.filterInUse, 52 "Page Number": c.pageNumber, 53 "Page Size": c.pageSize, 54 }) 55 return err 56 } 57 58 if ok, err := c.WriteJSON(out, o); ok { 59 return err 60 } 61 62 if c.Globals.Verbose() { 63 printVerbose(out, o) 64 } else { 65 err = c.printSummary(out, o) 66 if err != nil { 67 return err 68 } 69 } 70 return nil 71 } 72 73 // constructInput transforms values parsed from CLI flags into an object to be used by the API client library. 74 func (c *ListCommand) constructInput() *fastly.ListPrivateKeysInput { 75 var input fastly.ListPrivateKeysInput 76 77 if c.filterInUse != "" { 78 input.FilterInUse = c.filterInUse 79 } 80 if c.pageNumber > 0 { 81 input.PageNumber = c.pageNumber 82 } 83 if c.pageSize > 0 { 84 input.PageSize = c.pageSize 85 } 86 87 return &input 88 } 89 90 // printVerbose displays the information returned from the API in a verbose 91 // format. 92 func printVerbose(out io.Writer, rs []*fastly.PrivateKey) { 93 for _, r := range rs { 94 fmt.Fprintf(out, "\nID: %s\n", r.ID) 95 fmt.Fprintf(out, "Name: %s\n", r.Name) 96 fmt.Fprintf(out, "Key Length: %d\n", r.KeyLength) 97 fmt.Fprintf(out, "Key Type: %s\n", r.KeyType) 98 fmt.Fprintf(out, "Public Key SHA1: %s\n", r.PublicKeySHA1) 99 100 if r.CreatedAt != nil { 101 fmt.Fprintf(out, "Created at: %s\n", r.CreatedAt) 102 } 103 104 fmt.Fprintf(out, "Replace: %t\n", r.Replace) 105 fmt.Fprintf(out, "\n") 106 } 107 } 108 109 // printSummary displays the information returned from the API in a summarised 110 // format. 111 func (c *ListCommand) printSummary(out io.Writer, rs []*fastly.PrivateKey) error { 112 t := text.NewTable(out) 113 t.AddHeader("ID", "NAME", "KEY LENGTH", "KEY TYPE", "PUBLIC KEY SHA1", "REPLACE") 114 for _, r := range rs { 115 t.AddLine(r.ID, r.Name, r.KeyLength, r.KeyType, r.PublicKeySHA1, r.Replace) 116 } 117 t.Print() 118 return nil 119 }