github.com/hernad/nomad@v1.6.112/command/acl_token_delete.go (about)

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