github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/cmd/juju/removeunit.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package main
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/names"
    11  
    12  	"github.com/juju/juju/cmd/envcmd"
    13  	"github.com/juju/juju/cmd/juju/block"
    14  )
    15  
    16  // RemoveUnitCommand is responsible for destroying service units.
    17  type RemoveUnitCommand struct {
    18  	envcmd.EnvCommandBase
    19  	UnitNames []string
    20  }
    21  
    22  func (c *RemoveUnitCommand) Info() *cmd.Info {
    23  	return &cmd.Info{
    24  		Name:    "remove-unit",
    25  		Args:    "<unit> [...]",
    26  		Purpose: "remove service units from the environment",
    27  		Aliases: []string{"destroy-unit"},
    28  	}
    29  }
    30  
    31  func (c *RemoveUnitCommand) Init(args []string) error {
    32  	c.UnitNames = args
    33  	if len(c.UnitNames) == 0 {
    34  		return fmt.Errorf("no units specified")
    35  	}
    36  	for _, name := range c.UnitNames {
    37  		if !names.IsValidUnit(name) {
    38  			return fmt.Errorf("invalid unit name %q", name)
    39  		}
    40  	}
    41  	return nil
    42  }
    43  
    44  // Run connects to the environment specified on the command line and destroys
    45  // units therein.
    46  func (c *RemoveUnitCommand) Run(_ *cmd.Context) error {
    47  	client, err := c.NewAPIClient()
    48  	if err != nil {
    49  		return err
    50  	}
    51  	defer client.Close()
    52  	return block.ProcessBlockedError(client.DestroyServiceUnits(c.UnitNames...), block.BlockRemove)
    53  }