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