Skip to content

Commit 440259a

Browse files
committed
[cli] refactor cmd tasks and ports supervisor related func
1 parent 0723994 commit 440259a

File tree

8 files changed

+98
-114
lines changed

8 files changed

+98
-114
lines changed

components/gitpod-cli/cmd/ports/ports-list.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,25 @@ package ports
77
import (
88
"context"
99
"fmt"
10+
"log"
1011
"os"
1112
"sort"
1213
"time"
1314

14-
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
15-
"github.com/gitpod-io/gitpod/supervisor/api"
16-
log "github.com/sirupsen/logrus"
17-
"github.com/spf13/cobra"
18-
15+
supervisor_helper "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor-helper"
16+
supervisor "github.com/gitpod-io/gitpod/supervisor/api"
1917
"github.com/olekukonko/tablewriter"
18+
"github.com/spf13/cobra"
2019
)
2120

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

26-
conn := supervisor.Dial()
27-
client := api.NewStatusServiceClient(conn)
25+
ports, err := supervisor_helper.GetPortsList(ctx)
2826

29-
ports, portsListError := supervisor.GetPortsList(ctx, client)
30-
31-
if portsListError != nil {
32-
log.WithError(portsListError).Error("Could not get the ports list.")
33-
return
27+
if err != nil {
28+
log.Fatalf("could not get ports list: %s", err)
3429
}
3530

3631
if len(ports) == 0 {
@@ -51,7 +46,7 @@ func ListPortsCmd(*cobra.Command, []string) {
5146
status := "not served"
5247
statusColor := tablewriter.FgHiBlackColor
5348
if port.Exposed == nil && port.Tunneled == nil {
54-
if port.AutoExposure == api.PortAutoExposure_failed {
49+
if port.AutoExposure == supervisor.PortAutoExposure_failed {
5550
status = "failed to expose"
5651
statusColor = tablewriter.FgRedColor
5752
} else {
@@ -60,7 +55,7 @@ func ListPortsCmd(*cobra.Command, []string) {
6055
}
6156
} else if port.Served {
6257
status = "open (" + port.Exposed.Visibility.String() + ")"
63-
if port.Exposed.Visibility == api.PortVisibility_public {
58+
if port.Exposed.Visibility == supervisor.PortVisibility_public {
6459
statusColor = tablewriter.FgHiGreenColor
6560
} else {
6661
statusColor = tablewriter.FgHiCyanColor

components/gitpod-cli/cmd/tasks/tasks-attach.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ package tasks
77
import (
88
"context"
99
"fmt"
10+
"log"
1011
"os"
1112
"time"
1213

13-
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
14-
"github.com/gitpod-io/gitpod/supervisor/api"
14+
supervisor_helper "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor-helper"
15+
supervisor "github.com/gitpod-io/gitpod/supervisor/api"
1516
"github.com/manifoldco/promptui"
1617
"github.com/spf13/cobra"
1718
"google.golang.org/grpc/codes"
@@ -21,19 +22,15 @@ import (
2122
func AttachTasksCmd(cmd *cobra.Command, args []string) {
2223
var terminalAlias string
2324

24-
conn := supervisor.Dial()
25-
2625
if len(args) > 0 {
2726
terminalAlias = args[0]
2827
} else {
29-
statusClient := api.NewStatusServiceClient(conn)
30-
stateToFilter := api.TaskState(api.TaskState_value["running"])
31-
3228
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
3329
defer cancel()
34-
35-
tasks := supervisor.GetTasksListByState(ctx, statusClient, stateToFilter)
36-
30+
tasks, err := supervisor_helper.GetTasksListByState(ctx, supervisor.TaskState_running)
31+
if err != nil {
32+
log.Fatalf("cannot get task list: %s", err)
33+
}
3734
if len(tasks) == 0 {
3835
fmt.Println("There are no running tasks")
3936
return
@@ -73,10 +70,8 @@ func AttachTasksCmd(cmd *cobra.Command, args []string) {
7370

7471
terminalAlias = tasks[taskIndex].Terminal
7572
}
76-
77-
terminalClient := api.NewTerminalServiceClient(conn)
78-
79-
terminal, err := terminalClient.Get(context.Background(), &api.GetTerminalRequest{Alias: terminalAlias})
73+
srvClient := supervisor_helper.GetTerminalServiceClient(context.Background())
74+
terminal, err := srvClient.Get(context.Background(), &supervisor.GetTerminalRequest{Alias: terminalAlias})
8075
if err != nil {
8176
if e, ok := status.FromError(err); ok {
8277
switch e.Code() {
@@ -100,7 +95,7 @@ func AttachTasksCmd(cmd *cobra.Command, args []string) {
10095
interactive, _ := cmd.Flags().GetBool("interactive")
10196
forceResize, _ := cmd.Flags().GetBool("force-resize")
10297

103-
supervisor.AttachToTerminal(context.Background(), terminalClient, terminalAlias, supervisor.AttachToTerminalOpts{
98+
supervisor_helper.AttachToTerminal(context.Background(), srvClient, terminalAlias, supervisor_helper.AttachToTerminalOpts{
10499
ForceResize: forceResize,
105100
Interactive: interactive,
106101
})

components/gitpod-cli/cmd/tasks/tasks-list.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,23 @@ package tasks
77
import (
88
"context"
99
"fmt"
10+
"log"
1011
"os"
1112
"time"
1213

13-
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
14-
"github.com/gitpod-io/gitpod/supervisor/api"
15-
"github.com/spf13/cobra"
16-
14+
supervisor_helper "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor-helper"
15+
supervisor "github.com/gitpod-io/gitpod/supervisor/api"
1716
"github.com/olekukonko/tablewriter"
17+
"github.com/spf13/cobra"
1818
)
1919

2020
func ListTasksCmd(cmd *cobra.Command, args []string) {
2121
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
2222
defer cancel()
23-
24-
conn := supervisor.Dial()
25-
client := api.NewStatusServiceClient(conn)
26-
27-
tasks := supervisor.GetTasksList(ctx, client)
23+
tasks, err := supervisor_helper.GetTasksList(ctx)
24+
if err != nil {
25+
log.Fatalf("cannot get task list: %s", err)
26+
}
2827

2928
if len(tasks) == 0 {
3029
fmt.Println("No tasks detected")
@@ -36,7 +35,7 @@ func ListTasksCmd(cmd *cobra.Command, args []string) {
3635
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
3736
table.SetCenterSeparator("|")
3837

39-
mapStatusToColor := map[api.TaskState]int{
38+
mapStatusToColor := map[supervisor.TaskState]int{
4039
0: tablewriter.FgHiGreenColor,
4140
1: tablewriter.FgHiGreenColor,
4241
2: tablewriter.FgHiBlackColor,

components/gitpod-cli/pkg/supervisor/ports.go renamed to components/gitpod-cli/pkg/supervisor-helper/ports.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
// Licensed under the GNU Affero General Public License (AGPL).
33
// See License-AGPL.txt in the project root for license information.
44

5-
package supervisor
5+
package supervisor_helper
66

77
import (
88
"context"
9-
"github.com/gitpod-io/gitpod/supervisor/api"
9+
10+
supervisor "github.com/gitpod-io/gitpod/supervisor/api"
1011
)
1112

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

1517
if portsStatusClientError != nil {
1618
return nil, portsStatusClientError
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package supervisor_helper
6+
7+
import (
8+
"context"
9+
10+
supervisor "github.com/gitpod-io/gitpod/supervisor/api"
11+
"golang.org/x/xerrors"
12+
)
13+
14+
func GetTasksList(ctx context.Context) ([]*supervisor.TaskStatus, error) {
15+
client := supervisor.NewStatusServiceClient(conn)
16+
respClient, err := client.TasksStatus(ctx, &supervisor.TasksStatusRequest{Observe: false})
17+
if err != nil {
18+
return nil, xerrors.Errorf("failed get tasks status client: %w", err)
19+
}
20+
resp, err := respClient.Recv()
21+
if err != nil {
22+
return nil, xerrors.Errorf("failed receive data: %w", err)
23+
}
24+
return resp.GetTasks(), nil
25+
}
26+
27+
func GetTasksListByState(ctx context.Context, filterState supervisor.TaskState) ([]*supervisor.TaskStatus, error) {
28+
tasks, err := GetTasksList(ctx)
29+
if err != nil {
30+
return nil, err
31+
}
32+
var filteredTasks []*supervisor.TaskStatus
33+
for _, task := range tasks {
34+
if task.State == filterState {
35+
filteredTasks = append(filteredTasks, task)
36+
}
37+
}
38+
return filteredTasks, nil
39+
}

components/gitpod-cli/pkg/supervisor/supervisor.go renamed to components/gitpod-cli/pkg/supervisor-helper/supervisor.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,29 @@
22
// Licensed under the GNU Affero General Public License (AGPL).
33
// See License-AGPL.txt in the project root for license information.
44

5-
package supervisor
5+
package supervisor_helper
66

77
import (
8+
"log"
89
"os"
910

10-
log "github.com/sirupsen/logrus"
1111
"google.golang.org/grpc"
1212
)
1313

14-
func Dial() *grpc.ClientConn {
14+
var conn *grpc.ClientConn
15+
16+
func init() {
1517
supervisorAddr := os.Getenv("SUPERVISOR_ADDR")
1618
if supervisorAddr == "" {
1719
supervisorAddr = "localhost:22999"
1820
}
19-
supervisorConn, err := grpc.Dial(supervisorAddr, grpc.WithInsecure())
21+
c, err := grpc.Dial(supervisorAddr, grpc.WithInsecure())
2022
if err != nil {
21-
log.WithError(err).Fatal("cannot connect to supervisor")
23+
log.Fatalf("failed connecting to supervisor: %s", err)
2224
}
25+
conn = c
26+
}
2327

24-
return supervisorConn
28+
func GetSupervisorConn() *grpc.ClientConn {
29+
return conn
2530
}

components/gitpod-cli/pkg/supervisor/terminal-attach.go renamed to components/gitpod-cli/pkg/supervisor-helper/terminal-attach.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the GNU Affero General Public License (AGPL).
33
// See License-AGPL.txt in the project root for license information.
44

5-
package supervisor
5+
package supervisor_helper
66

77
import (
88
"context"
@@ -12,7 +12,7 @@ import (
1212
"syscall"
1313

1414
"github.com/creack/pty"
15-
"github.com/gitpod-io/gitpod/supervisor/api"
15+
supervisor "github.com/gitpod-io/gitpod/supervisor/api"
1616
log "github.com/sirupsen/logrus"
1717
"golang.org/x/term"
1818
)
@@ -23,9 +23,9 @@ type AttachToTerminalOpts struct {
2323
Token string
2424
}
2525

26-
func AttachToTerminal(ctx context.Context, client api.TerminalServiceClient, alias string, opts AttachToTerminalOpts) {
26+
func AttachToTerminal(ctx context.Context, client supervisor.TerminalServiceClient, alias string, opts AttachToTerminalOpts) {
2727
// Copy to stdout/stderr
28-
listen, err := client.Listen(ctx, &api.ListenTerminalRequest{
28+
listen, err := client.Listen(ctx, &supervisor.ListenTerminalRequest{
2929
Alias: alias,
3030
})
3131
if err != nil {
@@ -66,9 +66,9 @@ func AttachToTerminal(ctx context.Context, client api.TerminalServiceClient, ali
6666
continue
6767
}
6868

69-
req := &api.SetTerminalSizeRequest{
69+
req := &supervisor.SetTerminalSizeRequest{
7070
Alias: alias,
71-
Size: &api.TerminalSize{
71+
Size: &supervisor.TerminalSize{
7272
Cols: uint32(size.Cols),
7373
Rows: uint32(size.Rows),
7474
WidthPx: uint32(size.X),
@@ -78,10 +78,10 @@ func AttachToTerminal(ctx context.Context, client api.TerminalServiceClient, ali
7878

7979
var expectResize bool
8080
if opts.ForceResize {
81-
req.Priority = &api.SetTerminalSizeRequest_Force{Force: true}
81+
req.Priority = &supervisor.SetTerminalSizeRequest_Force{Force: true}
8282
expectResize = true
8383
} else if opts.Token != "" {
84-
req.Priority = &api.SetTerminalSizeRequest_Token{Token: opts.Token}
84+
req.Priority = &supervisor.SetTerminalSizeRequest_Token{Token: opts.Token}
8585
expectResize = true
8686
}
8787

@@ -100,7 +100,7 @@ func AttachToTerminal(ctx context.Context, client api.TerminalServiceClient, ali
100100
for {
101101
n, err := os.Stdin.Read(buf)
102102
if n > 0 {
103-
_, serr := client.Write(ctx, &api.WriteTerminalRequest{Alias: alias, Stdin: buf[:n]})
103+
_, serr := client.Write(ctx, &supervisor.WriteTerminalRequest{Alias: alias, Stdin: buf[:n]})
104104
if serr != nil {
105105
errchan <- err
106106
return
@@ -127,3 +127,8 @@ func AttachToTerminal(ctx context.Context, client api.TerminalServiceClient, ali
127127
case <-stopch:
128128
}
129129
}
130+
131+
func GetTerminalServiceClient(ctx context.Context) supervisor.TerminalServiceClient {
132+
terminalClient := supervisor.NewTerminalServiceClient(conn)
133+
return terminalClient
134+
}

components/gitpod-cli/pkg/supervisor/status-tasks.go

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)