github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  	jujucmd "github.com/juju/juju/cmd"
    12  	"github.com/juju/juju/core/status"
    13  )
    14  
    15  // StatusSetCommand implements the status-set command.
    16  type StatusSetCommand struct {
    17  	cmd.CommandBase
    18  	ctx         Context
    19  	status      string
    20  	message     string
    21  	application bool
    22  }
    23  
    24  // NewStatusSetCommand makes a jujuc status-set command.
    25  func NewStatusSetCommand(ctx Context) (cmd.Command, error) {
    26  	return &StatusSetCommand{ctx: ctx}, nil
    27  }
    28  
    29  func (c *StatusSetCommand) Info() *cmd.Info {
    30  	doc := `
    31  Sets the workload status of the charm. Message is optional.
    32  The "last updated" attribute of the status is set, even if the
    33  status and message are the same as what's already set.
    34  `
    35  	return jujucmd.Info(&cmd.Info{
    36  		Name:    "status-set",
    37  		Args:    "<maintenance | blocked | waiting | active> [message]",
    38  		Purpose: "set status information",
    39  		Doc:     doc,
    40  	})
    41  }
    42  
    43  var validStatus = []status.Status{
    44  	status.Maintenance,
    45  	status.Blocked,
    46  	status.Waiting,
    47  	status.Active,
    48  }
    49  
    50  func (c *StatusSetCommand) SetFlags(f *gnuflag.FlagSet) {
    51  	f.BoolVar(&c.application, "application", 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.application {
    82  		return c.ctx.SetApplicationStatus(statusInfo)
    83  	}
    84  	return c.ctx.SetUnitStatus(statusInfo)
    85  
    86  }