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