-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[gitpod-cli] New cmd gp tasks stop
🧪🔬
#12116
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,136 @@ | ||
// 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 | ||
} | ||
|
||
// stopTaskCmd represents the stop 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 | ||
) | ||
|
||
terminalClient, err := supervisor_helper.GetTerminalServiceClient(context.Background()) | ||
|
||
if err != nil { | ||
log.Fatalf("cannot get terminal service: %s", err) | ||
return | ||
} | ||
|
||
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
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) | ||
return | ||
} | ||
|
||
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 { | ||
_, err := terminalClient.Get(ctx, &api.GetTerminalRequest{ | ||
Alias: args[0], | ||
}) | ||
|
||
if err != nil { | ||
fmt.Printf("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]) | ||
return | ||
} | ||
|
||
terminalAliases = append(terminalAliases, args[0]) | ||
} else { | ||
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, | ||
HideSelected: true, | ||
} | ||
|
||
selectedIndex, selectedValue, err := prompt.Run() | ||
|
||
if selectedValue == "" { | ||
return | ||
} | ||
|
||
if err != nil { | ||
log.Fatalf("error occurred with the input prompt: %s", err) | ||
} | ||
|
||
taskIndex = selectedIndex | ||
} | ||
|
||
terminalAliases = append(terminalAliases, tasks[taskIndex].Terminal) | ||
} | ||
|
||
for _, terminalAlias := range terminalAliases { | ||
_, err := terminalClient.Shutdown(context.Background(), &supervisor.ShutdownTerminalRequest{Alias: terminalAlias}) | ||
if err != nil { | ||
log.Fatalf("cannot stop task: %s", err) | ||
return | ||
} | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
tasksCmd.AddCommand(stopTaskCmd) | ||
|
||
stopTaskCmd.Flags().BoolVarP(&stopTaskCmdOpts.All, "all", "a", false, "stop all tasks") | ||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The newer version allows hiding the message after selection using
HideSelected
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also ran
go mod tidy
that's why these changes