forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_task_command.go
87 lines (70 loc) · 2.53 KB
/
run_task_command.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
package v3
import (
"code.cloudfoundry.org/cli/actor/v3action"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/flag"
"code.cloudfoundry.org/cli/command/v3/shared"
)
//go:generate counterfeiter . RunTaskActor
type RunTaskActor interface {
GetApplicationByNameAndSpace(appName string, spaceGUID string) (v3action.Application, v3action.Warnings, error)
RunTask(appGUID string, command string, name string) (v3action.Task, v3action.Warnings, error)
CloudControllerAPIVersion() string
}
type RunTaskCommand struct {
RequiredArgs flag.RunTaskArgs `positional-args:"yes"`
Name string `long:"name" description:"Name to give the task (generated if omitted)"`
usage interface{} `usage:"CF_NAME run-task APP_NAME COMMAND [--name TASK_NAME]\n\nEXAMPLES:\n CF_NAME run-task my-app \"bundle exec rake db:migrate\" --name migrate"`
relatedCommands interface{} `related_commands:"logs, tasks, terminate-task"`
UI command.UI
Actor RunTaskActor
Config command.Config
}
func (cmd *RunTaskCommand) Setup(config command.Config, ui command.UI) error {
cmd.UI = ui
cmd.Config = config
client, err := shared.NewClients(config, ui)
if err != nil {
return err
}
cmd.Actor = v3action.NewActor(client)
return nil
}
func (cmd RunTaskCommand) Execute(args []string) error {
err := command.MinimumAPIVersionCheck(cmd.Actor.CloudControllerAPIVersion(), "3.0.0")
if err != nil {
return err
}
err = command.CheckTarget(cmd.Config, true, true)
if err != nil {
return err
}
space := cmd.Config.TargetedSpace()
user, err := cmd.Config.CurrentUser()
if err != nil {
return err
}
application, warnings, err := cmd.Actor.GetApplicationByNameAndSpace(cmd.RequiredArgs.AppName, space.GUID)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return shared.HandleError(err)
}
cmd.UI.DisplayTextWithFlavor("Creating task for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...", map[string]interface{}{
"AppName": cmd.RequiredArgs.AppName,
"OrgName": cmd.Config.TargetedOrganization().Name,
"SpaceName": space.Name,
"CurrentUser": user.Name,
})
task, warnings, err := cmd.Actor.RunTask(application.GUID, cmd.RequiredArgs.Command, cmd.Name)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return shared.HandleError(err)
}
cmd.UI.DisplayOK()
cmd.UI.DisplayNewline()
cmd.UI.DisplayText("Task {{.TaskSequenceID}} has been submitted successfully for execution.",
map[string]interface{}{
"TaskSequenceID": task.SequenceID,
})
return nil
}