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