github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/worker/uniter/runner/jujuc/action-fail.go (about)

     1  // Copyright 2014 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  	"launchpad.net/gnuflag"
     9  )
    10  
    11  // ActionFailCommand implements the action-fail command.
    12  type ActionFailCommand struct {
    13  	cmd.CommandBase
    14  	ctx         Context
    15  	failMessage string
    16  }
    17  
    18  // NewActionFailCommand returns a new ActionFailCommand with the given context.
    19  func NewActionFailCommand(ctx Context) (cmd.Command, error) {
    20  	return &ActionFailCommand{ctx: ctx}, nil
    21  }
    22  
    23  // Info returns the content for --help.
    24  func (c *ActionFailCommand) Info() *cmd.Info {
    25  	doc := `
    26  action-fail sets the action's fail state with a given error message.  Using
    27  action-fail without a failure message will set a default message indicating a
    28  problem with the action.
    29  `
    30  	return &cmd.Info{
    31  		Name:    "action-fail",
    32  		Args:    "[\"<failure message>\"]",
    33  		Purpose: "set action fail status with message",
    34  		Doc:     doc,
    35  	}
    36  }
    37  
    38  // SetFlags handles any option flags, but there are none.
    39  func (c *ActionFailCommand) SetFlags(f *gnuflag.FlagSet) {
    40  }
    41  
    42  // Init sets the fail message and checks for malformed invocations.
    43  func (c *ActionFailCommand) Init(args []string) error {
    44  	if len(args) == 0 {
    45  		c.failMessage = "action failed without reason given, check action for errors"
    46  		return nil
    47  	}
    48  	c.failMessage = args[0]
    49  	return cmd.CheckEmpty(args[1:])
    50  }
    51  
    52  // Run sets the Action's fail state.
    53  func (c *ActionFailCommand) Run(ctx *cmd.Context) error {
    54  	err := c.ctx.SetActionMessage(c.failMessage)
    55  	if err != nil {
    56  		return err
    57  	}
    58  	return c.ctx.SetActionFailed()
    59  }