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