github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/plugins/juju-metadata/deleteimage.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package main
     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  func newDeleteImageMetadataCommand() cmd.Command {
    15  	deleteCmd := &deleteImageMetadataCommand{}
    16  	deleteCmd.newAPIFunc = func() (MetadataDeleteAPI, error) {
    17  		return deleteCmd.NewImageMetadataAPI()
    18  	}
    19  	return modelcmd.Wrap(deleteCmd)
    20  }
    21  
    22  const deleteImageCommandDoc = `
    23  Delete image metadata from Juju environment.
    24  
    25  This command takes only one positional argument - an image id.
    26  
    27  arguments:
    28  image-id
    29     image identifier
    30  `
    31  
    32  // deleteImageMetadataCommand deletes image metadata from Juju environment.
    33  type deleteImageMetadataCommand struct {
    34  	cloudImageMetadataCommandBase
    35  
    36  	newAPIFunc func() (MetadataDeleteAPI, error)
    37  
    38  	ImageId string
    39  }
    40  
    41  // Init implements Command.Init.
    42  func (c *deleteImageMetadataCommand) Init(args []string) (err error) {
    43  	if len(args) == 0 {
    44  		return errors.New("image ID must be supplied when deleting image metadata")
    45  	}
    46  	if len(args) != 1 {
    47  		return errors.New("only one image ID can be supplied as an argument to this command")
    48  	}
    49  	c.ImageId = args[0]
    50  	return nil
    51  }
    52  
    53  // Info implements Command.Info.
    54  func (c *deleteImageMetadataCommand) Info() *cmd.Info {
    55  	return &cmd.Info{
    56  		Name:    "delete-image",
    57  		Purpose: "deletes image metadata from environment",
    58  		Doc:     deleteImageCommandDoc,
    59  	}
    60  }
    61  
    62  // SetFlags implements Command.SetFlags.
    63  func (c *deleteImageMetadataCommand) SetFlags(f *gnuflag.FlagSet) {
    64  	c.cloudImageMetadataCommandBase.SetFlags(f)
    65  }
    66  
    67  // Run implements Command.Run.
    68  func (c *deleteImageMetadataCommand) Run(ctx *cmd.Context) (err error) {
    69  	api, err := c.newAPIFunc()
    70  	if err != nil {
    71  		return err
    72  	}
    73  	defer api.Close()
    74  
    75  	err = api.Delete(c.ImageId)
    76  	if err != nil {
    77  		return err
    78  	}
    79  	return nil
    80  }
    81  
    82  // MetadataDeleteAPI defines the API methods that delete image metadata command uses.
    83  type MetadataDeleteAPI interface {
    84  	Close() error
    85  	Delete(imageId string) error
    86  }