Skip to content

Refactor cmd tasks and ports supervisor related func #10403

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

Merged
merged 2 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions components/gitpod-cli/cmd/ports-list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// 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"
"os"
"sort"
"time"

supervisor_helper "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor-helper"
supervisor "github.com/gitpod-io/gitpod/supervisor/api"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/olekukonko/tablewriter"
)

var listPortsCmd = &cobra.Command{
Use: "list",
Short: "Lists the workspace ports and their states.",
Run: func(*cobra.Command, []string) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

ports, portsListError := supervisor_helper.GetPortsList(ctx)

if portsListError != nil {
log.WithError(portsListError).Error("Could not get the ports list.")
return
}

if len(ports) == 0 {
fmt.Println("No ports detected.")
return
}

sort.Slice(ports, func(i, j int) bool {
return int(ports[i].LocalPort) < int(ports[j].LocalPort)
})

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Port", "Status", "URL", "Name & Description"})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")

for _, port := range ports {
status := "not served"
statusColor := tablewriter.FgHiBlackColor
if port.Exposed == nil && port.Tunneled == nil {
if port.AutoExposure == supervisor.PortAutoExposure_failed {
status = "failed to expose"
statusColor = tablewriter.FgRedColor
} else {
status = "detecting..."
statusColor = tablewriter.FgYellowColor
}
} else if port.Served {
status = "open (" + port.Exposed.Visibility.String() + ")"
if port.Exposed.Visibility == supervisor.PortVisibility_public {
statusColor = tablewriter.FgHiGreenColor
} else {
statusColor = tablewriter.FgHiCyanColor
}
}

nameAndDescription := port.Name
if len(port.Description) > 0 {
if len(nameAndDescription) > 0 {
nameAndDescription = fmt.Sprint(nameAndDescription, ": ", port.Description)
} else {
nameAndDescription = port.Description
}
}

table.Rich(
[]string{fmt.Sprint(port.LocalPort), status, port.Exposed.Url, nameAndDescription},
[]tablewriter.Colors{{}, {statusColor}, {}, {}},
)
}

table.Render()
},
}

func init() {
portsCmd.AddCommand(listPortsCmd)
}
8 changes: 0 additions & 8 deletions components/gitpod-cli/cmd/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package cmd

import (
"github.com/gitpod-io/gitpod/gitpod-cli/cmd/ports"
"github.com/spf13/cobra"
)

Expand All @@ -19,13 +18,6 @@ var portsCmd = &cobra.Command{
},
}

var listPortsCmd = &cobra.Command{
Use: "list",
Short: "Lists the workspace ports and their states.",
Run: ports.ListPortsCmd,
}

func init() {
rootCmd.AddCommand(portsCmd)
portsCmd.AddCommand(listPortsCmd)
}
86 changes: 0 additions & 86 deletions components/gitpod-cli/cmd/ports/ports-list.go

This file was deleted.

127 changes: 127 additions & 0 deletions components/gitpod-cli/cmd/tasks-attach.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// 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"
"os"
"time"

supervisor_helper "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor-helper"
supervisor "github.com/gitpod-io/gitpod/supervisor/api"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

var attachTaskCmdOpts struct {
Interactive bool
ForceResize bool
}

// attachTaskCmd represents the attach task command
var attachTaskCmd = &cobra.Command{
Use: "attach <id>",
Short: "Attach to a workspace task",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var terminalAlias string

if len(args) > 0 {
terminalAlias = 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 attach to?",
Items: taskNames,
Templates: &promptui.SelectTemplates{
Selected: "Attaching to task: {{ . }}",
},
}

selectedIndex, selectedValue, err := prompt.Run()

if selectedValue == "" {
return
}

if err != nil {
panic(err)
}

taskIndex = selectedIndex
}

terminalAlias = tasks[taskIndex].Terminal
}

terminalClient, err := supervisor_helper.GetTerminalServiceClient(context.Background())
if err != nil {
log.Fatalf("cannot get terminal service: %s", err)
}

terminal, err := terminalClient.Get(context.Background(), &supervisor.GetTerminalRequest{Alias: terminalAlias})
if err != nil {
if e, ok := status.FromError(err); ok {
switch e.Code() {
case codes.NotFound:
fmt.Println("Terminal is inactive:", terminalAlias)
default:
fmt.Println(e.Code(), e.Message())
}
return
} else {
panic(err)
}
}
ppid := int64(os.Getppid())

if ppid == terminal.Pid {
fmt.Println("You are already in terminal:", terminalAlias)
return
}

interactive, _ := cmd.Flags().GetBool("interactive")
forceResize, _ := cmd.Flags().GetBool("force-resize")

supervisor_helper.AttachToTerminal(context.Background(), terminalClient, terminalAlias, supervisor_helper.AttachToTerminalOpts{
ForceResize: forceResize,
Interactive: interactive,
})
},
}

func init() {
tasksCmd.AddCommand(attachTaskCmd)

attachTaskCmd.Flags().BoolVarP(&attachTaskCmdOpts.Interactive, "interactive", "i", true, "assume control over the terminal")
attachTaskCmd.Flags().BoolVarP(&attachTaskCmdOpts.ForceResize, "force-resize", "r", true, "force this terminal's size irregardless of other clients")
}
60 changes: 60 additions & 0 deletions components/gitpod-cli/cmd/tasks-list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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"
"os"
"time"

supervisor_helper "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor-helper"
"github.com/gitpod-io/gitpod/supervisor/api"
"github.com/spf13/cobra"

"github.com/olekukonko/tablewriter"
)

// listTasksCmd represents the tasks list command
var listTasksCmd = &cobra.Command{
Use: "list",
Short: "Lists the workspace tasks and their state",
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

tasks, err := supervisor_helper.GetTasksList(ctx)
if err != nil {
log.Fatalf("cannot get task list: %s", err)
}

if len(tasks) == 0 {
fmt.Println("No tasks detected")
return
}

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Terminal ID", "Name", "State"})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")

mapStatusToColor := map[api.TaskState]int{
0: tablewriter.FgHiGreenColor,
1: tablewriter.FgHiGreenColor,
2: tablewriter.FgHiBlackColor,
}

for _, task := range tasks {
table.Rich([]string{task.Terminal, task.Presentation.Name, task.State.String()}, []tablewriter.Colors{{}, {}, {mapStatusToColor[task.State]}})
}

table.Render()
},
}

func init() {
tasksCmd.AddCommand(listTasksCmd)
}
Loading