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

     1  package certificate
     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 certificates")
    21  	c.Globals = g
    22  
    23  	// Optional.
    24  	c.CmdClause.Flag("filter-not-after", "Limit the returned certificates to those that expire prior to the specified date in UTC").StringVar(&c.filterNotAfter)
    25  	c.CmdClause.Flag("filter-domain", "Limit the returned certificates to those that include the specific domain").StringVar(&c.filterTLSDomainID)
    26  	c.CmdClause.Flag("include", "Include related objects (comma-separated values)").HintOptions("tls_activations").EnumVar(&c.include, "tls_activations")
    27  	c.RegisterFlagBool(c.JSONFlag()) // --json
    28  	c.CmdClause.Flag("page", "Page number of data set to fetch").IntVar(&c.pageNumber)
    29  	c.CmdClause.Flag("per-page", "Number of records per page").IntVar(&c.pageSize)
    30  	c.CmdClause.Flag("sort", "The order in which to list the results by creation date").StringVar(&c.sort)
    31  
    32  	return &c
    33  }
    34  
    35  // ListCommand calls the Fastly API to list appropriate resources.
    36  type ListCommand struct {
    37  	argparser.Base
    38  	argparser.JSONOutput
    39  
    40  	filterNotAfter    string
    41  	filterTLSDomainID string
    42  	include           string
    43  	pageNumber        int
    44  	pageSize          int
    45  	sort              string
    46  }
    47  
    48  // Exec invokes the application logic for the command.
    49  func (c *ListCommand) Exec(_ io.Reader, out io.Writer) error {
    50  	if c.Globals.Verbose() && c.JSONOutput.Enabled {
    51  		return fsterr.ErrInvalidVerboseJSONCombo
    52  	}
    53  
    54  	input := c.constructInput()
    55  
    56  	o, err := c.Globals.APIClient.ListCustomTLSCertificates(input)
    57  	if err != nil {
    58  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
    59  			"Filter Not After":     c.filterNotAfter,
    60  			"Filter TLS Domain ID": c.filterTLSDomainID,
    61  			"Include":              c.include,
    62  			"Page Number":          c.pageNumber,
    63  			"Page Size":            c.pageSize,
    64  			"Sort":                 c.sort,
    65  		})
    66  		return err
    67  	}
    68  
    69  	if ok, err := c.WriteJSON(out, o); ok {
    70  		return err
    71  	}
    72  
    73  	if c.Globals.Verbose() {
    74  		printVerbose(out, o)
    75  	} else {
    76  		err = c.printSummary(out, o)
    77  		if err != nil {
    78  			return err
    79  		}
    80  	}
    81  	return nil
    82  }
    83  
    84  // constructInput transforms values parsed from CLI flags into an object to be used by the API client library.
    85  func (c *ListCommand) constructInput() *fastly.ListCustomTLSCertificatesInput {
    86  	var input fastly.ListCustomTLSCertificatesInput
    87  
    88  	if c.filterNotAfter != emptyString {
    89  		input.FilterNotAfter = c.filterNotAfter
    90  	}
    91  	if c.filterTLSDomainID != emptyString {
    92  		input.FilterTLSDomainsID = c.filterTLSDomainID
    93  	}
    94  	if c.include != emptyString {
    95  		input.Include = c.include
    96  	}
    97  	if c.pageNumber > 0 {
    98  		input.PageNumber = c.pageNumber
    99  	}
   100  	if c.pageSize > 0 {
   101  		input.PageSize = c.pageSize
   102  	}
   103  	if c.sort != "" {
   104  		input.Sort = c.sort
   105  	}
   106  
   107  	return &input
   108  }
   109  
   110  // printVerbose displays the information returned from the API in a verbose
   111  // format.
   112  func printVerbose(out io.Writer, rs []*fastly.CustomTLSCertificate) {
   113  	for _, r := range rs {
   114  		fmt.Fprintf(out, "ID: %s\n", r.ID)
   115  		fmt.Fprintf(out, "Issued to: %s\n", r.IssuedTo)
   116  		fmt.Fprintf(out, "Issuer: %s\n", r.Issuer)
   117  		fmt.Fprintf(out, "Name: %s\n", r.Name)
   118  
   119  		if r.NotAfter != nil {
   120  			fmt.Fprintf(out, "Not after: %s\n", r.NotAfter)
   121  		}
   122  		if r.NotBefore != nil {
   123  			fmt.Fprintf(out, "Not before: %s\n", r.NotBefore)
   124  		}
   125  
   126  		fmt.Fprintf(out, "Replace: %t\n", r.Replace)
   127  		fmt.Fprintf(out, "Serial number: %s\n", r.SerialNumber)
   128  		fmt.Fprintf(out, "Signature algorithm: %s\n", r.SignatureAlgorithm)
   129  
   130  		if r.CreatedAt != nil {
   131  			fmt.Fprintf(out, "Created at: %s\n", r.CreatedAt)
   132  		}
   133  		if r.UpdatedAt != nil {
   134  			fmt.Fprintf(out, "Updated at: %s\n", r.UpdatedAt)
   135  		}
   136  
   137  		fmt.Fprintf(out, "\n")
   138  	}
   139  }
   140  
   141  // printSummary displays the information returned from the API in a summarised
   142  // format.
   143  func (c *ListCommand) printSummary(out io.Writer, rs []*fastly.CustomTLSCertificate) error {
   144  	t := text.NewTable(out)
   145  	t.AddHeader("ID", "ISSUED TO", "NAME", "REPLACE", "SIGNATURE ALGORITHM")
   146  	for _, r := range rs {
   147  		t.AddLine(r.ID, r.IssuedTo, r.Name, r.Replace, r.SignatureAlgorithm)
   148  	}
   149  	t.Print()
   150  	return nil
   151  }