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