Skip to content

Commit be78308

Browse files
committed
[gp-cli] refactor cmd tasks and ports supervisor related func
1 parent f937087 commit be78308

File tree

9 files changed

+122
-119
lines changed

9 files changed

+122
-119
lines changed

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"sort"
1212
"time"
1313

14-
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
15-
"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"
1616
log "github.com/sirupsen/logrus"
1717
"github.com/spf13/cobra"
1818

@@ -26,10 +26,7 @@ var listPortsCmd = &cobra.Command{
2626
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
2727
defer cancel()
2828

29-
conn := supervisor.Dial()
30-
client := api.NewStatusServiceClient(conn)
31-
32-
ports, portsListError := supervisor.GetPortsList(ctx, client)
29+
ports, portsListError := supervisor_helper.GetPortsList(ctx)
3330

3431
if portsListError != nil {
3532
log.WithError(portsListError).Error("Could not get the ports list.")
@@ -54,7 +51,7 @@ var listPortsCmd = &cobra.Command{
5451
status := "not served"
5552
statusColor := tablewriter.FgHiBlackColor
5653
if port.Exposed == nil && port.Tunneled == nil {
57-
if port.AutoExposure == api.PortAutoExposure_failed {
54+
if port.AutoExposure == supervisor.PortAutoExposure_failed {
5855
status = "failed to expose"
5956
statusColor = tablewriter.FgRedColor
6057
} else {
@@ -63,7 +60,7 @@ var listPortsCmd = &cobra.Command{
6360
}
6461
} else if port.Served {
6562
status = "open (" + port.Exposed.Visibility.String() + ")"
66-
if port.Exposed.Visibility == api.PortVisibility_public {
63+
if port.Exposed.Visibility == supervisor.PortVisibility_public {
6764
statusColor = tablewriter.FgHiGreenColor
6865
} else {
6966
statusColor = tablewriter.FgHiCyanColor

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

+13-11
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ package cmd
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"
@@ -31,18 +32,16 @@ var attachTaskCmd = &cobra.Command{
3132
Run: func(cmd *cobra.Command, args []string) {
3233
var terminalAlias string
3334

34-
conn := supervisor.Dial()
35-
3635
if len(args) > 0 {
3736
terminalAlias = args[0]
3837
} else {
39-
statusClient := api.NewStatusServiceClient(conn)
40-
stateToFilter := api.TaskState(api.TaskState_value["running"])
41-
4238
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
4339
defer cancel()
4440

45-
tasks := supervisor.GetTasksListByState(ctx, statusClient, stateToFilter)
41+
tasks, err := supervisor_helper.GetTasksListByState(ctx, supervisor.TaskState_running)
42+
if err != nil {
43+
log.Fatalf("cannot get task list: %s", err)
44+
}
4645

4746
if len(tasks) == 0 {
4847
fmt.Println("There are no running tasks")
@@ -84,9 +83,12 @@ var attachTaskCmd = &cobra.Command{
8483
terminalAlias = tasks[taskIndex].Terminal
8584
}
8685

87-
terminalClient := api.NewTerminalServiceClient(conn)
86+
terminalClient, err := supervisor_helper.GetTerminalServiceClient(context.Background())
87+
if err != nil {
88+
log.Fatalf("cannot get terminal service: %s", err)
89+
}
8890

89-
terminal, err := terminalClient.Get(context.Background(), &api.GetTerminalRequest{Alias: terminalAlias})
91+
terminal, err := terminalClient.Get(context.Background(), &supervisor.GetTerminalRequest{Alias: terminalAlias})
9092
if err != nil {
9193
if e, ok := status.FromError(err); ok {
9294
switch e.Code() {
@@ -110,7 +112,7 @@ var attachTaskCmd = &cobra.Command{
110112
interactive, _ := cmd.Flags().GetBool("interactive")
111113
forceResize, _ := cmd.Flags().GetBool("force-resize")
112114

113-
supervisor.AttachToTerminal(context.Background(), terminalClient, terminalAlias, supervisor.AttachToTerminalOpts{
115+
supervisor_helper.AttachToTerminal(context.Background(), terminalClient, terminalAlias, supervisor_helper.AttachToTerminalOpts{
114116
ForceResize: forceResize,
115117
Interactive: interactive,
116118
})

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ package cmd
77
import (
88
"context"
99
"fmt"
10+
"log"
1011
"os"
1112
"time"
1213

13-
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
14+
supervisor_helper "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor-helper"
1415
"github.com/gitpod-io/gitpod/supervisor/api"
1516
"github.com/spf13/cobra"
1617

@@ -25,10 +26,10 @@ var listTasksCmd = &cobra.Command{
2526
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
2627
defer cancel()
2728

28-
conn := supervisor.Dial()
29-
client := api.NewStatusServiceClient(conn)
30-
31-
tasks := supervisor.GetTasksList(ctx, client)
29+
tasks, err := supervisor_helper.GetTasksList(ctx)
30+
if err != nil {
31+
log.Fatalf("cannot get task list: %s", err)
32+
}
3233

3334
if len(tasks) == 0 {
3435
fmt.Println("No tasks detected")

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
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+
conn, err := Dial(ctx)
15+
if err != nil {
16+
return nil, err
17+
}
18+
client := supervisor.NewStatusServiceClient(conn)
19+
portsStatusClient, portsStatusClientError := client.PortsStatus(ctx, &supervisor.PortsStatusRequest{Observe: false})
1420

1521
if portsStatusClientError != nil {
1622
return nil, portsStatusClientError
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
conn, err := Dial(ctx)
16+
if err != nil {
17+
return nil, err
18+
}
19+
client := supervisor.NewStatusServiceClient(conn)
20+
respClient, err := client.TasksStatus(ctx, &supervisor.TasksStatusRequest{Observe: false})
21+
if err != nil {
22+
return nil, xerrors.Errorf("failed get tasks status client: %w", err)
23+
}
24+
resp, err := respClient.Recv()
25+
if err != nil {
26+
return nil, xerrors.Errorf("failed receive data: %w", err)
27+
}
28+
return resp.GetTasks(), nil
29+
}
30+
31+
func GetTasksListByState(ctx context.Context, filterState supervisor.TaskState) ([]*supervisor.TaskStatus, error) {
32+
tasks, err := GetTasksList(ctx)
33+
if err != nil {
34+
return nil, err
35+
}
36+
var filteredTasks []*supervisor.TaskStatus
37+
for _, task := range tasks {
38+
if task.State == filterState {
39+
filteredTasks = append(filteredTasks, task)
40+
}
41+
}
42+
return filteredTasks, nil
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
"os"
10+
11+
"golang.org/x/xerrors"
12+
"google.golang.org/grpc"
13+
"google.golang.org/grpc/credentials/insecure"
14+
)
15+
16+
func Dial(ctx context.Context) (*grpc.ClientConn, error) {
17+
supervisorAddr := os.Getenv("SUPERVISOR_ADDR")
18+
if supervisorAddr == "" {
19+
supervisorAddr = "localhost:22999"
20+
}
21+
supervisorConn, err := grpc.DialContext(ctx, supervisorAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
22+
if err != nil {
23+
err = xerrors.Errorf("failed connecting to supervisor: %w", err)
24+
}
25+
return supervisorConn, err
26+
}

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

+19-10
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 {
@@ -50,7 +50,7 @@ func AttachToTerminal(ctx context.Context, client api.TerminalServiceClient, ali
5050
// Set stdin in raw mode.
5151
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
5252
if err != nil {
53-
panic(err)
53+
log.WithError(err).Fatal("cannot attach to terminal")
5454
}
5555
defer func() { _ = term.Restore(int(os.Stdin.Fd()), oldState) }() // Best effort.
5656

@@ -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,12 @@ func AttachToTerminal(ctx context.Context, client api.TerminalServiceClient, ali
127127
case <-stopch:
128128
}
129129
}
130+
131+
func GetTerminalServiceClient(ctx context.Context) (supervisor.TerminalServiceClient, error) {
132+
conn, err := Dial(ctx)
133+
if err != nil {
134+
return nil, err
135+
}
136+
terminalClient := supervisor.NewTerminalServiceClient(conn)
137+
return terminalClient, nil
138+
}

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

-56
This file was deleted.

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

-25
This file was deleted.

0 commit comments

Comments
 (0)