github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/commands/get.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package commands
     5  
     6  import (
     7  	"errors"
     8  
     9  	"github.com/juju/cmd"
    10  	"launchpad.net/gnuflag"
    11  
    12  	"github.com/juju/juju/cmd/envcmd"
    13  )
    14  
    15  // GetCommand retrieves the configuration of a service.
    16  type GetCommand struct {
    17  	envcmd.EnvCommandBase
    18  	ServiceName string
    19  	out         cmd.Output
    20  }
    21  
    22  const getDoc = `
    23  The command output includes the service and charm names, a detailed list of all config
    24  settings for <service>, including the setting name, whether it uses the default value
    25  or not ("default: true"), description (if set), type, and current value. Example:
    26  
    27  $ juju get wordpress
    28  
    29  charm: wordpress
    30  service: wordpress
    31  settings:
    32    engine:
    33        default: true
    34        description: 'Currently two ...'
    35        type: string
    36        value: nginx
    37     tuning:
    38        description: "This is the tuning level..."
    39        type: string
    40        value: optimized
    41  
    42  NOTE: In the example above the descriptions and most other settings were omitted for
    43  brevity. The "engine" setting was left at its default value ("nginx"), while the
    44  "tuning" setting was set to "optimized" (the default value is "single").
    45  `
    46  
    47  func (c *GetCommand) Info() *cmd.Info {
    48  	return &cmd.Info{
    49  		Name:    "get",
    50  		Args:    "<service>",
    51  		Purpose: "get service configuration options",
    52  		Doc:     getDoc,
    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  // Run fetches the configuration of the service and formats
    73  // the result as a YAML string.
    74  func (c *GetCommand) Run(ctx *cmd.Context) error {
    75  	client, err := c.NewAPIClient()
    76  	if err != nil {
    77  		return err
    78  	}
    79  	defer client.Close()
    80  
    81  	results, err := client.ServiceGet(c.ServiceName)
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	resultsMap := map[string]interface{}{
    87  		"service":  results.Service,
    88  		"charm":    results.Charm,
    89  		"settings": results.Config,
    90  	}
    91  	return c.out.Write(ctx, resultsMap)
    92  }