github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/tls/config/list.go (about) 1 package config 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 var c ListCommand 19 c.CmdClause = parent.Command("list", "List all TLS configurations") 20 c.Globals = g 21 22 // Optional. 23 c.CmdClause.Flag("filter-bulk", "Optionally filter by the bulk attribute").Action(c.filterBulk.Set).BoolVar(&c.filterBulk.Value) 24 c.CmdClause.Flag("include", "Include related objects (comma-separated values)").HintOptions(include).EnumVar(&c.include, include) 25 c.RegisterFlagBool(c.JSONFlag()) // --json 26 c.CmdClause.Flag("page", "Page number of data set to fetch").IntVar(&c.pageNumber) 27 c.CmdClause.Flag("per-page", "Number of records per page").IntVar(&c.pageSize) 28 29 return &c 30 } 31 32 // ListCommand calls the Fastly API to list appropriate resources. 33 type ListCommand struct { 34 argparser.Base 35 argparser.JSONOutput 36 37 filterBulk argparser.OptionalBool 38 include string 39 pageNumber int 40 pageSize int 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 input := c.constructInput() 50 51 o, err := c.Globals.APIClient.ListCustomTLSConfigurations(input) 52 if err != nil { 53 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 54 "Filter Bulk": c.filterBulk, 55 "Include": c.include, 56 "Page Number": c.pageNumber, 57 "Page Size": c.pageSize, 58 }) 59 return err 60 } 61 62 if ok, err := c.WriteJSON(out, o); ok { 63 return err 64 } 65 66 if c.Globals.Verbose() { 67 c.printVerbose(out, o) 68 } else { 69 err = c.printSummary(out, o) 70 if err != nil { 71 return err 72 } 73 } 74 return nil 75 } 76 77 // constructInput transforms values parsed from CLI flags into an object to be used by the API client library. 78 func (c *ListCommand) constructInput() *fastly.ListCustomTLSConfigurationsInput { 79 var input fastly.ListCustomTLSConfigurationsInput 80 81 if c.filterBulk.WasSet { 82 input.FilterBulk = c.filterBulk.Value 83 } 84 if c.include != "" { 85 input.Include = c.include 86 } 87 if c.pageNumber > 0 { 88 input.PageNumber = c.pageNumber 89 } 90 if c.pageSize > 0 { 91 input.PageSize = c.pageSize 92 } 93 94 return &input 95 } 96 97 // printVerbose displays the information returned from the API in a verbose 98 // format. 99 func (c *ListCommand) printVerbose(out io.Writer, rs []*fastly.CustomTLSConfiguration) { 100 for _, r := range rs { 101 fmt.Fprintf(out, "ID: %s\n", r.ID) 102 fmt.Fprintf(out, "Name: %s\n", r.Name) 103 104 if len(r.DNSRecords) > 0 { 105 for _, v := range r.DNSRecords { 106 if v != nil { 107 fmt.Fprintf(out, "DNS Record ID: %s\n", v.ID) 108 fmt.Fprintf(out, "DNS Record Type: %s\n", v.RecordType) 109 fmt.Fprintf(out, "DNS Record Region: %s\n", v.Region) 110 } 111 } 112 } 113 114 fmt.Fprintf(out, "Bulk: %t\n", r.Bulk) 115 fmt.Fprintf(out, "Default: %t\n", r.Default) 116 117 if len(r.HTTPProtocols) > 0 { 118 for _, v := range r.HTTPProtocols { 119 fmt.Fprintf(out, "HTTP Protocol: %s\n", v) 120 } 121 } 122 123 if len(r.TLSProtocols) > 0 { 124 for _, v := range r.TLSProtocols { 125 fmt.Fprintf(out, "TLS Protocol: %s\n", v) 126 } 127 } 128 129 if r.CreatedAt != nil { 130 fmt.Fprintf(out, "Created at: %s\n", r.CreatedAt) 131 } 132 if r.UpdatedAt != nil { 133 fmt.Fprintf(out, "Updated at: %s\n", r.UpdatedAt) 134 } 135 136 fmt.Fprintf(out, "\n") 137 } 138 } 139 140 // printSummary displays the information returned from the API in a summarised 141 // format. 142 func (c *ListCommand) printSummary(out io.Writer, rs []*fastly.CustomTLSConfiguration) error { 143 t := text.NewTable(out) 144 t.AddHeader("NAME", "ID", "BULK", "DEFAULT", "TLS PROTOCOLS", "HTTP PROTOCOLS", "DNS RECORDS") 145 for _, r := range rs { 146 drs := make([]string, len(r.DNSRecords)) 147 for i, v := range r.DNSRecords { 148 if v != nil { 149 drs[i] = v.ID 150 } 151 } 152 t.AddLine( 153 r.Name, 154 r.ID, 155 r.Bulk, 156 r.Default, 157 strings.Join(r.TLSProtocols, ", "), 158 strings.Join(r.HTTPProtocols, ", "), 159 strings.Join(drs, ", "), 160 ) 161 } 162 t.Print() 163 return nil 164 }