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

     1  package config
     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  const include = "dns_records"
    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 configuration").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.GetCustomTLSConfiguration(input)
    50  	if err != nil {
    51  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
    52  			"TLS Configuration 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.GetCustomTLSConfigurationInput {
    66  	var input fastly.GetCustomTLSConfigurationInput
    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.CustomTLSConfiguration) error {
    79  	fmt.Fprintf(out, "\nID: %s\n", r.ID)
    80  	fmt.Fprintf(out, "Name: %s\n", r.Name)
    81  
    82  	if len(r.DNSRecords) > 0 {
    83  		for _, v := range r.DNSRecords {
    84  			if v != nil {
    85  				fmt.Fprintf(out, "DNS Record ID: %s\n", v.ID)
    86  				fmt.Fprintf(out, "DNS Record Type: %s\n", v.RecordType)
    87  				fmt.Fprintf(out, "DNS Record Region: %s\n", v.Region)
    88  			}
    89  		}
    90  	}
    91  
    92  	fmt.Fprintf(out, "Bulk: %t\n", r.Bulk)
    93  	fmt.Fprintf(out, "Default: %t\n", r.Default)
    94  
    95  	if len(r.HTTPProtocols) > 0 {
    96  		for _, v := range r.HTTPProtocols {
    97  			fmt.Fprintf(out, "HTTP Protocol: %s\n", v)
    98  		}
    99  	}
   100  
   101  	if len(r.TLSProtocols) > 0 {
   102  		for _, v := range r.TLSProtocols {
   103  			fmt.Fprintf(out, "TLS Protocol: %s\n", v)
   104  		}
   105  	}
   106  
   107  	if r.CreatedAt != nil {
   108  		fmt.Fprintf(out, "Created at: %s\n", r.CreatedAt)
   109  	}
   110  	if r.UpdatedAt != nil {
   111  		fmt.Fprintf(out, "Updated at: %s\n", r.UpdatedAt)
   112  	}
   113  
   114  	return nil
   115  }