github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/cmd/juju/environment.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package main
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  
    10  	"launchpad.net/gnuflag"
    11  
    12  	"github.com/juju/juju/cmd"
    13  	"github.com/juju/juju/cmd/envcmd"
    14  	"github.com/juju/juju/juju"
    15  )
    16  
    17  // GetEnvironmentCommand is able to output either the entire environment or
    18  // the requested value in a format of the user's choosing.
    19  type GetEnvironmentCommand struct {
    20  	envcmd.EnvCommandBase
    21  	key string
    22  	out cmd.Output
    23  }
    24  
    25  const getEnvHelpDoc = `
    26  If no extra args passed on the command line, all configuration keys and values
    27  for the environment are output using the selected formatter.
    28  
    29  A single environment value can be output by adding the environment key name to
    30  the end of the command line.
    31  
    32  Example:
    33    
    34    juju get-environment default-series  (returns the default series for the environment)
    35  `
    36  
    37  func (c *GetEnvironmentCommand) Info() *cmd.Info {
    38  	return &cmd.Info{
    39  		Name:    "get-environment",
    40  		Args:    "[<environment key>]",
    41  		Purpose: "view environment values",
    42  		Doc:     strings.TrimSpace(getEnvHelpDoc),
    43  		Aliases: []string{"get-env"},
    44  	}
    45  }
    46  
    47  func (c *GetEnvironmentCommand) SetFlags(f *gnuflag.FlagSet) {
    48  	c.out.AddFlags(f, "smart", cmd.DefaultFormatters)
    49  }
    50  
    51  func (c *GetEnvironmentCommand) Init(args []string) (err error) {
    52  	c.key, err = cmd.ZeroOrOneArgs(args)
    53  	return
    54  }
    55  
    56  func (c *GetEnvironmentCommand) Run(ctx *cmd.Context) error {
    57  	client, err := juju.NewAPIClientFromName(c.EnvName)
    58  	if err != nil {
    59  		return err
    60  	}
    61  	defer client.Close()
    62  
    63  	attrs, err := client.EnvironmentGet()
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	if c.key != "" {
    69  		if value, found := attrs[c.key]; found {
    70  			return c.out.Write(ctx, value)
    71  		}
    72  		return fmt.Errorf("Key %q not found in %q environment.", c.key, attrs["name"])
    73  	}
    74  	// If key is empty, write out the whole lot.
    75  	return c.out.Write(ctx, attrs)
    76  }
    77  
    78  type attributes map[string]interface{}
    79  
    80  // SetEnvironment
    81  type SetEnvironmentCommand struct {
    82  	envcmd.EnvCommandBase
    83  	values attributes
    84  }
    85  
    86  const setEnvHelpDoc = `
    87  Updates the environment of a running Juju instance.  Multiple key/value pairs
    88  can be passed on as command line arguments.
    89  `
    90  
    91  func (c *SetEnvironmentCommand) Info() *cmd.Info {
    92  	return &cmd.Info{
    93  		Name:    "set-environment",
    94  		Args:    "key=[value] ...",
    95  		Purpose: "replace environment values",
    96  		Doc:     strings.TrimSpace(setEnvHelpDoc),
    97  		Aliases: []string{"set-env"},
    98  	}
    99  }
   100  
   101  func (c *SetEnvironmentCommand) Init(args []string) (err error) {
   102  	if len(args) == 0 {
   103  		return fmt.Errorf("No key, value pairs specified")
   104  	}
   105  	// TODO(thumper) look to have a common library of functions for dealing
   106  	// with key=value pairs.
   107  	c.values = make(attributes)
   108  	for i, arg := range args {
   109  		bits := strings.SplitN(arg, "=", 2)
   110  		if len(bits) < 2 {
   111  			return fmt.Errorf(`Missing "=" in arg %d: %q`, i+1, arg)
   112  		}
   113  		key := bits[0]
   114  		if key == "agent-version" {
   115  			return fmt.Errorf("agent-version must be set via upgrade-juju")
   116  		}
   117  		if _, exists := c.values[key]; exists {
   118  			return fmt.Errorf(`Key %q specified more than once`, key)
   119  		}
   120  		c.values[key] = bits[1]
   121  	}
   122  	return nil
   123  }
   124  
   125  func (c *SetEnvironmentCommand) Run(ctx *cmd.Context) error {
   126  	client, err := juju.NewAPIClientFromName(c.EnvName)
   127  	if err != nil {
   128  		return err
   129  	}
   130  	defer client.Close()
   131  	return client.EnvironmentSet(c.values)
   132  }
   133  
   134  // UnsetEnvironment
   135  type UnsetEnvironmentCommand struct {
   136  	envcmd.EnvCommandBase
   137  	keys []string
   138  }
   139  
   140  const unsetEnvHelpDoc = `
   141  Reset one or more the environment configuration attributes to its default
   142  value in a running Juju instance.  Attributes without defaults are removed,
   143  and attempting to remove a required attribute with no default will result
   144  in an error.
   145  
   146  Multiple attributes may be removed at once; keys are space-separated.
   147  `
   148  
   149  func (c *UnsetEnvironmentCommand) Info() *cmd.Info {
   150  	return &cmd.Info{
   151  		Name:    "unset-environment",
   152  		Args:    "<environment key> ...",
   153  		Purpose: "unset environment values",
   154  		Doc:     strings.TrimSpace(unsetEnvHelpDoc),
   155  		Aliases: []string{"unset-env"},
   156  	}
   157  }
   158  
   159  func (c *UnsetEnvironmentCommand) Init(args []string) (err error) {
   160  	if len(args) == 0 {
   161  		return fmt.Errorf("No keys specified")
   162  	}
   163  	c.keys = args
   164  	return nil
   165  }
   166  
   167  func (c *UnsetEnvironmentCommand) Run(ctx *cmd.Context) error {
   168  	client, err := juju.NewAPIClientFromName(c.EnvName)
   169  	if err != nil {
   170  		return err
   171  	}
   172  	defer client.Close()
   173  	return client.EnvironmentUnset(c.keys...)
   174  }