github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/uniter/runner/jujuc/is-leader.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/gnuflag"
    10  )
    11  
    12  // isLeaderCommand implements the is-leader command.
    13  type isLeaderCommand struct {
    14  	cmd.CommandBase
    15  	ctx Context
    16  	out cmd.Output
    17  }
    18  
    19  // NewIsLeaderCommand returns a new isLeaderCommand with the given context.
    20  func NewIsLeaderCommand(ctx Context) (cmd.Command, error) {
    21  	return &isLeaderCommand{ctx: ctx}, nil
    22  }
    23  
    24  // Info is part of the cmd.Command interface.
    25  func (c *isLeaderCommand) Info() *cmd.Info {
    26  	doc := `
    27  is-leader prints a boolean indicating whether the local unit is guaranteed to
    28  be service leader for at least 30 seconds. If it fails, you should assume that
    29  there is no such guarantee.
    30  `
    31  	return &cmd.Info{
    32  		Name:    "is-leader",
    33  		Purpose: "print service leadership status",
    34  		Doc:     doc,
    35  	}
    36  }
    37  
    38  // SetFlags is part of the cmd.Command interface.
    39  func (c *isLeaderCommand) SetFlags(f *gnuflag.FlagSet) {
    40  	c.out.AddFlags(f, "smart", cmd.DefaultFormatters)
    41  }
    42  
    43  // Run is part of the cmd.Command interface.
    44  func (c *isLeaderCommand) Run(ctx *cmd.Context) error {
    45  	success, err := c.ctx.IsLeader()
    46  	if err != nil {
    47  		return errors.Annotatef(err, "leadership status unknown")
    48  	}
    49  	return c.out.Write(ctx, success)
    50  }