github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/tls/custom/activation/describe.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  )
    13  
    14  var include = []string{"tls_certificate", "tls_configuration", "tls_domain"}
    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 configuration").Alias("get")
    20  	c.Globals = g
    21  
    22  	// Required.
    23  	c.CmdClause.Flag("id", "Alphanumeric string identifying a TLS activation").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.GetTLSActivation(input)
    50  	if err != nil {
    51  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
    52  			"TLS Activation ID": c.id,
    53  		})
    54  		return err
    55  	}
    56  
    57  	if ok, err := c.WriteJSON(out, o); ok {
    58  		return err
    59  	}
    60  
    61  	return c.print(out, o)
    62  }
    63  
    64  // constructInput transforms values parsed from CLI flags into an object to be used by the API client library.
    65  func (c *DescribeCommand) constructInput() *fastly.GetTLSActivationInput {
    66  	var input fastly.GetTLSActivationInput
    67  
    68  	input.ID = c.id
    69  
    70  	if c.include != "" {
    71  		input.Include = &c.include
    72  	}
    73  
    74  	return &input
    75  }
    76  
    77  // print displays the information returned from the API.
    78  func (c *DescribeCommand) print(out io.Writer, r *fastly.TLSActivation) error {
    79  	fmt.Fprintf(out, "\nID: %s\n", r.ID)
    80  
    81  	if r.CreatedAt != nil {
    82  		fmt.Fprintf(out, "Created at: %s\n", r.CreatedAt)
    83  	}
    84  
    85  	return nil
    86  }