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