-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gitpod-cli): new cmd to stop tasks
- Loading branch information
Andrea Falzetti
committed
Aug 13, 2022
1 parent
5fb85e4
commit 986a6df
Showing
1 changed file
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
|
||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
supervisor_helper "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor-helper" | ||
"github.com/gitpod-io/gitpod/supervisor/api" | ||
supervisor "github.com/gitpod-io/gitpod/supervisor/api" | ||
"github.com/manifoldco/promptui" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var stopTaskCmdOpts struct { | ||
All bool | ||
} | ||
|
||
func stopTask(ctx context.Context, terminalClient supervisor.TerminalServiceClient, terminalAlias string) { | ||
terminal, err := terminalClient.Get(context.Background(), &supervisor.GetTerminalRequest{Alias: terminalAlias}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
stopCmd := fmt.Sprintf("%s kill %d%s", "\003\n", terminal.Pid, "\n") | ||
|
||
_, err = terminalClient.Write(ctx, &api.WriteTerminalRequest{Alias: terminal.Alias, Stdin: []byte(stopCmd)}) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// stopTaskCmd represents the attach task command | ||
var stopTaskCmd = &cobra.Command{ | ||
Use: "stop <id>", | ||
Short: "Stop a workspace task", | ||
Args: cobra.MaximumNArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
var ( | ||
terminalAliases []string | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
) | ||
|
||
all, _ := cmd.Flags().GetBool("all") | ||
|
||
if all { | ||
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
tasks, err := supervisor_helper.GetTasksListByState(ctx, supervisor.TaskState_running) | ||
|
||
if err != nil { | ||
log.Fatalf("cannot get task list: %s", err) | ||
} | ||
|
||
if len(tasks) == 0 { | ||
fmt.Println("There are no running tasks") | ||
return | ||
} | ||
|
||
for _, task := range tasks { | ||
terminalAliases = append(terminalAliases, task.Terminal) | ||
} | ||
} else if len(args) > 0 { | ||
terminalAliases = append(terminalAliases, args[0]) | ||
} else { | ||
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
tasks, err := supervisor_helper.GetTasksListByState(ctx, supervisor.TaskState_running) | ||
if err != nil { | ||
log.Fatalf("cannot get task list: %s", err) | ||
} | ||
|
||
if len(tasks) == 0 { | ||
fmt.Println("There are no running tasks") | ||
return | ||
} | ||
|
||
var taskNames []string | ||
var taskIndex int | ||
|
||
if len(tasks) == 1 { | ||
taskIndex = 0 | ||
} else { | ||
|
||
for _, task := range tasks { | ||
taskNames = append(taskNames, task.Presentation.Name) | ||
} | ||
|
||
prompt := promptui.Select{ | ||
Label: "What task do you want to stop?", | ||
Items: taskNames, | ||
Templates: &promptui.SelectTemplates{ | ||
Selected: "Stopping task: {{ . }}", | ||
}, | ||
} | ||
|
||
selectedIndex, selectedValue, err := prompt.Run() | ||
|
||
if selectedValue == "" { | ||
return | ||
} | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
taskIndex = selectedIndex | ||
} | ||
|
||
terminalAliases = append(terminalAliases, tasks[taskIndex].Terminal) | ||
} | ||
|
||
terminalClient, err := supervisor_helper.GetTerminalServiceClient(context.Background()) | ||
|
||
if err != nil { | ||
log.Fatalf("cannot get terminal service: %s", err) | ||
} | ||
|
||
for _, terminalAlias := range terminalAliases { | ||
stopTask(ctx, terminalClient, terminalAlias) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
tasksCmd.AddCommand(stopTaskCmd) | ||
|
||
stopTaskCmd.Flags().BoolVarP(&stopTaskCmdOpts.All, "all", "a", false, "stop all tasks") | ||
} |