Path: blob/main/components/gitpod-cli/cmd/tasks-stop.go
2498 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package cmd56import (7"context"8"fmt"9"time"1011"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"12"github.com/gitpod-io/gitpod/gitpod-cli/pkg/utils"13"github.com/gitpod-io/gitpod/supervisor/api"14"github.com/manifoldco/promptui"15"github.com/spf13/cobra"16"golang.org/x/xerrors"17)1819var stopTaskCmdOpts struct {20All bool21ForceSuccess bool22}2324// stopTaskCmd represents the stop task command25var stopTaskCmd = &cobra.Command{26Use: "stop <id>",27Short: "Stop a workspace task",28Args: cobra.MaximumNArgs(1),29RunE: func(cmd *cobra.Command, args []string) error {30ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)31defer cancel()3233var (34terminalAliases []string35)3637client, err := supervisor.New(ctx)38if err != nil {39return xerrors.Errorf("cannot get task list: %w", err)40}41defer client.Close()4243all, _ := cmd.Flags().GetBool("all")4445if all {46tasks, err := client.GetTasksListByState(ctx, api.TaskState_running)4748if err != nil {49return xerrors.Errorf("cannot get task list: %w", err)50}5152if len(tasks) == 0 {53fmt.Println("There are no running tasks")54return nil55}5657for _, task := range tasks {58terminalAliases = append(terminalAliases, task.Terminal)59}60} else if len(args) > 0 {61_, err := client.Terminal.Get(ctx, &api.GetTerminalRequest{62Alias: args[0],63})6465if err != nil {66msg := 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])67return GpError{Err: err, Message: msg, OutCome: utils.Outcome_UserErr}68}6970terminalAliases = append(terminalAliases, args[0])71} else {72tasks, err := client.GetTasksListByState(ctx, api.TaskState_running)73if err != nil {74return xerrors.Errorf("cannot get task list: %w", err)75}7677if len(tasks) == 0 {78fmt.Println("There are no running tasks")79return nil80}8182var taskNames []string83var taskIndex int8485if len(tasks) == 1 {86taskIndex = 087} else {8889for _, task := range tasks {90taskNames = append(taskNames, task.Presentation.Name)91}9293prompt := promptui.Select{94Label: "What task do you want to stop?",95Items: taskNames,96HideSelected: true,97}9899selectedIndex, selectedValue, err := prompt.Run()100101if selectedValue == "" {102return nil103}104105if err != nil {106return xerrors.Errorf("error occurred with the input prompt: %w", err)107}108109taskIndex = selectedIndex110}111112terminalAliases = append(terminalAliases, tasks[taskIndex].Terminal)113}114115for _, terminalAlias := range terminalAliases {116_, err = client.Terminal.Shutdown(ctx, &api.ShutdownTerminalRequest{Alias: terminalAlias, ForceSuccess: stopTaskCmdOpts.ForceSuccess})117if err != nil {118return xerrors.Errorf("cannot stop task: %w", err)119}120}121return nil122},123}124125func init() {126tasksCmd.AddCommand(stopTaskCmd)127128stopTaskCmd.Flags().BoolVarP(&stopTaskCmdOpts.All, "all", "a", false, "stop all tasks")129stopTaskCmd.Flags().BoolVarP(&stopTaskCmdOpts.ForceSuccess, "force-success", "f", false, "force the task to exit with status code 0")130}131132133