github.com/sl1pm4t/consul@v1.4.5-0.20190325224627-74c31c540f9c/command/acl/token/clone/token_clone.go (about)

     1  package tokenclone
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  
     7  	"github.com/hashicorp/consul/command/acl"
     8  	"github.com/hashicorp/consul/command/flags"
     9  	"github.com/mitchellh/cli"
    10  )
    11  
    12  func New(ui cli.Ui) *cmd {
    13  	c := &cmd{UI: ui}
    14  	c.init()
    15  	return c
    16  }
    17  
    18  type cmd struct {
    19  	UI    cli.Ui
    20  	flags *flag.FlagSet
    21  	http  *flags.HTTPFlags
    22  	help  string
    23  
    24  	tokenID     string
    25  	description string
    26  }
    27  
    28  func (c *cmd) init() {
    29  	c.flags = flag.NewFlagSet("", flag.ContinueOnError)
    30  	c.flags.StringVar(&c.tokenID, "id", "", "The Accessor ID of the token to clone. "+
    31  		"It may be specified as a unique ID prefix but will error if the prefix "+
    32  		"matches multiple token Accessor IDs. The special value of 'anonymous' may "+
    33  		"be provided instead of the anonymous tokens accessor ID")
    34  	c.flags.StringVar(&c.description, "description", "", "A description of the new cloned token")
    35  	c.http = &flags.HTTPFlags{}
    36  	flags.Merge(c.flags, c.http.ClientFlags())
    37  	flags.Merge(c.flags, c.http.ServerFlags())
    38  	c.help = flags.Usage(help, c.flags)
    39  }
    40  
    41  func (c *cmd) Run(args []string) int {
    42  	if err := c.flags.Parse(args); err != nil {
    43  		return 1
    44  	}
    45  
    46  	if c.tokenID == "" {
    47  		c.UI.Error(fmt.Sprintf("Cannot update a token without specifying the -id parameter"))
    48  		return 1
    49  	}
    50  
    51  	client, err := c.http.APIClient()
    52  	if err != nil {
    53  		c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
    54  		return 1
    55  	}
    56  
    57  	tokenID, err := acl.GetTokenIDFromPartial(client, c.tokenID)
    58  	if err != nil {
    59  		c.UI.Error(fmt.Sprintf("Error determining token ID: %v", err))
    60  		return 1
    61  	}
    62  
    63  	token, _, err := client.ACL().TokenClone(tokenID, c.description, nil)
    64  	if err != nil {
    65  		c.UI.Error(fmt.Sprintf("Error cloning token: %v", err))
    66  		return 1
    67  	}
    68  
    69  	c.UI.Info("Token cloned successfully.")
    70  	acl.PrintToken(token, c.UI, false)
    71  	return 0
    72  }
    73  
    74  func (c *cmd) Synopsis() string {
    75  	return synopsis
    76  }
    77  
    78  func (c *cmd) Help() string {
    79  	return flags.Usage(c.help, nil)
    80  }
    81  
    82  const synopsis = "Clone an ACL Token"
    83  const help = `
    84  Usage: consul acl token clone [options]
    85  
    86      This command will clone a token. When cloning an alternate description may be given
    87      for use with the new token.
    88  
    89      Example:
    90  
    91          $ consul acl token clone -id abcd -description "replication"
    92  `