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