github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/cmd/juju/backups/backups.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  	"io"
     9  
    10  	"github.com/juju/cmd"
    11  	"github.com/juju/errors"
    12  
    13  	"github.com/juju/juju/api/backups"
    14  	"github.com/juju/juju/apiserver/params"
    15  	"github.com/juju/juju/cmd/envcmd"
    16  )
    17  
    18  var backupsDoc = `
    19  "juju backups" is used to manage backups of the state of a juju environment.
    20  `
    21  
    22  const backupsPurpose = "create, manage, and restore backups of juju's state"
    23  
    24  // Command is the top-level command wrapping all backups functionality.
    25  type Command struct {
    26  	cmd.SuperCommand
    27  }
    28  
    29  // NewCommand returns a new backups super-command.
    30  func NewCommand() cmd.Command {
    31  	backupsCmd := Command{
    32  		SuperCommand: *cmd.NewSuperCommand(
    33  			cmd.SuperCommandParams{
    34  				Name:        "backups",
    35  				Doc:         backupsDoc,
    36  				UsagePrefix: "juju",
    37  				Purpose:     backupsPurpose,
    38  			},
    39  		),
    40  	}
    41  	backupsCmd.Register(envcmd.Wrap(&CreateCommand{}))
    42  	backupsCmd.Register(envcmd.Wrap(&InfoCommand{}))
    43  	backupsCmd.Register(envcmd.Wrap(&ListCommand{}))
    44  	backupsCmd.Register(envcmd.Wrap(&DownloadCommand{}))
    45  	backupsCmd.Register(envcmd.Wrap(&UploadCommand{}))
    46  	backupsCmd.Register(envcmd.Wrap(&RemoveCommand{}))
    47  	return &backupsCmd
    48  }
    49  
    50  // APIClient represents the backups API client functionality used by
    51  // the backups command.
    52  type APIClient interface {
    53  	io.Closer
    54  	// Create sends an RPC request to create a new backup.
    55  	Create(notes string) (*params.BackupsMetadataResult, error)
    56  	// Info gets the backup's metadata.
    57  	Info(id string) (*params.BackupsMetadataResult, error)
    58  	// List gets all stored metadata.
    59  	List() (*params.BackupsListResult, error)
    60  	// Download pulls the backup archive file.
    61  	Download(id string) (io.ReadCloser, error)
    62  	// Upload pushes a backup archive to storage.
    63  	Upload(ar io.Reader, meta params.BackupsMetadataResult) (string, error)
    64  	// Remove removes the stored backup.
    65  	Remove(id string) error
    66  }
    67  
    68  // CommandBase is the base type for backups sub-commands.
    69  type CommandBase struct {
    70  	envcmd.EnvCommandBase
    71  }
    72  
    73  // NewAPIClient returns a client for the backups api endpoint.
    74  func (c *CommandBase) NewAPIClient() (APIClient, error) {
    75  	return newAPIClient(c)
    76  }
    77  
    78  var newAPIClient = func(c *CommandBase) (APIClient, error) {
    79  	root, err := c.NewAPIRoot()
    80  	if err != nil {
    81  		return nil, errors.Trace(err)
    82  	}
    83  	return backups.NewClient(root), nil
    84  }
    85  
    86  // dumpMetadata writes the formatted backup metadata to stdout.
    87  func (c *CommandBase) dumpMetadata(ctx *cmd.Context, result *params.BackupsMetadataResult) {
    88  	fmt.Fprintf(ctx.Stdout, "backup ID:       %q\n", result.ID)
    89  	fmt.Fprintf(ctx.Stdout, "checksum:        %q\n", result.Checksum)
    90  	fmt.Fprintf(ctx.Stdout, "checksum format: %q\n", result.ChecksumFormat)
    91  	fmt.Fprintf(ctx.Stdout, "size (B):        %d\n", result.Size)
    92  	fmt.Fprintf(ctx.Stdout, "stored:          %v\n", result.Stored)
    93  
    94  	fmt.Fprintf(ctx.Stdout, "started:         %v\n", result.Started)
    95  	fmt.Fprintf(ctx.Stdout, "finished:        %v\n", result.Finished)
    96  	fmt.Fprintf(ctx.Stdout, "notes:           %q\n", result.Notes)
    97  
    98  	fmt.Fprintf(ctx.Stdout, "environment ID:  %q\n", result.Environment)
    99  	fmt.Fprintf(ctx.Stdout, "machine ID:      %q\n", result.Machine)
   100  	fmt.Fprintf(ctx.Stdout, "created on host: %q\n", result.Hostname)
   101  	fmt.Fprintf(ctx.Stdout, "juju version:    %v\n", result.Version)
   102  }