github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/command/namespace_delete.go (about)

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