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

     1  package activation
     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 activations")
    19  	c.Globals = g
    20  
    21  	// Optional.
    22  	c.CmdClause.Flag("filter-cert", "Limit the returned activations to a specific certificate").StringVar(&c.filterTLSCertID)
    23  	c.CmdClause.Flag("filter-config", "Limit the returned activations to a specific TLS configuration").StringVar(&c.filterTLSConfigID)
    24  	c.CmdClause.Flag("filter-domain", "Limit the returned rules to a specific domain name").StringVar(&c.filterTLSDomainID)
    25  	c.CmdClause.Flag("include", "Include related objects (comma-separated values)").HintOptions(include...).EnumVar(&c.include, include...)
    26  	c.RegisterFlagBool(c.JSONFlag()) // --json
    27  	c.CmdClause.Flag("page", "Page number of data set to fetch").IntVar(&c.pageNumber)
    28  	c.CmdClause.Flag("per-page", "Number of records per page").IntVar(&c.pageSize)
    29  
    30  	return &c
    31  }
    32  
    33  // ListCommand calls the Fastly API to list appropriate resources.
    34  type ListCommand struct {
    35  	argparser.Base
    36  	argparser.JSONOutput
    37  
    38  	filterTLSCertID   string
    39  	filterTLSConfigID string
    40  	filterTLSDomainID string
    41  	include           string
    42  	pageNumber        int
    43  	pageSize          int
    44  }
    45  
    46  // Exec invokes the application logic for the command.
    47  func (c *ListCommand) Exec(_ io.Reader, out io.Writer) error {
    48  	if c.Globals.Verbose() && c.JSONOutput.Enabled {
    49  		return fsterr.ErrInvalidVerboseJSONCombo
    50  	}
    51  
    52  	input := c.constructInput()
    53  
    54  	o, err := c.Globals.APIClient.ListTLSActivations(input)
    55  	if err != nil {
    56  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
    57  			"Filter TLS Certificate ID":   c.filterTLSCertID,
    58  			"Filter TLS Configuration ID": c.filterTLSConfigID,
    59  			"Filter TLS Domain ID":        c.filterTLSDomainID,
    60  			"Include":                     c.include,
    61  			"Page Number":                 c.pageNumber,
    62  			"Page Size":                   c.pageSize,
    63  		})
    64  		return err
    65  	}
    66  
    67  	if ok, err := c.WriteJSON(out, o); ok {
    68  		return err
    69  	}
    70  
    71  	if c.Globals.Verbose() {
    72  		c.printVerbose(out, o)
    73  	} else {
    74  		err = c.printSummary(out, o)
    75  		if err != nil {
    76  			return err
    77  		}
    78  	}
    79  	return nil
    80  }
    81  
    82  // constructInput transforms values parsed from CLI flags into an object to be used by the API client library.
    83  func (c *ListCommand) constructInput() *fastly.ListTLSActivationsInput {
    84  	var input fastly.ListTLSActivationsInput
    85  
    86  	if c.filterTLSCertID != "" {
    87  		input.FilterTLSCertificateID = c.filterTLSCertID
    88  	}
    89  	if c.filterTLSConfigID != "" {
    90  		input.FilterTLSConfigurationID = c.filterTLSConfigID
    91  	}
    92  	if c.filterTLSDomainID != "" {
    93  		input.FilterTLSDomainID = c.filterTLSDomainID
    94  	}
    95  	if c.include != "" {
    96  		input.Include = c.include
    97  	}
    98  	if c.pageNumber > 0 {
    99  		input.PageNumber = c.pageNumber
   100  	}
   101  	if c.pageSize > 0 {
   102  		input.PageSize = c.pageSize
   103  	}
   104  
   105  	return &input
   106  }
   107  
   108  // printVerbose displays the information returned from the API in a verbose
   109  // format.
   110  func (c *ListCommand) printVerbose(out io.Writer, rs []*fastly.TLSActivation) {
   111  	for _, r := range rs {
   112  		fmt.Fprintf(out, "\nID: %s\n", r.ID)
   113  
   114  		if r.CreatedAt != nil {
   115  			fmt.Fprintf(out, "Created at: %s\n", r.CreatedAt)
   116  		}
   117  
   118  		fmt.Fprintf(out, "\n")
   119  	}
   120  }
   121  
   122  // printSummary displays the information returned from the API in a summarised
   123  // format.
   124  func (c *ListCommand) printSummary(out io.Writer, rs []*fastly.TLSActivation) error {
   125  	t := text.NewTable(out)
   126  	t.AddHeader("ID", "CREATED_AT")
   127  	for _, r := range rs {
   128  		t.AddLine(r.ID, r.CreatedAt)
   129  	}
   130  	t.Print()
   131  	return nil
   132  }