github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/worker/uniter/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 "launchpad.net/gnuflag" 10 11 "github.com/juju/juju/cmd" 12 ) 13 14 // RelationListCommand implements the relation-list command. 15 type RelationListCommand struct { 16 cmd.CommandBase 17 ctx Context 18 RelationId int 19 out cmd.Output 20 } 21 22 func NewRelationListCommand(ctx Context) cmd.Command { 23 return &RelationListCommand{ctx: ctx} 24 } 25 26 func (c *RelationListCommand) Info() *cmd.Info { 27 doc := "-r must be specified when not in a relation hook" 28 if _, found := c.ctx.HookRelation(); found { 29 doc = "" 30 } 31 return &cmd.Info{ 32 Name: "relation-list", 33 Purpose: "list relation units", 34 Doc: doc, 35 } 36 } 37 38 func (c *RelationListCommand) SetFlags(f *gnuflag.FlagSet) { 39 c.out.AddFlags(f, "smart", cmd.DefaultFormatters) 40 f.Var(newRelationIdValue(c.ctx, &c.RelationId), "r", "specify a relation by id") 41 } 42 43 func (c *RelationListCommand) Init(args []string) (err error) { 44 if c.RelationId == -1 { 45 return fmt.Errorf("no relation id specified") 46 } 47 return cmd.CheckEmpty(args) 48 } 49 50 func (c *RelationListCommand) Run(ctx *cmd.Context) error { 51 r, found := c.ctx.Relation(c.RelationId) 52 if !found { 53 return fmt.Errorf("unknown relation id") 54 } 55 unitNames := r.UnitNames() 56 if unitNames == nil { 57 unitNames = []string{} 58 } 59 return c.out.Write(ctx, unitNames) 60 }