From 986a6dfe6ad1ff8a2d113ba1f570e8fe55c16293 Mon Sep 17 00:00:00 2001 From: Andrea Falzetti Date: Sat, 13 Aug 2022 16:55:07 +0000 Subject: [PATCH] feat(gitpod-cli): new cmd to stop tasks --- components/gitpod-cli/cmd/tasks-stop.go | 138 ++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 components/gitpod-cli/cmd/tasks-stop.go diff --git a/components/gitpod-cli/cmd/tasks-stop.go b/components/gitpod-cli/cmd/tasks-stop.go new file mode 100644 index 00000000000000..815ccf48b605c2 --- /dev/null +++ b/components/gitpod-cli/cmd/tasks-stop.go @@ -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 ", + 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") +}