github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/cmd/juju/service/get.go (about)

     1  // Copyright 2012-2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package service
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/errors"
     9  	"launchpad.net/gnuflag"
    10  
    11  	"github.com/juju/juju/api/service"
    12  	"github.com/juju/juju/apiserver/params"
    13  	"github.com/juju/juju/cmd/modelcmd"
    14  )
    15  
    16  var usageGetConfigSummary = `
    17  Displays configuration settings for a deployed service.`[1:]
    18  
    19  var usageGetConfigDetails = `
    20  Output includes the name of the charm used to deploy the service and a
    21  listing of the service-specific configuration settings.
    22  See `[1:] + "`juju status`" + ` for service names.
    23  
    24  Examples:
    25      juju get-config mysql
    26      juju get-config mysql-testing
    27  
    28  See also: 
    29      set-config
    30      deploy
    31      status`
    32  
    33  // NewGetCommand returns a command used to get service attributes.
    34  func NewGetCommand() cmd.Command {
    35  	return modelcmd.Wrap(&getCommand{})
    36  }
    37  
    38  // getCommand retrieves the configuration of a service.
    39  type getCommand struct {
    40  	modelcmd.ModelCommandBase
    41  	ServiceName string
    42  	out         cmd.Output
    43  	api         getServiceAPI
    44  }
    45  
    46  func (c *getCommand) Info() *cmd.Info {
    47  	return &cmd.Info{
    48  		Name:    "get-config",
    49  		Args:    "<service name>",
    50  		Purpose: usageGetConfigSummary,
    51  		Doc:     usageGetConfigDetails,
    52  		Aliases: []string{"get-configs"},
    53  	}
    54  }
    55  
    56  func (c *getCommand) SetFlags(f *gnuflag.FlagSet) {
    57  	// TODO(dfc) add json formatting ?
    58  	c.out.AddFlags(f, "yaml", map[string]cmd.Formatter{
    59  		"yaml": cmd.FormatYaml,
    60  	})
    61  }
    62  
    63  func (c *getCommand) Init(args []string) error {
    64  	// TODO(dfc) add --schema-only
    65  	if len(args) == 0 {
    66  		return errors.New("no service name specified")
    67  	}
    68  	c.ServiceName = args[0]
    69  	return cmd.CheckEmpty(args[1:])
    70  }
    71  
    72  // getServiceAPI defines the methods on the client API
    73  // that the service get command calls.
    74  type getServiceAPI interface {
    75  	Close() error
    76  	Get(service string) (*params.ServiceGetResults, error)
    77  }
    78  
    79  func (c *getCommand) getAPI() (getServiceAPI, error) {
    80  	if c.api != nil {
    81  		return c.api, nil
    82  	}
    83  	root, err := c.NewAPIRoot()
    84  	if err != nil {
    85  		return nil, errors.Trace(err)
    86  	}
    87  	return service.NewClient(root), nil
    88  }
    89  
    90  // Run fetches the configuration of the service and formats
    91  // the result as a YAML string.
    92  func (c *getCommand) Run(ctx *cmd.Context) error {
    93  	apiclient, err := c.getAPI()
    94  	if err != nil {
    95  		return err
    96  	}
    97  	defer apiclient.Close()
    98  
    99  	results, err := apiclient.Get(c.ServiceName)
   100  	if err != nil {
   101  		return err
   102  	}
   103  
   104  	resultsMap := map[string]interface{}{
   105  		"service":  results.Service,
   106  		"charm":    results.Charm,
   107  		"settings": results.Config,
   108  	}
   109  	return c.out.Write(ctx, resultsMap)
   110  }