github.com/hernad/nomad@v1.6.112/command/acl_binding_rule_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/mitchellh/cli"
    11  	"github.com/posener/complete"
    12  )
    13  
    14  // Ensure ACLBindingRuleDeleteCommand satisfies the cli.Command interface.
    15  var _ cli.Command = &ACLBindingRuleDeleteCommand{}
    16  
    17  // ACLBindingRuleDeleteCommand implements cli.Command.
    18  type ACLBindingRuleDeleteCommand struct {
    19  	Meta
    20  }
    21  
    22  // Help satisfies the cli.Command Help function.
    23  func (a *ACLBindingRuleDeleteCommand) Help() string {
    24  	helpText := `
    25  Usage: nomad acl binding-rule delete <acl_binding_rule_id>
    26  
    27    Delete is used to delete an existing ACL binding rule. Use requires a
    28    management token.
    29  
    30  General Options:
    31  
    32    ` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace)
    33  
    34  	return strings.TrimSpace(helpText)
    35  }
    36  
    37  func (a *ACLBindingRuleDeleteCommand) AutocompleteFlags() complete.Flags {
    38  	return mergeAutocompleteFlags(a.Meta.AutocompleteFlags(FlagSetClient),
    39  		complete.Flags{})
    40  }
    41  
    42  func (a *ACLBindingRuleDeleteCommand) AutocompleteArgs() complete.Predictor {
    43  	return complete.PredictNothing
    44  }
    45  
    46  // Synopsis satisfies the cli.Command Synopsis function.
    47  func (a *ACLBindingRuleDeleteCommand) Synopsis() string { return "Delete an existing ACL binding rule" }
    48  
    49  // Name returns the name of this command.
    50  func (a *ACLBindingRuleDeleteCommand) Name() string { return "acl binding-rule delete" }
    51  
    52  // Run satisfies the cli.Command Run function.
    53  func (a *ACLBindingRuleDeleteCommand) Run(args []string) int {
    54  
    55  	flags := a.Meta.FlagSet(a.Name(), FlagSetClient)
    56  	flags.Usage = func() { a.Ui.Output(a.Help()) }
    57  
    58  	if err := flags.Parse(args); err != nil {
    59  		return 1
    60  	}
    61  
    62  	// Check that the last argument is the rule ID to delete.
    63  	if len(flags.Args()) != 1 {
    64  		a.Ui.Error("This command takes one argument: <acl_binding_rule_id>")
    65  		a.Ui.Error(commandErrorText(a))
    66  		return 1
    67  	}
    68  
    69  	aclBindingRuleID := flags.Args()[0]
    70  
    71  	// Get the HTTP client.
    72  	client, err := a.Meta.Client()
    73  	if err != nil {
    74  		a.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    75  		return 1
    76  	}
    77  
    78  	// Delete the specified ACL binding rule.
    79  	_, err = client.ACLBindingRules().Delete(aclBindingRuleID, nil)
    80  	if err != nil {
    81  		a.Ui.Error(fmt.Sprintf("Error deleting ACL binding rule: %s", err))
    82  		return 1
    83  	}
    84  
    85  	// Give some feedback to indicate the deletion was successful.
    86  	a.Ui.Output(fmt.Sprintf("ACL binding rule %s successfully deleted", aclBindingRuleID))
    87  	return 0
    88  }