github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/cmd/juju/controller/getconfig.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package controller
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/errors"
    11  	"github.com/juju/gnuflag"
    12  
    13  	apicontroller "github.com/juju/juju/api/controller"
    14  	"github.com/juju/juju/cmd/modelcmd"
    15  	"github.com/juju/juju/cmd/output"
    16  	"github.com/juju/juju/controller"
    17  )
    18  
    19  func NewGetConfigCommand() cmd.Command {
    20  	return modelcmd.WrapController(&getConfigCommand{})
    21  }
    22  
    23  // getConfigCommand is able to output either the entire environment or
    24  // the requested value in a format of the user's choosing.
    25  type getConfigCommand struct {
    26  	modelcmd.ControllerCommandBase
    27  	api controllerAPI
    28  	key string
    29  	out cmd.Output
    30  }
    31  
    32  const getControllerHelpDoc = `
    33  By default, all configuration (keys and values) for the controller are
    34  displayed if a key is not specified.
    35  
    36  Examples:
    37  
    38      juju controller-config
    39      juju controller-config api-port
    40      juju controller-config -c mycontroller
    41  
    42  See also:
    43      controllers
    44  `
    45  
    46  func (c *getConfigCommand) Info() *cmd.Info {
    47  	return &cmd.Info{
    48  		Name:    "controller-config",
    49  		Args:    "[<attribute key>]",
    50  		Purpose: "Displays configuration settings for a controller.",
    51  		Doc:     strings.TrimSpace(getControllerHelpDoc),
    52  	}
    53  }
    54  
    55  func (c *getConfigCommand) SetFlags(f *gnuflag.FlagSet) {
    56  	c.ControllerCommandBase.SetFlags(f)
    57  	c.out.AddFlags(f, "yaml", output.DefaultFormatters)
    58  }
    59  
    60  func (c *getConfigCommand) Init(args []string) (err error) {
    61  	c.key, err = cmd.ZeroOrOneArgs(args)
    62  	return
    63  }
    64  
    65  type controllerAPI interface {
    66  	Close() error
    67  	ControllerConfig() (controller.Config, error)
    68  }
    69  
    70  func (c *getConfigCommand) getAPI() (controllerAPI, error) {
    71  	if c.api != nil {
    72  		return c.api, nil
    73  	}
    74  	root, err := c.NewAPIRoot()
    75  	if err != nil {
    76  		return nil, errors.Trace(err)
    77  	}
    78  	return apicontroller.NewClient(root), nil
    79  }
    80  
    81  func (c *getConfigCommand) Run(ctx *cmd.Context) error {
    82  	client, err := c.getAPI()
    83  	if err != nil {
    84  		return err
    85  	}
    86  	defer client.Close()
    87  
    88  	attrs, err := client.ControllerConfig()
    89  	if err != nil {
    90  		return err
    91  	}
    92  
    93  	if c.key != "" {
    94  		if value, found := attrs[c.key]; found {
    95  			return c.out.Write(ctx, value)
    96  		}
    97  		return errors.Errorf("key %q not found in %q controller.", c.key, c.ControllerName())
    98  	}
    99  	// If key is empty, write out the whole lot.
   100  	return c.out.Write(ctx, attrs)
   101  }