github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/backups/show.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  	"github.com/juju/cmd"
     8  	"github.com/juju/errors"
     9  
    10  	jujucmd "github.com/juju/juju/cmd"
    11  	"github.com/juju/juju/cmd/modelcmd"
    12  )
    13  
    14  const showDoc = `
    15  show-backup provides the metadata associated with a backup.
    16  `
    17  
    18  // NewShowCommand returns a command used to show metadata for a backup.
    19  func NewShowCommand() cmd.Command {
    20  	return modelcmd.Wrap(&showCommand{})
    21  }
    22  
    23  // showCommand is the sub-command for creating a new backup.
    24  type showCommand struct {
    25  	CommandBase
    26  	// ID is the backup ID to get.
    27  	ID string
    28  }
    29  
    30  // Info implements Command.Info.
    31  func (c *showCommand) Info() *cmd.Info {
    32  	return jujucmd.Info(&cmd.Info{
    33  		Name:    "show-backup",
    34  		Args:    "<ID>",
    35  		Purpose: "Show metadata for the specified backup.",
    36  		Doc:     showDoc,
    37  	})
    38  }
    39  
    40  // Init implements Command.Init.
    41  func (c *showCommand) Init(args []string) error {
    42  	if len(args) == 0 {
    43  		return errors.New("missing ID")
    44  	}
    45  	id, args := args[0], args[1:]
    46  	if err := cmd.CheckEmpty(args); err != nil {
    47  		return errors.Trace(err)
    48  	}
    49  	c.ID = id
    50  	return nil
    51  }
    52  
    53  // Run implements Command.Run.
    54  func (c *showCommand) Run(ctx *cmd.Context) error {
    55  	if c.Log != nil {
    56  		if err := c.Log.Start(ctx); err != nil {
    57  			return err
    58  		}
    59  	}
    60  	client, err := c.NewAPIClient()
    61  	if err != nil {
    62  		return errors.Trace(err)
    63  	}
    64  	defer client.Close()
    65  
    66  	result, err := client.Info(c.ID)
    67  	if err != nil {
    68  		return errors.Trace(err)
    69  	}
    70  
    71  	c.dumpMetadata(ctx, result)
    72  	return nil
    73  }