-
Notifications
You must be signed in to change notification settings - Fork 491
/
relation-ids.go
85 lines (74 loc) · 2.08 KB
/
relation-ids.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
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package jujuc
import (
"fmt"
"sort"
"github.com/juju/cmd/v3"
"github.com/juju/errors"
"github.com/juju/gnuflag"
jujucmd "github.com/juju/juju/cmd"
"github.com/juju/juju/core/life"
)
// RelationIdsCommand implements the relation-ids command.
type RelationIdsCommand struct {
cmd.CommandBase
ctx Context
Name string
out cmd.Output
}
func NewRelationIdsCommand(ctx Context) (cmd.Command, error) {
name := ""
if r, err := ctx.HookRelation(); err == nil {
name = r.Name()
} else if cause := errors.Cause(err); !errors.IsNotFound(cause) {
return nil, errors.Trace(err)
}
return &RelationIdsCommand{ctx: ctx, Name: name}, nil
}
func (c *RelationIdsCommand) Info() *cmd.Info {
args := "<name>"
doc := ""
if r, err := c.ctx.HookRelation(); err == nil {
// There's not much we can do about this error here.
args = "[<name>]"
doc = fmt.Sprintf("Current default relation name is %q.", r.Name())
} else if !errors.IsNotFound(err) {
logger.Errorf("Could not retrieve hook relation: %v", err)
}
return jujucmd.Info(&cmd.Info{
Name: "relation-ids",
Args: args,
Purpose: "list all relation ids with the given relation name",
Doc: doc,
})
}
func (c *RelationIdsCommand) SetFlags(f *gnuflag.FlagSet) {
c.out.AddFlags(f, "smart", cmd.DefaultFormatters.Formatters())
}
func (c *RelationIdsCommand) Init(args []string) error {
if len(args) > 0 {
c.Name = args[0]
args = args[1:]
} else if c.Name == "" {
return fmt.Errorf("no relation name specified")
}
return cmd.CheckEmpty(args)
}
func (c *RelationIdsCommand) Run(ctx *cmd.Context) error {
result := []string{}
ids, err := c.ctx.RelationIds()
if err != nil && !errors.IsNotFound(err) {
return errors.Trace(err)
}
for _, id := range ids {
r, err := c.ctx.Relation(id)
if err == nil && r.Name() == c.Name && r.Life() != life.Dead {
result = append(result, r.FakeId())
} else if err != nil && !errors.IsNotFound(err) {
return errors.Trace(err)
}
}
sort.Strings(result)
return c.out.Write(ctx, result)
}