forked from juju/juju
/
is-leader.go
50 lines (43 loc) · 1.29 KB
/
is-leader.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package jujuc
import (
"github.com/juju/cmd"
"github.com/juju/errors"
"github.com/juju/gnuflag"
)
// isLeaderCommand implements the is-leader command.
type isLeaderCommand struct {
cmd.CommandBase
ctx Context
out cmd.Output
}
// NewIsLeaderCommand returns a new isLeaderCommand with the given context.
func NewIsLeaderCommand(ctx Context) (cmd.Command, error) {
return &isLeaderCommand{ctx: ctx}, nil
}
// Info is part of the cmd.Command interface.
func (c *isLeaderCommand) Info() *cmd.Info {
doc := `
is-leader prints a boolean indicating whether the local unit is guaranteed to
be service leader for at least 30 seconds. If it fails, you should assume that
there is no such guarantee.
`
return &cmd.Info{
Name: "is-leader",
Purpose: "print service leadership status",
Doc: doc,
}
}
// SetFlags is part of the cmd.Command interface.
func (c *isLeaderCommand) SetFlags(f *gnuflag.FlagSet) {
c.out.AddFlags(f, "smart", cmd.DefaultFormatters)
}
// Run is part of the cmd.Command interface.
func (c *isLeaderCommand) Run(ctx *cmd.Context) error {
success, err := c.ctx.IsLeader()
if err != nil {
return errors.Annotatef(err, "leadership status unknown")
}
return c.out.Write(ctx, success)
}