github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/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 "launchpad.net/gnuflag" 12 ) 13 14 const listDoc = ` 15 "list" provides the metadata associated with all backups. 16 ` 17 18 // ListCommand is the sub-command for listing all available backups. 19 type ListCommand struct { 20 CommandBase 21 // Brief means only IDs will be printed. 22 Brief bool 23 } 24 25 // Info implements Command.Info. 26 func (c *ListCommand) Info() *cmd.Info { 27 return &cmd.Info{ 28 Name: "list", 29 Args: "", 30 Purpose: "get all metadata", 31 Doc: listDoc, 32 } 33 } 34 35 // SetFlags implements Command.SetFlags. 36 func (c *ListCommand) SetFlags(f *gnuflag.FlagSet) { 37 f.BoolVar(&c.Brief, "brief", false, "only print IDs") 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 client, err := c.NewAPIClient() 51 if err != nil { 52 return errors.Trace(err) 53 } 54 defer client.Close() 55 56 result, err := client.List() 57 if err != nil { 58 return errors.Trace(err) 59 } 60 61 if len(result.List) == 0 { 62 fmt.Fprintln(ctx.Stdout, "(no backups found)") 63 return nil 64 } 65 66 if c.Brief { 67 fmt.Fprintln(ctx.Stdout, result.List[0].ID) 68 } else { 69 c.dumpMetadata(ctx, &result.List[0]) 70 } 71 for _, resultItem := range result.List[1:] { 72 if c.Brief { 73 fmt.Fprintln(ctx.Stdout, resultItem.ID) 74 } else { 75 fmt.Fprintln(ctx.Stdout) 76 c.dumpMetadata(ctx, &resultItem) 77 } 78 } 79 return nil 80 }