github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/uniter/runner/jujuc/status-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/gnuflag"
    10  
    11  	"github.com/juju/juju/status"
    12  )
    13  
    14  // StatusSetCommand implements the status-set command.
    15  type StatusSetCommand struct {
    16  	cmd.CommandBase
    17  	ctx     Context
    18  	status  string
    19  	message string
    20  	service bool
    21  }
    22  
    23  // NewStatusSetCommand makes a jujuc status-set command.
    24  func NewStatusSetCommand(ctx Context) (cmd.Command, error) {
    25  	return &StatusSetCommand{ctx: ctx}, nil
    26  }
    27  
    28  func (c *StatusSetCommand) Info() *cmd.Info {
    29  	doc := `
    30  Sets the workload status of the charm. Message is optional.
    31  The "last updated" attribute of the status is set, even if the
    32  status and message are the same as what's already set.
    33  `
    34  	return &cmd.Info{
    35  		Name:    "status-set",
    36  		Args:    "<maintenance | blocked | waiting | active> [message]",
    37  		Purpose: "set status information",
    38  		Doc:     doc,
    39  	}
    40  }
    41  
    42  var validStatus = []status.Status{
    43  	status.Maintenance,
    44  	status.Blocked,
    45  	status.Waiting,
    46  	status.Active,
    47  }
    48  
    49  func (c *StatusSetCommand) SetFlags(f *gnuflag.FlagSet) {
    50  	f.BoolVar(&c.service, "application", false, "set this status for the application to which the unit belongs if the unit is the leader")
    51  	f.BoolVar(&c.service, "service", false, "set this status for the application to which the unit belongs if the unit is the leader")
    52  }
    53  
    54  func (c *StatusSetCommand) Init(args []string) error {
    55  	if len(args) < 1 {
    56  		return errors.Errorf("invalid args, require <status> [message]")
    57  	}
    58  	valid := false
    59  	for _, s := range validStatus {
    60  		if string(s) == args[0] {
    61  			valid = true
    62  			break
    63  		}
    64  	}
    65  	if !valid {
    66  		return errors.Errorf("invalid status %q, expected one of %v", args[0], validStatus)
    67  	}
    68  	c.status = args[0]
    69  	if len(args) > 1 {
    70  		c.message = args[1]
    71  		return cmd.CheckEmpty(args[2:])
    72  	}
    73  	return nil
    74  }
    75  
    76  func (c *StatusSetCommand) Run(ctx *cmd.Context) error {
    77  	statusInfo := StatusInfo{
    78  		Status: c.status,
    79  		Info:   c.message,
    80  	}
    81  	if c.service {
    82  		return c.ctx.SetApplicationStatus(statusInfo)
    83  	}
    84  	return c.ctx.SetUnitStatus(statusInfo)
    85  
    86  }