Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-cli/cmd/tasks-stop.go
2498 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"context"
9
"fmt"
10
"time"
11
12
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
13
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/utils"
14
"github.com/gitpod-io/gitpod/supervisor/api"
15
"github.com/manifoldco/promptui"
16
"github.com/spf13/cobra"
17
"golang.org/x/xerrors"
18
)
19
20
var stopTaskCmdOpts struct {
21
All bool
22
ForceSuccess bool
23
}
24
25
// stopTaskCmd represents the stop task command
26
var stopTaskCmd = &cobra.Command{
27
Use: "stop <id>",
28
Short: "Stop a workspace task",
29
Args: cobra.MaximumNArgs(1),
30
RunE: func(cmd *cobra.Command, args []string) error {
31
ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
32
defer cancel()
33
34
var (
35
terminalAliases []string
36
)
37
38
client, err := supervisor.New(ctx)
39
if err != nil {
40
return xerrors.Errorf("cannot get task list: %w", err)
41
}
42
defer client.Close()
43
44
all, _ := cmd.Flags().GetBool("all")
45
46
if all {
47
tasks, err := client.GetTasksListByState(ctx, api.TaskState_running)
48
49
if err != nil {
50
return xerrors.Errorf("cannot get task list: %w", err)
51
}
52
53
if len(tasks) == 0 {
54
fmt.Println("There are no running tasks")
55
return nil
56
}
57
58
for _, task := range tasks {
59
terminalAliases = append(terminalAliases, task.Terminal)
60
}
61
} else if len(args) > 0 {
62
_, err := client.Terminal.Get(ctx, &api.GetTerminalRequest{
63
Alias: args[0],
64
})
65
66
if err != nil {
67
msg := fmt.Sprintf("The selected task was not found or already stopped: %s.\nMake sure to use the correct task ID.\nUse 'gp tasks list' to obtain the task id or run 'gp tasks stop' to select the desired task\n", args[0])
68
return GpError{Err: err, Message: msg, OutCome: utils.Outcome_UserErr}
69
}
70
71
terminalAliases = append(terminalAliases, args[0])
72
} else {
73
tasks, err := client.GetTasksListByState(ctx, api.TaskState_running)
74
if err != nil {
75
return xerrors.Errorf("cannot get task list: %w", err)
76
}
77
78
if len(tasks) == 0 {
79
fmt.Println("There are no running tasks")
80
return nil
81
}
82
83
var taskNames []string
84
var taskIndex int
85
86
if len(tasks) == 1 {
87
taskIndex = 0
88
} else {
89
90
for _, task := range tasks {
91
taskNames = append(taskNames, task.Presentation.Name)
92
}
93
94
prompt := promptui.Select{
95
Label: "What task do you want to stop?",
96
Items: taskNames,
97
HideSelected: true,
98
}
99
100
selectedIndex, selectedValue, err := prompt.Run()
101
102
if selectedValue == "" {
103
return nil
104
}
105
106
if err != nil {
107
return xerrors.Errorf("error occurred with the input prompt: %w", err)
108
}
109
110
taskIndex = selectedIndex
111
}
112
113
terminalAliases = append(terminalAliases, tasks[taskIndex].Terminal)
114
}
115
116
for _, terminalAlias := range terminalAliases {
117
_, err = client.Terminal.Shutdown(ctx, &api.ShutdownTerminalRequest{Alias: terminalAlias, ForceSuccess: stopTaskCmdOpts.ForceSuccess})
118
if err != nil {
119
return xerrors.Errorf("cannot stop task: %w", err)
120
}
121
}
122
return nil
123
},
124
}
125
126
func init() {
127
tasksCmd.AddCommand(stopTaskCmd)
128
129
stopTaskCmd.Flags().BoolVarP(&stopTaskCmdOpts.All, "all", "a", false, "stop all tasks")
130
stopTaskCmd.Flags().BoolVarP(&stopTaskCmdOpts.ForceSuccess, "force-success", "f", false, "force the task to exit with status code 0")
131
}
132
133