-
Notifications
You must be signed in to change notification settings - Fork 491
/
relation-list.go
73 lines (60 loc) · 1.63 KB
/
relation-list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package jujuc
import (
"fmt"
"github.com/juju/cmd"
"github.com/juju/errors"
"github.com/juju/gnuflag"
jujucmd "github.com/juju/juju/cmd"
)
// RelationListCommand implements the relation-list command.
type RelationListCommand struct {
cmd.CommandBase
ctx Context
RelationId int
relationIdProxy gnuflag.Value
out cmd.Output
}
func NewRelationListCommand(ctx Context) (cmd.Command, error) {
c := &RelationListCommand{ctx: ctx}
rV, err := NewRelationIdValue(c.ctx, &c.RelationId)
if err != nil {
return nil, errors.Trace(err)
}
c.relationIdProxy = rV
return c, nil
}
func (c *RelationListCommand) Info() *cmd.Info {
doc := "-r must be specified when not in a relation hook"
if _, err := c.ctx.HookRelation(); err == nil {
doc = ""
}
return jujucmd.Info(&cmd.Info{
Name: "relation-list",
Purpose: "list relation units",
Doc: doc,
})
}
func (c *RelationListCommand) SetFlags(f *gnuflag.FlagSet) {
c.out.AddFlags(f, "smart", cmd.DefaultFormatters.Formatters())
f.Var(c.relationIdProxy, "r", "specify a relation by id")
f.Var(c.relationIdProxy, "relation", "")
}
func (c *RelationListCommand) Init(args []string) (err error) {
if c.RelationId == -1 {
return fmt.Errorf("no relation id specified")
}
return cmd.CheckEmpty(args)
}
func (c *RelationListCommand) Run(ctx *cmd.Context) error {
r, err := c.ctx.Relation(c.RelationId)
if err != nil {
return errors.Trace(err)
}
unitNames := r.UnitNames()
if unitNames == nil {
unitNames = []string{}
}
return c.out.Write(ctx, unitNames)
}