github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/runner/jujuc/relation-list.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 jujucmd "github.com/juju/juju/cmd" 14 ) 15 16 // RelationListCommand implements the relation-list command. 17 type RelationListCommand struct { 18 cmd.CommandBase 19 ctx Context 20 RelationId int 21 relationIdProxy gnuflag.Value 22 out cmd.Output 23 } 24 25 func NewRelationListCommand(ctx Context) (cmd.Command, error) { 26 c := &RelationListCommand{ctx: ctx} 27 28 rV, err := NewRelationIdValue(c.ctx, &c.RelationId) 29 if err != nil { 30 return nil, errors.Trace(err) 31 } 32 c.relationIdProxy = rV 33 34 return c, nil 35 36 } 37 38 func (c *RelationListCommand) Info() *cmd.Info { 39 doc := "-r must be specified when not in a relation hook" 40 if _, err := c.ctx.HookRelation(); err == nil { 41 doc = "" 42 } 43 return jujucmd.Info(&cmd.Info{ 44 Name: "relation-list", 45 Purpose: "list relation units", 46 Doc: doc, 47 }) 48 } 49 50 func (c *RelationListCommand) SetFlags(f *gnuflag.FlagSet) { 51 c.out.AddFlags(f, "smart", cmd.DefaultFormatters) 52 f.Var(c.relationIdProxy, "r", "specify a relation by id") 53 f.Var(c.relationIdProxy, "relation", "") 54 } 55 56 func (c *RelationListCommand) Init(args []string) (err error) { 57 if c.RelationId == -1 { 58 return fmt.Errorf("no relation id specified") 59 } 60 return cmd.CheckEmpty(args) 61 } 62 63 func (c *RelationListCommand) Run(ctx *cmd.Context) error { 64 r, err := c.ctx.Relation(c.RelationId) 65 if err != nil { 66 return errors.Trace(err) 67 } 68 unitNames := r.UnitNames() 69 if unitNames == nil { 70 unitNames = []string{} 71 } 72 return c.out.Write(ctx, unitNames) 73 }