github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/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) Run(args []string) int {
    41  	flags := c.Meta.FlagSet("acl policy delete", FlagSetClient)
    42  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    43  	if err := flags.Parse(args); err != nil {
    44  		return 1
    45  	}
    46  
    47  	// Check that we got exactly one argument
    48  	args = flags.Args()
    49  	if l := len(args); l != 1 {
    50  		c.Ui.Error(c.Help())
    51  		return 1
    52  	}
    53  
    54  	// Get the policy name
    55  	policyName := args[0]
    56  
    57  	// Get the HTTP client
    58  	client, err := c.Meta.Client()
    59  	if err != nil {
    60  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    61  		return 1
    62  	}
    63  
    64  	// Delete the policy
    65  	_, err = client.ACLPolicies().Delete(policyName, nil)
    66  	if err != nil {
    67  		c.Ui.Error(fmt.Sprintf("Error deleting ACL policy: %s", err))
    68  		return 1
    69  	}
    70  
    71  	c.Ui.Output(fmt.Sprintf("Successfully deleted %s policy!",
    72  		policyName))
    73  	return 0
    74  }