github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/tls/subscription/list.go (about) 1 package subscription 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 var states = []string{"pending", "processing", "issued", "renewing"} 16 17 // NewListCommand returns a usable command registered under the parent. 18 func NewListCommand(parent argparser.Registerer, g *global.Data) *ListCommand { 19 var c ListCommand 20 c.CmdClause = parent.Command("list", "List all TLS subscriptions") 21 c.Globals = g 22 23 // Optional. 24 c.CmdClause.Flag("filter-active", "Limit the returned subscriptions to those that have currently active orders").BoolVar(&c.filterHasActiveOrder) 25 c.CmdClause.Flag("filter-domain", "Limit the returned subscriptions to those that include the specific domain").StringVar(&c.filterTLSDomainID) 26 c.CmdClause.Flag("filter-state", "Limit the returned subscriptions by state").HintOptions(states...).EnumVar(&c.filterState, states...) 27 c.CmdClause.Flag("include", "Include related objects (comma-separated values)").HintOptions(include...).EnumVar(&c.include, include...) // include is defined in ./describe.go 28 c.RegisterFlagBool(c.JSONFlag()) // --json 29 c.CmdClause.Flag("page", "Page number of data set to fetch").IntVar(&c.pageNumber) 30 c.CmdClause.Flag("per-page", "Number of records per page").IntVar(&c.pageSize) 31 c.CmdClause.Flag("sort", "The order in which to list the results by creation date").StringVar(&c.sort) 32 33 return &c 34 } 35 36 // ListCommand calls the Fastly API to list appropriate resources. 37 type ListCommand struct { 38 argparser.Base 39 argparser.JSONOutput 40 41 filterHasActiveOrder bool 42 filterState string 43 filterTLSDomainID string 44 include string 45 pageNumber int 46 pageSize int 47 sort string 48 } 49 50 // Exec invokes the application logic for the command. 51 func (c *ListCommand) Exec(_ io.Reader, out io.Writer) error { 52 if c.Globals.Verbose() && c.JSONOutput.Enabled { 53 return fsterr.ErrInvalidVerboseJSONCombo 54 } 55 56 input := c.constructInput() 57 58 o, err := c.Globals.APIClient.ListTLSSubscriptions(input) 59 if err != nil { 60 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 61 "Filter Active": c.filterHasActiveOrder, 62 "Filter State": c.filterState, 63 "Filter TLS Domain ID": c.filterTLSDomainID, 64 "Include": c.include, 65 "Page Number": c.pageNumber, 66 "Page Size": c.pageSize, 67 "Sort": c.sort, 68 }) 69 return err 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.ListTLSSubscriptionsInput { 89 var input fastly.ListTLSSubscriptionsInput 90 91 if c.filterHasActiveOrder { 92 input.FilterActiveOrders = c.filterHasActiveOrder 93 } 94 if c.filterState != "" { 95 input.FilterState = c.filterState 96 } 97 if c.filterTLSDomainID != "" { 98 input.FilterTLSDomainsID = c.filterTLSDomainID 99 } 100 if c.include != "" { 101 input.Include = c.include 102 } 103 if c.pageNumber > 0 { 104 input.PageNumber = c.pageNumber 105 } 106 if c.pageSize > 0 { 107 input.PageSize = c.pageSize 108 } 109 if c.sort != "" { 110 input.Sort = c.sort 111 } 112 113 return &input 114 } 115 116 // printVerbose displays the information returned from the API in a verbose 117 // format. 118 func (c *ListCommand) printVerbose(out io.Writer, rs []*fastly.TLSSubscription) { 119 for _, r := range rs { 120 fmt.Fprintf(out, "ID: %s\n", r.ID) 121 fmt.Fprintf(out, "Certificate Authority: %s\n", r.CertificateAuthority) 122 fmt.Fprintf(out, "State: %s\n", r.State) 123 124 if r.CreatedAt != nil { 125 fmt.Fprintf(out, "Created at: %s\n", r.CreatedAt) 126 } 127 if r.UpdatedAt != nil { 128 fmt.Fprintf(out, "Updated at: %s\n", r.UpdatedAt) 129 } 130 131 fmt.Fprintf(out, "\n") 132 } 133 } 134 135 // printSummary displays the information returned from the API in a summarised 136 // format. 137 func (c *ListCommand) printSummary(out io.Writer, rs []*fastly.TLSSubscription) error { 138 t := text.NewTable(out) 139 t.AddHeader("ID", "CERT AUTHORITY", "STATE", "CREATED") 140 for _, r := range rs { 141 t.AddLine(r.ID, r.CertificateAuthority, r.State, r.CreatedAt) 142 } 143 t.Print() 144 return nil 145 }