OLD | NEW |
1 package jujuc | 1 package jujuc |
2 | 2 |
3 import ( | 3 import ( |
4 "fmt" | 4 "fmt" |
5 "launchpad.net/gnuflag" | 5 "launchpad.net/gnuflag" |
6 "launchpad.net/juju-core/cmd" | 6 "launchpad.net/juju-core/cmd" |
7 ) | 7 ) |
8 | 8 |
9 // RelationListCommand implements the relation-list command. | 9 // RelationListCommand implements the relation-list command. |
10 type RelationListCommand struct { | 10 type RelationListCommand struct { |
11 cmd.CommandBase | 11 cmd.CommandBase |
12 ctx Context | 12 ctx Context |
13 RelationId int | 13 RelationId int |
14 out cmd.Output | 14 out cmd.Output |
15 } | 15 } |
16 | 16 |
17 func NewRelationListCommand(ctx Context) cmd.Command { | 17 func NewRelationListCommand(ctx Context) cmd.Command { |
18 return &RelationListCommand{ctx: ctx} | 18 return &RelationListCommand{ctx: ctx} |
19 } | 19 } |
20 | 20 |
21 func (c *RelationListCommand) Info() *cmd.Info { | 21 func (c *RelationListCommand) Info() *cmd.Info { |
22 » args := "<id>" | 22 » doc := "-r must be specified when not in a relation hook" |
23 » doc := "" | 23 » if _, found := c.ctx.HookRelation(); found { |
24 » if r, found := c.ctx.HookRelation(); found { | 24 » » doc = "" |
25 » » args = "[<id>]" | |
26 » » doc = fmt.Sprintf("Current default relation id is %q.", r.FakeId
()) | |
27 } | 25 } |
28 return &cmd.Info{ | 26 return &cmd.Info{ |
29 Name: "relation-list", | 27 Name: "relation-list", |
30 Args: args, | |
31 Purpose: "list relation units", | 28 Purpose: "list relation units", |
32 Doc: doc, | 29 Doc: doc, |
33 } | 30 } |
34 } | 31 } |
35 | 32 |
36 func (c *RelationListCommand) SetFlags(f *gnuflag.FlagSet) { | 33 func (c *RelationListCommand) SetFlags(f *gnuflag.FlagSet) { |
37 c.out.AddFlags(f, "smart", cmd.DefaultFormatters) | 34 c.out.AddFlags(f, "smart", cmd.DefaultFormatters) |
| 35 f.Var(newRelationIdValue(c.ctx, &c.RelationId), "r", "specify a relation
by id") |
38 } | 36 } |
39 | 37 |
40 func (c *RelationListCommand) Init(args []string) (err error) { | 38 func (c *RelationListCommand) Init(args []string) (err error) { |
41 v := newRelationIdValue(c.ctx, &c.RelationId) | |
42 if len(args) > 0 { | |
43 if err := v.Set(args[0]); err != nil { | |
44 return err | |
45 } | |
46 args = args[1:] | |
47 } | |
48 if c.RelationId == -1 { | 39 if c.RelationId == -1 { |
49 return fmt.Errorf("no relation id specified") | 40 return fmt.Errorf("no relation id specified") |
50 } | 41 } |
51 return cmd.CheckEmpty(args) | 42 return cmd.CheckEmpty(args) |
52 } | 43 } |
53 | 44 |
54 func (c *RelationListCommand) Run(ctx *cmd.Context) error { | 45 func (c *RelationListCommand) Run(ctx *cmd.Context) error { |
55 r, found := c.ctx.Relation(c.RelationId) | 46 r, found := c.ctx.Relation(c.RelationId) |
56 if !found { | 47 if !found { |
57 return fmt.Errorf("unknown relation id") | 48 return fmt.Errorf("unknown relation id") |
58 } | 49 } |
59 return c.out.Write(ctx, r.UnitNames()) | 50 return c.out.Write(ctx, r.UnitNames()) |
60 } | 51 } |
OLD | NEW |