Skip to content

Commit

Permalink
[cli] refactor cmd tasks and ports supervisor related func
Browse files Browse the repository at this point in the history
  • Loading branch information
mustard-mh committed Jun 1, 2022
1 parent 0723994 commit 440259a
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 114 deletions.
23 changes: 9 additions & 14 deletions components/gitpod-cli/cmd/ports/ports-list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,25 @@ package ports
import (
"context"
"fmt"
"log"
"os"
"sort"
"time"

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

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

func ListPortsCmd(*cobra.Command, []string) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

conn := supervisor.Dial()
client := api.NewStatusServiceClient(conn)
ports, err := supervisor_helper.GetPortsList(ctx)

ports, portsListError := supervisor.GetPortsList(ctx, client)

if portsListError != nil {
log.WithError(portsListError).Error("Could not get the ports list.")
return
if err != nil {
log.Fatalf("could not get ports list: %s", err)
}

if len(ports) == 0 {
Expand All @@ -51,7 +46,7 @@ func ListPortsCmd(*cobra.Command, []string) {
status := "not served"
statusColor := tablewriter.FgHiBlackColor
if port.Exposed == nil && port.Tunneled == nil {
if port.AutoExposure == api.PortAutoExposure_failed {
if port.AutoExposure == supervisor.PortAutoExposure_failed {
status = "failed to expose"
statusColor = tablewriter.FgRedColor
} else {
Expand All @@ -60,7 +55,7 @@ func ListPortsCmd(*cobra.Command, []string) {
}
} else if port.Served {
status = "open (" + port.Exposed.Visibility.String() + ")"
if port.Exposed.Visibility == api.PortVisibility_public {
if port.Exposed.Visibility == supervisor.PortVisibility_public {
statusColor = tablewriter.FgHiGreenColor
} else {
statusColor = tablewriter.FgHiCyanColor
Expand Down
25 changes: 10 additions & 15 deletions components/gitpod-cli/cmd/tasks/tasks-attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ package tasks
import (
"context"
"fmt"
"log"
"os"
"time"

"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
"github.com/gitpod-io/gitpod/supervisor/api"
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"
Expand All @@ -21,19 +22,15 @@ import (
func AttachTasksCmd(cmd *cobra.Command, args []string) {
var terminalAlias string

conn := supervisor.Dial()

if len(args) > 0 {
terminalAlias = args[0]
} else {
statusClient := api.NewStatusServiceClient(conn)
stateToFilter := api.TaskState(api.TaskState_value["running"])

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

tasks := supervisor.GetTasksListByState(ctx, statusClient, stateToFilter)

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
Expand Down Expand Up @@ -73,10 +70,8 @@ func AttachTasksCmd(cmd *cobra.Command, args []string) {

terminalAlias = tasks[taskIndex].Terminal
}

terminalClient := api.NewTerminalServiceClient(conn)

terminal, err := terminalClient.Get(context.Background(), &api.GetTerminalRequest{Alias: terminalAlias})
srvClient := supervisor_helper.GetTerminalServiceClient(context.Background())
terminal, err := srvClient.Get(context.Background(), &supervisor.GetTerminalRequest{Alias: terminalAlias})
if err != nil {
if e, ok := status.FromError(err); ok {
switch e.Code() {
Expand All @@ -100,7 +95,7 @@ func AttachTasksCmd(cmd *cobra.Command, args []string) {
interactive, _ := cmd.Flags().GetBool("interactive")
forceResize, _ := cmd.Flags().GetBool("force-resize")

supervisor.AttachToTerminal(context.Background(), terminalClient, terminalAlias, supervisor.AttachToTerminalOpts{
supervisor_helper.AttachToTerminal(context.Background(), srvClient, terminalAlias, supervisor_helper.AttachToTerminalOpts{
ForceResize: forceResize,
Interactive: interactive,
})
Expand Down
19 changes: 9 additions & 10 deletions components/gitpod-cli/cmd/tasks/tasks-list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@ package tasks
import (
"context"
"fmt"
"log"
"os"
"time"

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

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

func ListTasksCmd(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

conn := supervisor.Dial()
client := api.NewStatusServiceClient(conn)

tasks := supervisor.GetTasksList(ctx, client)
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")
Expand All @@ -36,7 +35,7 @@ func ListTasksCmd(cmd *cobra.Command, args []string) {
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")

mapStatusToColor := map[api.TaskState]int{
mapStatusToColor := map[supervisor.TaskState]int{
0: tablewriter.FgHiGreenColor,
1: tablewriter.FgHiGreenColor,
2: tablewriter.FgHiBlackColor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.

package supervisor
package supervisor_helper

import (
"context"
"github.com/gitpod-io/gitpod/supervisor/api"

supervisor "github.com/gitpod-io/gitpod/supervisor/api"
)

func GetPortsList(ctx context.Context, client api.StatusServiceClient) ([]*api.PortsStatus, error) {
portsStatusClient, portsStatusClientError := client.PortsStatus(ctx, &api.PortsStatusRequest{Observe: false})
func GetPortsList(ctx context.Context) ([]*supervisor.PortsStatus, error) {
client := supervisor.NewStatusServiceClient(conn)
portsStatusClient, portsStatusClientError := client.PortsStatus(ctx, &supervisor.PortsStatusRequest{Observe: false})

if portsStatusClientError != nil {
return nil, portsStatusClientError
Expand Down
39 changes: 39 additions & 0 deletions components/gitpod-cli/pkg/supervisor-helper/status-tasks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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 supervisor_helper

import (
"context"

supervisor "github.com/gitpod-io/gitpod/supervisor/api"
"golang.org/x/xerrors"
)

func GetTasksList(ctx context.Context) ([]*supervisor.TaskStatus, error) {
client := supervisor.NewStatusServiceClient(conn)
respClient, err := client.TasksStatus(ctx, &supervisor.TasksStatusRequest{Observe: false})
if err != nil {
return nil, xerrors.Errorf("failed get tasks status client: %w", err)
}
resp, err := respClient.Recv()
if err != nil {
return nil, xerrors.Errorf("failed receive data: %w", err)
}
return resp.GetTasks(), nil
}

func GetTasksListByState(ctx context.Context, filterState supervisor.TaskState) ([]*supervisor.TaskStatus, error) {
tasks, err := GetTasksList(ctx)
if err != nil {
return nil, err
}
var filteredTasks []*supervisor.TaskStatus
for _, task := range tasks {
if task.State == filterState {
filteredTasks = append(filteredTasks, task)
}
}
return filteredTasks, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.

package supervisor
package supervisor_helper

import (
"log"
"os"

log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
)

func Dial() *grpc.ClientConn {
var conn *grpc.ClientConn

func init() {
supervisorAddr := os.Getenv("SUPERVISOR_ADDR")
if supervisorAddr == "" {
supervisorAddr = "localhost:22999"
}
supervisorConn, err := grpc.Dial(supervisorAddr, grpc.WithInsecure())
c, err := grpc.Dial(supervisorAddr, grpc.WithInsecure())
if err != nil {
log.WithError(err).Fatal("cannot connect to supervisor")
log.Fatalf("failed connecting to supervisor: %s", err)
}
conn = c
}

return supervisorConn
func GetSupervisorConn() *grpc.ClientConn {
return conn
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.

package supervisor
package supervisor_helper

import (
"context"
Expand All @@ -12,7 +12,7 @@ import (
"syscall"

"github.com/creack/pty"
"github.com/gitpod-io/gitpod/supervisor/api"
supervisor "github.com/gitpod-io/gitpod/supervisor/api"
log "github.com/sirupsen/logrus"
"golang.org/x/term"
)
Expand All @@ -23,9 +23,9 @@ type AttachToTerminalOpts struct {
Token string
}

func AttachToTerminal(ctx context.Context, client api.TerminalServiceClient, alias string, opts AttachToTerminalOpts) {
func AttachToTerminal(ctx context.Context, client supervisor.TerminalServiceClient, alias string, opts AttachToTerminalOpts) {
// Copy to stdout/stderr
listen, err := client.Listen(ctx, &api.ListenTerminalRequest{
listen, err := client.Listen(ctx, &supervisor.ListenTerminalRequest{
Alias: alias,
})
if err != nil {
Expand Down Expand Up @@ -66,9 +66,9 @@ func AttachToTerminal(ctx context.Context, client api.TerminalServiceClient, ali
continue
}

req := &api.SetTerminalSizeRequest{
req := &supervisor.SetTerminalSizeRequest{
Alias: alias,
Size: &api.TerminalSize{
Size: &supervisor.TerminalSize{
Cols: uint32(size.Cols),
Rows: uint32(size.Rows),
WidthPx: uint32(size.X),
Expand All @@ -78,10 +78,10 @@ func AttachToTerminal(ctx context.Context, client api.TerminalServiceClient, ali

var expectResize bool
if opts.ForceResize {
req.Priority = &api.SetTerminalSizeRequest_Force{Force: true}
req.Priority = &supervisor.SetTerminalSizeRequest_Force{Force: true}
expectResize = true
} else if opts.Token != "" {
req.Priority = &api.SetTerminalSizeRequest_Token{Token: opts.Token}
req.Priority = &supervisor.SetTerminalSizeRequest_Token{Token: opts.Token}
expectResize = true
}

Expand All @@ -100,7 +100,7 @@ func AttachToTerminal(ctx context.Context, client api.TerminalServiceClient, ali
for {
n, err := os.Stdin.Read(buf)
if n > 0 {
_, serr := client.Write(ctx, &api.WriteTerminalRequest{Alias: alias, Stdin: buf[:n]})
_, serr := client.Write(ctx, &supervisor.WriteTerminalRequest{Alias: alias, Stdin: buf[:n]})
if serr != nil {
errchan <- err
return
Expand All @@ -127,3 +127,8 @@ func AttachToTerminal(ctx context.Context, client api.TerminalServiceClient, ali
case <-stopch:
}
}

func GetTerminalServiceClient(ctx context.Context) supervisor.TerminalServiceClient {
terminalClient := supervisor.NewTerminalServiceClient(conn)
return terminalClient
}
56 changes: 0 additions & 56 deletions components/gitpod-cli/pkg/supervisor/status-tasks.go

This file was deleted.

0 comments on commit 440259a

Please sign in to comment.