github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/uniter/runner/jujuc/leader-set.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package jujuc 5 6 import ( 7 "github.com/juju/cmd" 8 "github.com/juju/errors" 9 "github.com/juju/utils/keyvalues" 10 ) 11 12 // leaderSetCommand implements the leader-set command. 13 type leaderSetCommand struct { 14 cmd.CommandBase 15 ctx Context 16 settings map[string]string 17 } 18 19 // NewLeaderSetCommand returns a new leaderSetCommand with the given context. 20 func NewLeaderSetCommand(ctx Context) (cmd.Command, error) { 21 return &leaderSetCommand{ctx: ctx}, nil 22 } 23 24 // Info is part of the cmd.Command interface. 25 func (c *leaderSetCommand) Info() *cmd.Info { 26 doc := ` 27 leader-set immediate writes the supplied key/value pairs to the controller, 28 which will then inform non-leader units of the change. It will fail if called 29 without arguments, or if called by a unit that is not currently service leader. 30 ` 31 return &cmd.Info{ 32 Name: "leader-set", 33 Args: "<key>=<value> [...]", 34 Purpose: "write service leadership settings", 35 Doc: doc, 36 } 37 } 38 39 // Init is part of the cmd.Command interface. 40 func (c *leaderSetCommand) Init(args []string) (err error) { 41 c.settings, err = keyvalues.Parse(args, true) 42 return 43 } 44 45 // Run is part of the cmd.Command interface. 46 func (c *leaderSetCommand) Run(_ *cmd.Context) error { 47 err := c.ctx.WriteLeaderSettings(c.settings) 48 return errors.Annotatef(err, "cannot write leadership settings") 49 }