github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/tls/custom/domain/list.go (about)

     1  package domain
     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  const emptyString = ""
    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 domains")
    21  	c.Globals = g
    22  
    23  	// Optional.
    24  	c.CmdClause.Flag("filter-cert", "Limit the returned domains to those listed in the given TLS certificate's SAN list").StringVar(&c.filterTLSCertsID)
    25  	c.CmdClause.Flag("filter-in-use", "Limit the returned domains to those currently using Fastly to terminate TLS with SNI").Action(c.filterInUse.Set).BoolVar(&c.filterInUse.Value)
    26  	c.CmdClause.Flag("filter-subscription", "Limit the returned domains to those for a given TLS subscription").StringVar(&c.filterTLSSubsID)
    27  	c.CmdClause.Flag("include", "Include related objects (comma-separated values)").HintOptions("tls_activations").EnumVar(&c.include, "tls_activations")
    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  	filterInUse      argparser.OptionalBool
    42  	filterTLSCertsID string
    43  	filterTLSSubsID  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.ListTLSDomains(input)
    59  	if err != nil {
    60  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
    61  			"Filter In Use":            c.filterInUse,
    62  			"Filter TLS Certificates":  c.filterTLSCertsID,
    63  			"Filter TLS Subscriptions": c.filterTLSSubsID,
    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  		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.ListTLSDomainsInput {
    89  	var input fastly.ListTLSDomainsInput
    90  
    91  	if c.filterInUse.WasSet {
    92  		input.FilterInUse = &c.filterInUse.Value
    93  	}
    94  	if c.filterTLSCertsID != emptyString {
    95  		input.FilterTLSCertificateID = c.filterTLSCertsID
    96  	}
    97  	if c.filterTLSSubsID != emptyString {
    98  		input.FilterTLSSubscriptionID = c.filterTLSSubsID
    99  	}
   100  	if c.include != emptyString {
   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 printVerbose(out io.Writer, rs []*fastly.TLSDomain) {
   119  	for _, r := range rs {
   120  		fmt.Fprintf(out, "\nID: %s\n", r.ID)
   121  		fmt.Fprintf(out, "Type: %s\n", r.Type)
   122  		fmt.Fprintf(out, "\n")
   123  	}
   124  }
   125  
   126  // printSummary displays the information returned from the API in a summarised
   127  // format.
   128  func (c *ListCommand) printSummary(out io.Writer, rs []*fastly.TLSDomain) error {
   129  	t := text.NewTable(out)
   130  	t.AddHeader("ID", "TYPE")
   131  	for _, r := range rs {
   132  		t.AddLine(r.ID, r.Type)
   133  	}
   134  	t.Print()
   135  	return nil
   136  }