github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/tls/platform/list.go (about) 1 package platform 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 certificates") 19 c.Globals = g 20 21 // Optional. 22 c.CmdClause.Flag("filter-domain", "Optionally filter by the bulk attribute").StringVar(&c.filterTLSDomainID) 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 c.CmdClause.Flag("sort", "The order in which to list the results by creation date").StringVar(&c.sort) 27 28 return &c 29 } 30 31 // ListCommand calls the Fastly API to list appropriate resources. 32 type ListCommand struct { 33 argparser.Base 34 argparser.JSONOutput 35 36 filterTLSDomainID string 37 pageNumber int 38 pageSize int 39 sort string 40 } 41 42 // Exec invokes the application logic for the command. 43 func (c *ListCommand) Exec(_ io.Reader, out io.Writer) error { 44 if c.Globals.Verbose() && c.JSONOutput.Enabled { 45 return fsterr.ErrInvalidVerboseJSONCombo 46 } 47 48 input := c.constructInput() 49 50 o, err := c.Globals.APIClient.ListBulkCertificates(input) 51 if err != nil { 52 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 53 "Filter TLS Domain ID": c.filterTLSDomainID, 54 "Page Number": c.pageNumber, 55 "Page Size": c.pageSize, 56 "Sort": c.sort, 57 }) 58 return err 59 } 60 61 if ok, err := c.WriteJSON(out, o); ok { 62 return err 63 } 64 65 if c.Globals.Verbose() { 66 printVerbose(out, o) 67 } else { 68 err = c.printSummary(out, o) 69 if err != nil { 70 return err 71 } 72 } 73 return nil 74 } 75 76 // constructInput transforms values parsed from CLI flags into an object to be used by the API client library. 77 func (c *ListCommand) constructInput() *fastly.ListBulkCertificatesInput { 78 var input fastly.ListBulkCertificatesInput 79 80 if c.filterTLSDomainID != "" { 81 input.FilterTLSDomainsIDMatch = c.filterTLSDomainID 82 } 83 if c.pageNumber > 0 { 84 input.PageNumber = c.pageNumber 85 } 86 if c.pageSize > 0 { 87 input.PageSize = c.pageSize 88 } 89 if c.sort != "" { 90 input.Sort = c.sort 91 } 92 93 return &input 94 } 95 96 // printVerbose displays the information returned from the API in a verbose 97 // format. 98 func printVerbose(out io.Writer, rs []*fastly.BulkCertificate) { 99 for _, r := range rs { 100 fmt.Fprintf(out, "ID: %s\n", r.ID) 101 102 if r.NotAfter != nil { 103 fmt.Fprintf(out, "Not after: %s\n", r.NotAfter) 104 } 105 if r.NotBefore != nil { 106 fmt.Fprintf(out, "Not before: %s\n", r.NotBefore) 107 } 108 if r.CreatedAt != nil { 109 fmt.Fprintf(out, "Created at: %s\n", r.CreatedAt) 110 } 111 if r.UpdatedAt != nil { 112 fmt.Fprintf(out, "Updated at: %s\n", r.UpdatedAt) 113 } 114 115 fmt.Fprintf(out, "Replace: %t\n", r.Replace) 116 fmt.Fprintf(out, "\n") 117 } 118 } 119 120 // printSummary displays the information returned from the API in a summarised 121 // format. 122 func (c *ListCommand) printSummary(out io.Writer, rs []*fastly.BulkCertificate) error { 123 t := text.NewTable(out) 124 t.AddHeader("ID", "REPLACE", "NOT BEFORE", "NOT AFTER", "CREATED") 125 for _, r := range rs { 126 t.AddLine(r.ID, r.Replace, r.NotBefore, r.NotAfter, r.CreatedAt) 127 } 128 t.Print() 129 return nil 130 }