Skip to content

Commit

Permalink
feat(gitpod-cli): new cmd to stop tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Falzetti committed Aug 15, 2022
1 parent 5fb85e4 commit fa495b3
Showing 1 changed file with 138 additions and 0 deletions.
138 changes: 138 additions & 0 deletions components/gitpod-cli/cmd/tasks-stop.go
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 stop tasks 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")
}

0 comments on commit fa495b3

Please sign in to comment.