-
Notifications
You must be signed in to change notification settings - Fork 494
/
leader-get.go
77 lines (67 loc) · 1.84 KB
/
leader-get.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package jujuc
import (
"strings"
"github.com/juju/cmd"
"github.com/juju/errors"
"github.com/juju/gnuflag"
jujucmd "github.com/juju/juju/cmd"
)
// leaderGetCommand implements the leader-get command.
type leaderGetCommand struct {
cmd.CommandBase
ctx Context
key string
out cmd.Output
}
// NewLeaderGetCommand returns a new leaderGetCommand with the given context.
func NewLeaderGetCommand(ctx Context) (cmd.Command, error) {
return &leaderGetCommand{ctx: ctx}, nil
}
// Info is part of the cmd.Command interface.
func (c *leaderGetCommand) Info() *cmd.Info {
doc := `
leader-get prints the value of a leadership setting specified by key. If no key
is given, or if the key is "-", all keys and values will be printed.
`
return jujucmd.Info(&cmd.Info{
Name: "leader-get",
Args: "[<key>]",
Purpose: "print application leadership settings",
Doc: doc,
})
}
// SetFlags is part of the cmd.Command interface.
func (c *leaderGetCommand) SetFlags(f *gnuflag.FlagSet) {
c.out.AddFlags(f, "smart", cmd.DefaultFormatters.Formatters())
}
// Init is part of the cmd.Command interface.
func (c *leaderGetCommand) Init(args []string) error {
c.key = ""
if len(args) == 0 {
return nil
}
key := args[0]
if key == "-" {
key = ""
} else if strings.Contains(key, "=") {
return errors.Errorf("invalid key %q", key)
}
c.key = key
return cmd.CheckEmpty(args[1:])
}
// Run is part of the cmd.Command interface.
func (c *leaderGetCommand) Run(ctx *cmd.Context) error {
settings, err := c.ctx.LeaderSettings()
if err != nil {
return errors.Annotatef(err, "cannot read leadership settings")
}
if c.key == "" {
return c.out.Write(ctx, settings)
}
if value, ok := settings[c.key]; ok {
return c.out.Write(ctx, value)
}
return c.out.Write(ctx, nil)
}