github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/controller/listcontrollers.go (about) 1 // Copyright 2015,2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package controller 5 6 import ( 7 "fmt" 8 "strings" 9 10 "github.com/juju/cmd" 11 "github.com/juju/errors" 12 "launchpad.net/gnuflag" 13 14 "github.com/juju/juju/cmd/modelcmd" 15 "github.com/juju/juju/jujuclient" 16 ) 17 18 var helpControllersSummary = ` 19 Lists all controllers.`[1:] 20 21 var helpControllersDetails = ` 22 The output format may be selected with the '--format' option. In the 23 default tabular output, the current controller is marked with an asterisk. 24 25 Examples: 26 juju list-controllers 27 juju list-controllers --format json --output ~/tmp/controllers.json 28 29 See also: 30 list-models 31 show-controller`[1:] 32 33 // NewListControllersCommand returns a command to list registered controllers. 34 func NewListControllersCommand() cmd.Command { 35 cmd := &listControllersCommand{ 36 store: jujuclient.NewFileClientStore(), 37 } 38 return modelcmd.WrapBase(cmd) 39 } 40 41 // Info implements Command.Info 42 func (c *listControllersCommand) Info() *cmd.Info { 43 return &cmd.Info{ 44 Name: "list-controllers", 45 Purpose: helpControllersSummary, 46 Doc: helpControllersDetails, 47 } 48 } 49 50 // SetFlags implements Command.SetFlags. 51 func (c *listControllersCommand) SetFlags(f *gnuflag.FlagSet) { 52 c.JujuCommandBase.SetFlags(f) 53 c.out.AddFlags(f, "tabular", map[string]cmd.Formatter{ 54 "yaml": cmd.FormatYaml, 55 "json": cmd.FormatJson, 56 "tabular": formatControllersListTabular, 57 }) 58 } 59 60 // Run implements Command.Run 61 func (c *listControllersCommand) Run(ctx *cmd.Context) error { 62 controllers, err := c.store.AllControllers() 63 if err != nil { 64 return errors.Annotate(err, "failed to list controllers") 65 } 66 details, errs := c.convertControllerDetails(controllers) 67 if len(errs) > 0 { 68 fmt.Fprintln(ctx.Stderr, strings.Join(errs, "\n")) 69 } 70 currentController, err := modelcmd.ReadCurrentController() 71 if err != nil { 72 return errors.Annotate(err, "getting current controller") 73 } 74 if _, ok := controllers[currentController]; !ok { 75 // TODO(axw) move handling of current-controller to 76 // the jujuclient code, and make sure the file is 77 // kept in-sync with the controllers.yaml file. 78 currentController = "" 79 } 80 controllerSet := ControllerSet{ 81 Controllers: details, 82 CurrentController: currentController, 83 } 84 return c.out.Write(ctx, controllerSet) 85 } 86 87 type listControllersCommand struct { 88 modelcmd.JujuCommandBase 89 90 out cmd.Output 91 store jujuclient.ClientStore 92 }