github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/cachedimages/remove.go (about)

     1  // Copyright 2016 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  	"github.com/juju/gnuflag"
    10  
    11  	"github.com/juju/juju/cmd/modelcmd"
    12  )
    13  
    14  const removeCommandDoc = `
    15  Remove cached os images in the Juju model.
    16  
    17  Images are identified by:
    18    Kind         eg "lxd"
    19    Series       eg "xenial"
    20    Architecture eg "amd64"
    21  
    22  Examples:
    23    # Remove cached lxd image for xenial amd64.
    24    juju remove-cached-images --kind lxd --series xenial --arch amd64
    25  `
    26  
    27  // NewRemoveCommand returns a command used to remove cached images.
    28  func NewRemoveCommand() cmd.Command {
    29  	return modelcmd.Wrap(&removeCommand{})
    30  }
    31  
    32  // removeCommand shows the images in the Juju server.
    33  type removeCommand struct {
    34  	CachedImagesCommandBase
    35  	Kind, Series, Arch string
    36  }
    37  
    38  // Info implements Command.Info.
    39  func (c *removeCommand) Info() *cmd.Info {
    40  	return &cmd.Info{
    41  		Name:    "remove-cached-images",
    42  		Purpose: "Remove cached OS images.",
    43  		Doc:     removeCommandDoc,
    44  	}
    45  }
    46  
    47  // SetFlags implements Command.SetFlags.
    48  func (c *removeCommand) SetFlags(f *gnuflag.FlagSet) {
    49  	c.CachedImagesCommandBase.SetFlags(f)
    50  	f.StringVar(&c.Kind, "kind", "", "The image kind to remove eg lxd")
    51  	f.StringVar(&c.Series, "series", "", "The series of the image to remove eg xenial")
    52  	f.StringVar(&c.Arch, "arch", "", "The architecture of the image to remove eg amd64")
    53  }
    54  
    55  // Init implements Command.Init.
    56  func (c *removeCommand) Init(args []string) (err error) {
    57  	if c.Kind == "" {
    58  		return errors.New("image kind must be specified")
    59  	}
    60  	if c.Series == "" {
    61  		return errors.New("image series must be specified")
    62  	}
    63  	if c.Arch == "" {
    64  		return errors.New("image architecture must be specified")
    65  	}
    66  	return cmd.CheckEmpty(args)
    67  }
    68  
    69  // RemoveImageAPI defines the imagemanager API methods that the remove command uses.
    70  type RemoveImageAPI interface {
    71  	DeleteImage(kind, series, arch string) error
    72  	Close() error
    73  }
    74  
    75  var getRemoveImageAPI = func(p *CachedImagesCommandBase) (RemoveImageAPI, error) {
    76  	return p.NewImagesManagerClient()
    77  }
    78  
    79  // Run implements Command.Run.
    80  func (c *removeCommand) Run(ctx *cmd.Context) (err error) {
    81  	client, err := getRemoveImageAPI(&c.CachedImagesCommandBase)
    82  	if err != nil {
    83  		return err
    84  	}
    85  	defer client.Close()
    86  
    87  	return client.DeleteImage(c.Kind, c.Series, c.Arch)
    88  }