github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/uniter/runner/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  	"github.com/juju/cmd"
    10  	"github.com/juju/errors"
    11  	"github.com/juju/gnuflag"
    12  
    13  	"github.com/juju/juju/apiserver/params"
    14  )
    15  
    16  // RelationGetCommand implements the relation-get command.
    17  type RelationGetCommand struct {
    18  	cmd.CommandBase
    19  	ctx Context
    20  
    21  	RelationId      int
    22  	relationIdProxy gnuflag.Value
    23  
    24  	Key      string
    25  	UnitName string
    26  	out      cmd.Output
    27  }
    28  
    29  func NewRelationGetCommand(ctx Context) (cmd.Command, error) {
    30  	var err error
    31  	cmd := &RelationGetCommand{ctx: ctx}
    32  	cmd.relationIdProxy, err = newRelationIdValue(ctx, &cmd.RelationId)
    33  	if err != nil {
    34  		return nil, errors.Trace(err)
    35  	}
    36  
    37  	return cmd, nil
    38  }
    39  
    40  // Info is part of the cmd.Command interface.
    41  func (c *RelationGetCommand) Info() *cmd.Info {
    42  	args := "<key> <unit id>"
    43  	doc := `
    44  relation-get prints the value of a unit's relation setting, specified by key.
    45  If no key is given, or if the key is "-", all keys and values will be printed.
    46  `
    47  	// There's nothing we can really do about the error here.
    48  	if name, err := c.ctx.RemoteUnitName(); err == nil {
    49  		args = "[<key> [<unit id>]]"
    50  		doc += fmt.Sprintf("Current default unit id is %q.", name)
    51  	} else if !errors.IsNotFound(err) {
    52  		logger.Errorf("Failed to retrieve remote unit name: %v", err)
    53  	}
    54  	return &cmd.Info{
    55  		Name:    "relation-get",
    56  		Args:    args,
    57  		Purpose: "get relation settings",
    58  		Doc:     doc,
    59  	}
    60  }
    61  
    62  // SetFlags is part of the cmd.Command interface.
    63  func (c *RelationGetCommand) SetFlags(f *gnuflag.FlagSet) {
    64  	c.out.AddFlags(f, "smart", cmd.DefaultFormatters)
    65  	f.Var(c.relationIdProxy, "r", "specify a relation by id")
    66  	f.Var(c.relationIdProxy, "relation", "")
    67  }
    68  
    69  // Init is part of the cmd.Command interface.
    70  func (c *RelationGetCommand) Init(args []string) error {
    71  	if c.RelationId == -1 {
    72  		return fmt.Errorf("no relation id specified")
    73  	}
    74  	c.Key = ""
    75  	if len(args) > 0 {
    76  		if c.Key = args[0]; c.Key == "-" {
    77  			c.Key = ""
    78  		}
    79  		args = args[1:]
    80  	}
    81  	name, err := c.ctx.RemoteUnitName()
    82  	if err == nil {
    83  		c.UnitName = name
    84  	} else if cause := errors.Cause(err); !errors.IsNotFound(cause) {
    85  		return errors.Trace(err)
    86  	}
    87  	if len(args) > 0 {
    88  		c.UnitName = args[0]
    89  		args = args[1:]
    90  	}
    91  	if c.UnitName == "" {
    92  		return fmt.Errorf("no unit id specified")
    93  	}
    94  	return cmd.CheckEmpty(args)
    95  }
    96  
    97  func (c *RelationGetCommand) Run(ctx *cmd.Context) error {
    98  	r, err := c.ctx.Relation(c.RelationId)
    99  	if err != nil {
   100  		return errors.Trace(err)
   101  	}
   102  	var settings params.Settings
   103  	if c.UnitName == c.ctx.UnitName() {
   104  		node, err := r.Settings()
   105  		if err != nil {
   106  			return err
   107  		}
   108  		settings = node.Map()
   109  	} else {
   110  		var err error
   111  		settings, err = r.ReadSettings(c.UnitName)
   112  		if err != nil {
   113  			return err
   114  		}
   115  	}
   116  	if c.Key == "" {
   117  		return c.out.Write(ctx, settings)
   118  	}
   119  	if value, ok := settings[c.Key]; ok {
   120  		return c.out.Write(ctx, value)
   121  	}
   122  	return c.out.Write(ctx, nil)
   123  }