github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/space/remove.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package space
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/errors"
    11  	"gopkg.in/juju/names.v2"
    12  
    13  	jujucmd "github.com/juju/juju/cmd"
    14  	"github.com/juju/juju/cmd/modelcmd"
    15  )
    16  
    17  // NewRemoveCommand returns a command used to remove a space.
    18  func NewRemoveCommand() modelcmd.ModelCommand {
    19  	return modelcmd.Wrap(&RemoveCommand{})
    20  }
    21  
    22  // RemoveCommand calls the API to remove an existing network space.
    23  type RemoveCommand struct {
    24  	SpaceCommandBase
    25  	name string
    26  }
    27  
    28  const removeCommandDoc = `
    29  Removes an existing Juju network space with the given name. Any subnets
    30  associated with the space will be transferred to the default space.
    31  `
    32  
    33  // Info is defined on the cmd.Command interface.
    34  func (c *RemoveCommand) Info() *cmd.Info {
    35  	return jujucmd.Info(&cmd.Info{
    36  		Name:    "remove-space",
    37  		Args:    "<name>",
    38  		Purpose: "Remove a network space",
    39  		Doc:     strings.TrimSpace(removeCommandDoc),
    40  	})
    41  }
    42  
    43  // Init is defined on the cmd.Command interface. It checks the
    44  // arguments for sanity and sets up the command to run.
    45  func (c *RemoveCommand) Init(args []string) (err error) {
    46  	defer errors.DeferredAnnotatef(&err, "invalid arguments specified")
    47  
    48  	// Validate given name.
    49  	if len(args) == 0 {
    50  		return errors.New("space name is required")
    51  	}
    52  	givenName := args[0]
    53  	if !names.IsValidSpace(givenName) {
    54  		return errors.Errorf("%q is not a valid space name", givenName)
    55  	}
    56  	c.name = givenName
    57  
    58  	return cmd.CheckEmpty(args[1:])
    59  }
    60  
    61  // Run implements Command.Run.
    62  func (c *RemoveCommand) Run(ctx *cmd.Context) error {
    63  	return c.RunWithAPI(ctx, func(api SpaceAPI, ctx *cmd.Context) error {
    64  		// Remove the space.
    65  		err := api.RemoveSpace(c.name)
    66  		if err != nil {
    67  			return errors.Annotatef(err, "cannot remove space %q", c.name)
    68  		}
    69  		ctx.Infof("removed space %q", c.name)
    70  		return nil
    71  	})
    72  }