github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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  	"errors"
     8  
     9  	"github.com/juju/cmd"
    10  	"launchpad.net/gnuflag"
    11  
    12  	"github.com/juju/juju/apiserver/params"
    13  	"github.com/juju/juju/cmd/envcmd"
    14  )
    15  
    16  // GetCommand retrieves the configuration of a service.
    17  type GetCommand struct {
    18  	envcmd.EnvCommandBase
    19  	ServiceName string
    20  	out         cmd.Output
    21  	api         GetServiceAPI
    22  }
    23  
    24  const getDoc = `
    25  The command output includes the service and charm names, a detailed list of all config
    26  settings for <service>, including the setting name, whether it uses the default value
    27  or not ("default: true"), description (if set), type, and current value. Example:
    28  
    29  $ juju service get wordpress
    30  
    31  charm: wordpress
    32  service: wordpress
    33  settings:
    34    engine:
    35        default: true
    36        description: 'Currently two ...'
    37        type: string
    38        value: nginx
    39     tuning:
    40        description: "This is the tuning level..."
    41        type: string
    42        value: optimized
    43  
    44  NOTE: In the example above the descriptions and most other settings were omitted or
    45  truncated for brevity. The "engine" setting was left at its default value ("nginx"),
    46  while the "tuning" setting was set to "optimized" (the default value is "single").
    47  
    48  Note that the "default" field indicates whether a configuration setting is at its
    49  default value. It does not indicate the default value for the setting.
    50  `
    51  
    52  func (c *GetCommand) Info() *cmd.Info {
    53  	return &cmd.Info{
    54  		Name:    "get",
    55  		Args:    "<service>",
    56  		Purpose: "get service configuration options",
    57  		Doc:     getDoc,
    58  	}
    59  }
    60  
    61  func (c *GetCommand) SetFlags(f *gnuflag.FlagSet) {
    62  	// TODO(dfc) add json formatting ?
    63  	c.out.AddFlags(f, "yaml", map[string]cmd.Formatter{
    64  		"yaml": cmd.FormatYaml,
    65  	})
    66  }
    67  
    68  func (c *GetCommand) Init(args []string) error {
    69  	// TODO(dfc) add --schema-only
    70  	if len(args) == 0 {
    71  		return errors.New("no service name specified")
    72  	}
    73  	c.ServiceName = args[0]
    74  	return cmd.CheckEmpty(args[1:])
    75  }
    76  
    77  // GetServiceAPI defines the methods on the client API
    78  // that the service get command calls.
    79  type GetServiceAPI interface {
    80  	Close() error
    81  	ServiceGet(service string) (*params.ServiceGetResults, error)
    82  }
    83  
    84  func (c *GetCommand) getAPI() (GetServiceAPI, error) {
    85  	if c.api != nil {
    86  		return c.api, nil
    87  	}
    88  	return c.NewAPIClient()
    89  }
    90  
    91  // Run fetches the configuration of the service and formats
    92  // the result as a YAML string.
    93  func (c *GetCommand) Run(ctx *cmd.Context) error {
    94  	client, err := c.getAPI()
    95  	if err != nil {
    96  		return err
    97  	}
    98  	defer client.Close()
    99  
   100  	results, err := client.ServiceGet(c.ServiceName)
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	resultsMap := map[string]interface{}{
   106  		"service":  results.Service,
   107  		"charm":    results.Charm,
   108  		"settings": results.Config,
   109  	}
   110  	return c.out.Write(ctx, resultsMap)
   111  }