github.com/criteo-forks/consul@v1.4.5-criteonogrpc/command/acl/token/delete/token_delete.go (about)

     1  package tokendelete
     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  }
    26  
    27  func (c *cmd) init() {
    28  	c.flags = flag.NewFlagSet("", flag.ContinueOnError)
    29  	c.flags.StringVar(&c.tokenID, "id", "", "The Accessor ID of the token to delete. "+
    30  		"It may be specified as a unique ID prefix but will error if the prefix "+
    31  		"matches multiple token Accessor IDs")
    32  	c.http = &flags.HTTPFlags{}
    33  	c.http = &flags.HTTPFlags{}
    34  	flags.Merge(c.flags, c.http.ClientFlags())
    35  	flags.Merge(c.flags, c.http.ServerFlags())
    36  	c.help = flags.Usage(help, c.flags)
    37  }
    38  
    39  func (c *cmd) Run(args []string) int {
    40  	if err := c.flags.Parse(args); err != nil {
    41  		return 1
    42  	}
    43  
    44  	if c.tokenID == "" {
    45  		c.UI.Error(fmt.Sprintf("Must specify the -id parameter"))
    46  		return 1
    47  	}
    48  
    49  	client, err := c.http.APIClient()
    50  	if err != nil {
    51  		c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
    52  		return 1
    53  	}
    54  
    55  	tokenID, err := acl.GetTokenIDFromPartial(client, c.tokenID)
    56  	if err != nil {
    57  		c.UI.Error(fmt.Sprintf("Error determining token ID: %v", err))
    58  		return 1
    59  	}
    60  
    61  	if _, err := client.ACL().TokenDelete(tokenID, nil); err != nil {
    62  		c.UI.Error(fmt.Sprintf("Error deleting token %q: %v", tokenID, err))
    63  		return 1
    64  	}
    65  
    66  	c.UI.Info(fmt.Sprintf("Token %q deleted successfully", tokenID))
    67  	return 0
    68  }
    69  
    70  func (c *cmd) Synopsis() string {
    71  	return synopsis
    72  }
    73  
    74  func (c *cmd) Help() string {
    75  	return flags.Usage(c.help, nil)
    76  }
    77  
    78  const synopsis = "Delete an ACL Token"
    79  const help = `
    80  Usage: consul acl token delete [options] -id TOKEN
    81  
    82    Deletes an ACL token by providing either the ID or a unique ID prefix.
    83  
    84        Delete by prefix:
    85  
    86            $ consul acl token delete -id b6b85
    87  
    88        Delete by full ID:
    89  
    90            $ consul acl token delete -id b6b856da-5193-4e78-845a-7d61ca8371ba
    91  `