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