github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/backups/remove.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package backups
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/errors"
    11  
    12  	"github.com/juju/juju/cmd/modelcmd"
    13  )
    14  
    15  const removeDoc = `
    16  remove-backup removes a backup from remote storage.
    17  `
    18  
    19  // NewRemoveCommand returns a command used to remove a
    20  // backup from remote storage.
    21  func NewRemoveCommand() cmd.Command {
    22  	return modelcmd.Wrap(&removeCommand{})
    23  }
    24  
    25  type removeCommand struct {
    26  	CommandBase
    27  	// ID refers to the backup to be removed.
    28  	ID string
    29  }
    30  
    31  // Info implements Command.Info.
    32  func (c *removeCommand) Info() *cmd.Info {
    33  	return &cmd.Info{
    34  		Name:    "remove-backup",
    35  		Args:    "<ID>",
    36  		Purpose: "delete a backup",
    37  		Doc:     removeDoc,
    38  	}
    39  }
    40  
    41  // Init implements Command.Init.
    42  func (c *removeCommand) Init(args []string) error {
    43  	if len(args) == 0 {
    44  		return errors.New("missing ID")
    45  	}
    46  	id, args := args[0], args[1:]
    47  	if err := cmd.CheckEmpty(args); err != nil {
    48  		return errors.Trace(err)
    49  	}
    50  	c.ID = id
    51  	return nil
    52  }
    53  
    54  // Run implements Command.Run.
    55  func (c *removeCommand) Run(ctx *cmd.Context) error {
    56  	if c.Log != nil {
    57  		if err := c.Log.Start(ctx); err != nil {
    58  			return err
    59  		}
    60  	}
    61  	client, err := c.NewAPIClient()
    62  	if err != nil {
    63  		return errors.Trace(err)
    64  	}
    65  	defer client.Close()
    66  
    67  	err = client.Remove(c.ID)
    68  	if err != nil {
    69  		return errors.Trace(err)
    70  	}
    71  
    72  	output := fmt.Sprintf("successfully removed: %v\n", c.ID)
    73  	ctx.Stdout.Write([]byte(output))
    74  	return nil
    75  }