Skip to content

Commit 075b695

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

File tree

5 files changed

+81
-77
lines changed

5 files changed

+81
-77
lines changed

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

Lines changed: 10 additions & 4 deletions
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
Lines changed: 43 additions & 0 deletions
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+
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,26 @@
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+
"context"
89
"os"
910

10-
log "github.com/sirupsen/logrus"
11+
"golang.org/x/xerrors"
1112
"google.golang.org/grpc"
1213
)
1314

14-
func Dial() *grpc.ClientConn {
15+
var conn *grpc.ClientConn
16+
17+
func Dial(ctx context.Context) (*grpc.ClientConn, error) {
1518
supervisorAddr := os.Getenv("SUPERVISOR_ADDR")
1619
if supervisorAddr == "" {
1720
supervisorAddr = "localhost:22999"
1821
}
19-
supervisorConn, err := grpc.Dial(supervisorAddr, grpc.WithInsecure())
22+
supervisorConn, err := grpc.DialContext(ctx, supervisorAddr, grpc.WithInsecure())
2023
if err != nil {
21-
log.WithError(err).Fatal("cannot connect to supervisor")
24+
err = xerrors.Errorf("failed connecting to supervisor: %w", err)
2225
}
23-
24-
return supervisorConn
26+
return supervisorConn, err
2527
}

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

Lines changed: 19 additions & 10 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 {
@@ -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

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

0 commit comments

Comments
 (0)