launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/cmd/juju/destroyunit.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  	"errors"
     8  	"fmt"
     9  
    10  	"launchpad.net/juju-core/cmd"
    11  	"launchpad.net/juju-core/juju"
    12  	"launchpad.net/juju-core/names"
    13  )
    14  
    15  // DestroyUnitCommand is responsible for destroying service units.
    16  type DestroyUnitCommand struct {
    17  	cmd.EnvCommandBase
    18  	UnitNames []string
    19  }
    20  
    21  func (c *DestroyUnitCommand) Info() *cmd.Info {
    22  	return &cmd.Info{
    23  		Name:    "destroy-unit",
    24  		Args:    "<unit> [...]",
    25  		Purpose: "destroy service units",
    26  		Aliases: []string{"remove-unit"},
    27  	}
    28  }
    29  
    30  func (c *DestroyUnitCommand) Init(args []string) error {
    31  	c.UnitNames = args
    32  	if len(c.UnitNames) == 0 {
    33  		return errors.New("no units specified")
    34  	}
    35  	for _, name := range c.UnitNames {
    36  		if !names.IsUnit(name) {
    37  			return fmt.Errorf("invalid unit name %q", name)
    38  		}
    39  	}
    40  	return nil
    41  }
    42  
    43  // Run connects to the environment specified on the command line and destroys
    44  // units therein.
    45  func (c *DestroyUnitCommand) Run(_ *cmd.Context) error {
    46  	client, err := juju.NewAPIClientFromName(c.EnvName)
    47  	if err != nil {
    48  		return err
    49  	}
    50  	defer client.Close()
    51  	return client.DestroyServiceUnits(c.UnitNames...)
    52  }