github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/backups/list.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 listDoc = ` 16 list-backups provides the metadata associated with all backups. 17 ` 18 19 // NewListCommand returns a command used to list metadata for backups. 20 func NewListCommand() cmd.Command { 21 return modelcmd.Wrap(&listCommand{}) 22 } 23 24 // listCommand is the sub-command for listing all available backups. 25 type listCommand struct { 26 CommandBase 27 } 28 29 // Info implements Command.Info. 30 func (c *listCommand) Info() *cmd.Info { 31 return &cmd.Info{ 32 Name: "list-backups", 33 Args: "", 34 Purpose: "get all metadata", 35 Doc: listDoc, 36 Aliases: []string{"backups"}, 37 } 38 } 39 40 // Init implements Command.Init. 41 func (c *listCommand) Init(args []string) error { 42 if err := cmd.CheckEmpty(args); err != nil { 43 return errors.Trace(err) 44 } 45 return nil 46 } 47 48 // Run implements Command.Run. 49 func (c *listCommand) Run(ctx *cmd.Context) error { 50 if c.Log != nil { 51 if err := c.Log.Start(ctx); err != nil { 52 return err 53 } 54 } 55 client, err := c.NewAPIClient() 56 if err != nil { 57 return errors.Trace(err) 58 } 59 defer client.Close() 60 61 result, err := client.List() 62 if err != nil { 63 return errors.Trace(err) 64 } 65 66 if len(result.List) == 0 { 67 fmt.Fprintln(ctx.Stdout, "(no backups found)") 68 return nil 69 } 70 71 verbose := c.Log != nil && c.Log.Verbose 72 if verbose { 73 c.dumpMetadata(ctx, &result.List[0]) 74 } else { 75 fmt.Fprintln(ctx.Stdout, result.List[0].ID) 76 } 77 for _, resultItem := range result.List[1:] { 78 if verbose { 79 fmt.Fprintln(ctx.Stdout) 80 c.dumpMetadata(ctx, &resultItem) 81 } else { 82 fmt.Fprintln(ctx.Stdout, resultItem.ID) 83 } 84 } 85 return nil 86 }