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

     1  package platform
     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  )
    13  
    14  // NewDescribeCommand returns a usable command registered under the parent.
    15  func NewDescribeCommand(parent argparser.Registerer, g *global.Data) *DescribeCommand {
    16  	var c DescribeCommand
    17  	c.CmdClause = parent.Command("describe", "Retrieve a single certificate").Alias("get")
    18  	c.Globals = g
    19  
    20  	// Required.
    21  	c.CmdClause.Flag("id", "Alphanumeric string identifying a TLS bulk certificate").Required().StringVar(&c.id)
    22  
    23  	// Optional.
    24  	c.RegisterFlagBool(c.JSONFlag()) // --json
    25  
    26  	return &c
    27  }
    28  
    29  // DescribeCommand calls the Fastly API to describe an appropriate resource.
    30  type DescribeCommand struct {
    31  	argparser.Base
    32  	argparser.JSONOutput
    33  
    34  	id string
    35  }
    36  
    37  // Exec invokes the application logic for the command.
    38  func (c *DescribeCommand) Exec(_ io.Reader, out io.Writer) error {
    39  	if c.Globals.Verbose() && c.JSONOutput.Enabled {
    40  		return fsterr.ErrInvalidVerboseJSONCombo
    41  	}
    42  
    43  	input := c.constructInput()
    44  
    45  	o, err := c.Globals.APIClient.GetBulkCertificate(input)
    46  	if err != nil {
    47  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
    48  			"TLS Bulk Certificate ID": c.id,
    49  		})
    50  		return err
    51  	}
    52  
    53  	if ok, err := c.WriteJSON(out, o); ok {
    54  		return err
    55  	}
    56  
    57  	return c.print(out, o)
    58  }
    59  
    60  // constructInput transforms values parsed from CLI flags into an object to be used by the API client library.
    61  func (c *DescribeCommand) constructInput() *fastly.GetBulkCertificateInput {
    62  	var input fastly.GetBulkCertificateInput
    63  
    64  	input.ID = c.id
    65  
    66  	return &input
    67  }
    68  
    69  // print displays the information returned from the API.
    70  func (c *DescribeCommand) print(out io.Writer, r *fastly.BulkCertificate) error {
    71  	fmt.Fprintf(out, "\nID: %s\n", r.ID)
    72  
    73  	if r.NotAfter != nil {
    74  		fmt.Fprintf(out, "Not after: %s\n", r.NotAfter)
    75  	}
    76  	if r.NotBefore != nil {
    77  		fmt.Fprintf(out, "Not before: %s\n", r.NotBefore)
    78  	}
    79  	if r.CreatedAt != nil {
    80  		fmt.Fprintf(out, "Created at: %s\n", r.CreatedAt)
    81  	}
    82  	if r.UpdatedAt != nil {
    83  		fmt.Fprintf(out, "Updated at: %s\n", r.UpdatedAt)
    84  	}
    85  
    86  	fmt.Fprintf(out, "Replace: %t\n", r.Replace)
    87  
    88  	return nil
    89  }