github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/command/acl_token_delete.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/posener/complete"
     8  )
     9  
    10  type ACLTokenDeleteCommand struct {
    11  	Meta
    12  }
    13  
    14  func (c *ACLTokenDeleteCommand) Help() string {
    15  	helpText := `
    16  Usage: nomad acl token delete <token_accessor_id>
    17  
    18    Delete is used to delete an existing ACL token. Requires a management token.
    19  
    20  General Options:
    21  
    22    ` + generalOptionsUsage()
    23  
    24  	return strings.TrimSpace(helpText)
    25  }
    26  
    27  func (c *ACLTokenDeleteCommand) AutocompleteFlags() complete.Flags {
    28  	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
    29  		complete.Flags{})
    30  }
    31  
    32  func (c *ACLTokenDeleteCommand) AutocompleteArgs() complete.Predictor {
    33  	return complete.PredictNothing
    34  }
    35  
    36  func (c *ACLTokenDeleteCommand) Synopsis() string {
    37  	return "Delete an existing ACL token"
    38  }
    39  
    40  func (c *ACLTokenDeleteCommand) Name() string { return "acl token delete" }
    41  
    42  func (c *ACLTokenDeleteCommand) Run(args []string) int {
    43  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    44  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    45  
    46  	if err := flags.Parse(args); err != nil {
    47  		return 1
    48  	}
    49  
    50  	// Check that the last argument is the token to delete. Return error if no
    51  	// such token was provided.
    52  	args = flags.Args()
    53  	if l := len(args); l != 1 {
    54  		c.Ui.Error("This command takes one argument: <token_accessor_id>")
    55  		c.Ui.Error(commandErrorText(c))
    56  		return 1
    57  	}
    58  
    59  	tokenAccessorID := args[0]
    60  
    61  	// Get the HTTP client
    62  	client, err := c.Meta.Client()
    63  	if err != nil {
    64  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    65  		return 1
    66  	}
    67  
    68  	// Delete the specified token
    69  	_, err = client.ACLTokens().Delete(tokenAccessorID, nil)
    70  	if err != nil {
    71  		c.Ui.Error(fmt.Sprintf("Error deleting token: %s", err))
    72  		return 1
    73  	}
    74  
    75  	// Format the output
    76  	c.Ui.Output(fmt.Sprintf("Token %s successfully deleted", tokenAccessorID))
    77  	return 0
    78  }