launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/worker/uniter/jujuc/relation-get.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujuc
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"launchpad.net/errgo/errors"
    10  	"launchpad.net/gnuflag"
    11  
    12  	"launchpad.net/juju-core/cmd"
    13  	"launchpad.net/juju-core/state/api/params"
    14  )
    15  
    16  // RelationGetCommand implements the relation-get command.
    17  type RelationGetCommand struct {
    18  	cmd.CommandBase
    19  	ctx        Context
    20  	RelationId int
    21  	Key        string
    22  	UnitName   string
    23  	out        cmd.Output
    24  }
    25  
    26  func NewRelationGetCommand(ctx Context) cmd.Command {
    27  	return &RelationGetCommand{ctx: ctx}
    28  }
    29  
    30  func (c *RelationGetCommand) Info() *cmd.Info {
    31  	args := "<key> <unit id>"
    32  	doc := `
    33  relation-get prints the value of a unit's relation setting, specified by key.
    34  If no key is given, or if the key is "-", all keys and values will be printed.
    35  `
    36  	if name, found := c.ctx.RemoteUnitName(); found {
    37  		args = "[<key> [<unit id>]]"
    38  		doc += fmt.Sprintf("Current default unit id is %q.", name)
    39  	}
    40  	return &cmd.Info{
    41  		Name:    "relation-get",
    42  		Args:    args,
    43  		Purpose: "get relation settings",
    44  		Doc:     doc,
    45  	}
    46  }
    47  
    48  func (c *RelationGetCommand) SetFlags(f *gnuflag.FlagSet) {
    49  	c.out.AddFlags(f, "smart", cmd.DefaultFormatters)
    50  	f.Var(newRelationIdValue(c.ctx, &c.RelationId), "r", "specify a relation by id")
    51  }
    52  
    53  func (c *RelationGetCommand) Init(args []string) error {
    54  	if c.RelationId == -1 {
    55  		return errors.Newf("no relation id specified")
    56  	}
    57  	c.Key = ""
    58  	if len(args) > 0 {
    59  		if c.Key = args[0]; c.Key == "-" {
    60  			c.Key = ""
    61  		}
    62  		args = args[1:]
    63  	}
    64  	if name, found := c.ctx.RemoteUnitName(); found {
    65  		c.UnitName = name
    66  	}
    67  	if len(args) > 0 {
    68  		c.UnitName = args[0]
    69  		args = args[1:]
    70  	}
    71  	if c.UnitName == "" {
    72  		return errors.Newf("no unit id specified")
    73  	}
    74  	return cmd.CheckEmpty(args)
    75  }
    76  
    77  func (c *RelationGetCommand) Run(ctx *cmd.Context) error {
    78  	r, found := c.ctx.Relation(c.RelationId)
    79  	if !found {
    80  		return errors.Newf("unknown relation id")
    81  	}
    82  	var settings params.RelationSettings
    83  	if c.UnitName == c.ctx.UnitName() {
    84  		node, err := r.Settings()
    85  		if err != nil {
    86  			return mask(err)
    87  		}
    88  		settings = node.Map()
    89  	} else {
    90  		var err error
    91  		settings, err = r.ReadSettings(c.UnitName)
    92  		if err != nil {
    93  			return mask(err)
    94  		}
    95  	}
    96  	if c.Key == "" {
    97  		return c.out.Write(ctx, settings)
    98  	}
    99  	if value, ok := settings[c.Key]; ok {
   100  		return c.out.Write(ctx, value)
   101  	}
   102  	return c.out.Write(ctx, nil)
   103  }