github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/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 for
    45  brevity. The "engine" setting was left at its default value ("nginx"), while the
    46  "tuning" setting was set to "optimized" (the default value is "single").
    47  `
    48  
    49  func (c *GetCommand) Info() *cmd.Info {
    50  	return &cmd.Info{
    51  		Name:    "get",
    52  		Args:    "<service>",
    53  		Purpose: "get service configuration options",
    54  		Doc:     getDoc,
    55  	}
    56  }
    57  
    58  func (c *GetCommand) SetFlags(f *gnuflag.FlagSet) {
    59  	// TODO(dfc) add json formatting ?
    60  	c.out.AddFlags(f, "yaml", map[string]cmd.Formatter{
    61  		"yaml": cmd.FormatYaml,
    62  	})
    63  }
    64  
    65  func (c *GetCommand) Init(args []string) error {
    66  	// TODO(dfc) add --schema-only
    67  	if len(args) == 0 {
    68  		return errors.New("no service name specified")
    69  	}
    70  	c.ServiceName = args[0]
    71  	return cmd.CheckEmpty(args[1:])
    72  }
    73  
    74  // GetServiceAPI defines the methods on the client API
    75  // that the service get command calls.
    76  type GetServiceAPI interface {
    77  	Close() error
    78  	ServiceGet(service string) (*params.ServiceGetResults, error)
    79  }
    80  
    81  func (c *GetCommand) getAPI() (GetServiceAPI, error) {
    82  	if c.api != nil {
    83  		return c.api, nil
    84  	}
    85  	return c.NewAPIClient()
    86  }
    87  
    88  // Run fetches the configuration of the service and formats
    89  // the result as a YAML string.
    90  func (c *GetCommand) Run(ctx *cmd.Context) error {
    91  	client, err := c.getAPI()
    92  	if err != nil {
    93  		return err
    94  	}
    95  	defer client.Close()
    96  
    97  	results, err := client.ServiceGet(c.ServiceName)
    98  	if err != nil {
    99  		return err
   100  	}
   101  
   102  	resultsMap := map[string]interface{}{
   103  		"service":  results.Service,
   104  		"charm":    results.Charm,
   105  		"settings": results.Config,
   106  	}
   107  	return c.out.Write(ctx, resultsMap)
   108  }