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

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