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

     1  package config
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/fastly/go-fastly/v9/fastly"
     7  
     8  	"github.com/fastly/cli/pkg/argparser"
     9  	"github.com/fastly/cli/pkg/global"
    10  	"github.com/fastly/cli/pkg/text"
    11  )
    12  
    13  // NewUpdateCommand returns a usable command registered under the parent.
    14  func NewUpdateCommand(parent argparser.Registerer, g *global.Data) *UpdateCommand {
    15  	var c UpdateCommand
    16  	c.CmdClause = parent.Command("update", "Update a TLS configuration")
    17  	c.Globals = g
    18  
    19  	// Required.
    20  	c.CmdClause.Flag("id", "Alphanumeric string identifying a TLS configuration").Required().StringVar(&c.id)
    21  	c.CmdClause.Flag("name", "A custom name for your TLS configuration").Required().StringVar(&c.name)
    22  	return &c
    23  }
    24  
    25  // UpdateCommand calls the Fastly API to update an appropriate resource.
    26  type UpdateCommand struct {
    27  	argparser.Base
    28  
    29  	id   string
    30  	name string
    31  }
    32  
    33  // Exec invokes the application logic for the command.
    34  func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error {
    35  	input := c.constructInput()
    36  
    37  	r, err := c.Globals.APIClient.UpdateCustomTLSConfiguration(input)
    38  	if err != nil {
    39  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
    40  			"TLS Configuration ID": c.id,
    41  		})
    42  		return err
    43  	}
    44  
    45  	text.Success(out, "Updated TLS Configuration '%s'", r.ID)
    46  	return nil
    47  }
    48  
    49  // constructInput transforms values parsed from CLI flags into an object to be used by the API client library.
    50  func (c *UpdateCommand) constructInput() *fastly.UpdateCustomTLSConfigurationInput {
    51  	var input fastly.UpdateCustomTLSConfigurationInput
    52  
    53  	input.ID = c.id
    54  	input.Name = c.name
    55  
    56  	return &input
    57  }