github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/acl_auth_method_delete.go (about)

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