forked from juju/juju
/
cancel.go
144 lines (118 loc) · 3.38 KB
/
cancel.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2014-2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package action
import (
"fmt"
"time"
"github.com/juju/cmd/v3"
"github.com/juju/errors"
"github.com/juju/gnuflag"
"github.com/juju/names/v4"
actionapi "github.com/DavinZhang/juju/api/action"
jujucmd "github.com/DavinZhang/juju/cmd"
"github.com/DavinZhang/juju/cmd/modelcmd"
"github.com/DavinZhang/juju/cmd/output"
)
func NewCancelCommand() cmd.Command {
return modelcmd.Wrap(&cancelCommand{})
}
type cancelCommand struct {
ActionCommandBase
out cmd.Output
requestedIDs []string
}
// Set up the output.
func (c *cancelCommand) SetFlags(f *gnuflag.FlagSet) {
c.ActionCommandBase.SetFlags(f)
c.out.AddFlags(f, "yaml", output.DefaultFormatters)
}
const cancelDoc = `
Cancel pending or running tasks matching given IDs or partial ID prefixes.`
func (c *cancelCommand) Info() *cmd.Info {
info := &cmd.Info{
Name: "cancel-task",
Args: "(<task-id>|<task-id-prefix>) [...]",
Purpose: "Cancel pending or running tasks.",
Doc: cancelDoc,
}
return jujucmd.Info(info)
}
func (c *cancelCommand) Init(args []string) error {
c.requestedIDs = args
return nil
}
func (c *cancelCommand) Run(ctx *cmd.Context) error {
api, err := c.NewActionAPIClient()
if err != nil {
return err
}
defer api.Close()
if len(c.requestedIDs) == 0 {
return errors.Errorf("no task IDs specified")
}
actions, err := api.Cancel(c.requestedIDs)
if err != nil {
return err
}
if len(actions) < 1 {
return errors.Errorf("no tasks found, no tasks have been canceled")
}
type unCanceledAction struct {
ID string
Result *actionapi.ActionResult
}
var failedCancels []unCanceledAction
var canceledActions []actionapi.ActionResult
for i, result := range actions {
if result.Action != nil {
canceledActions = append(canceledActions, result)
} else {
failedCancels = append(failedCancels, unCanceledAction{c.requestedIDs[i], &result})
}
}
if len(canceledActions) > 0 {
err = c.out.Write(ctx, resultsToMap(canceledActions))
}
if len(failedCancels) > 0 {
message := "The following tasks could not be canceled:\n"
for _, a := range failedCancels {
message += fmt.Sprintf("task: %s, error: %s\n", a.ID, a.Result.Message)
}
logger.Warningf(message)
}
return err
}
// resultsToMap is a helper function that takes in a []params.ActionResult
// and returns a map[string]interface{} ready to be served to the
// formatter for printing.
func resultsToMap(results []actionapi.ActionResult) map[string]interface{} {
items := []map[string]interface{}{}
for _, item := range results {
items = append(items, resultToMap(item))
}
return map[string]interface{}{"actions": items}
}
func resultToMap(result actionapi.ActionResult) map[string]interface{} {
item := map[string]interface{}{}
if result.Error != nil {
item["error"] = result.Error.Error()
}
if result.Action != nil {
item["action"] = result.Action.Name
item["id"] = result.Action.ID
rtag, err := names.ParseUnitTag(result.Action.Receiver)
if err != nil {
item["unit"] = result.Action.Receiver
} else {
item["unit"] = rtag.Id()
}
}
item["status"] = result.Status
// result.Completed uses the zero-value to indicate not completed
if result.Completed.Equal(time.Time{}) {
item["completed at"] = "n/a"
} else {
item["completed at"] = result.Completed.UTC().Format("2006-01-02 15:04:05")
}
return item
}