github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cmd/juju/cachedimages/delete.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for infos.
     3  
     4  package cachedimages
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/errors"
     9  	"launchpad.net/gnuflag"
    10  )
    11  
    12  const DeleteCommandDoc = `
    13  Delete cached os images in the Juju environment.
    14  
    15  Images are identified by:
    16    Kind         eg "lxc"
    17    Series       eg "trusty"
    18    Architecture eg "amd64"
    19  
    20  Examples:
    21  
    22    # Delete cached lxc image for trusty amd64.
    23    juju cache-images delete --kind lxc --series trusty --arch amd64
    24  `
    25  
    26  // DeleteCommand shows the images in the Juju server.
    27  type DeleteCommand struct {
    28  	CachedImagesCommandBase
    29  	Kind, Series, Arch string
    30  }
    31  
    32  // Info implements Command.Info.
    33  func (c *DeleteCommand) Info() *cmd.Info {
    34  	return &cmd.Info{
    35  		Name:    "delete",
    36  		Purpose: "delete cached os images",
    37  		Doc:     DeleteCommandDoc,
    38  	}
    39  }
    40  
    41  // SetFlags implements Command.SetFlags.
    42  func (c *DeleteCommand) SetFlags(f *gnuflag.FlagSet) {
    43  	c.CachedImagesCommandBase.SetFlags(f)
    44  	f.StringVar(&c.Kind, "kind", "", "the image kind to delete eg lxc")
    45  	f.StringVar(&c.Series, "series", "", "the series of the image to delete eg trusty")
    46  	f.StringVar(&c.Arch, "arch", "", "the architecture of the image to delete eg amd64")
    47  }
    48  
    49  // Init implements Command.Init.
    50  func (c *DeleteCommand) Init(args []string) (err error) {
    51  	if c.Kind == "" {
    52  		return errors.New("image kind must be specified")
    53  	}
    54  	if c.Series == "" {
    55  		return errors.New("image series must be specified")
    56  	}
    57  	if c.Arch == "" {
    58  		return errors.New("image architecture must be specified")
    59  	}
    60  	return cmd.CheckEmpty(args)
    61  }
    62  
    63  // DeleteImageAPI defines the imagemanager API methods that the delete command uses.
    64  type DeleteImageAPI interface {
    65  	DeleteImage(kind, series, arch string) error
    66  	Close() error
    67  }
    68  
    69  var getDeleteImageAPI = func(p *DeleteCommand) (DeleteImageAPI, error) {
    70  	return p.NewImagesManagerClient()
    71  }
    72  
    73  // Run implements Command.Run.
    74  func (c *DeleteCommand) Run(ctx *cmd.Context) (err error) {
    75  	client, err := getDeleteImageAPI(c)
    76  	if err != nil {
    77  		return err
    78  	}
    79  	defer client.Close()
    80  
    81  	return client.DeleteImage(c.Kind, c.Series, c.Arch)
    82  }