github.com/manicqin/nomad@v0.9.5/command/acl_policy_delete.go (about)

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