Skip to content

Commit 3444d8d

Browse files
committed
multiple fixes
1 parent c6d4364 commit 3444d8d

File tree

5 files changed

+123
-21
lines changed

5 files changed

+123
-21
lines changed

pkg/cmd/buildctl/proxy.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,21 @@ type HealthProxy struct {
996996
state func() *ProxyState
997997
}
998998

999+
func (p *HealthProxy) List(ctx context.Context, in *health.HealthListRequest) (*health.HealthListResponse, error) {
1000+
md, ok := metadata.FromIncomingContext(ctx)
1001+
if ok {
1002+
ctx = metadata.NewOutgoingContext(ctx, md)
1003+
}
1004+
1005+
state := p.state()
1006+
if state.Err != nil {
1007+
return nil, state.Err
1008+
}
1009+
1010+
client := health.NewHealthClient(state.Conn)
1011+
return client.List(ctx, in)
1012+
}
1013+
9991014
func (p *HealthProxy) Check(ctx context.Context, in *health.HealthCheckRequest) (*health.HealthCheckResponse, error) {
10001015
md, ok := metadata.FromIncomingContext(ctx)
10011016
if ok {

pkg/cmd/compute/exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func newComputeExec() *cobra.Command {
7575
case *civ1.ExecuteCommandResponse_Stdout:
7676
fmt.Fprint(os.Stdout, v.Stdout)
7777
case *civ1.ExecuteCommandResponse_Stderr:
78-
fmt.Fprint(os.Stdout, v.Stderr)
78+
fmt.Fprint(os.Stderr, v.Stderr)
7979
case *civ1.ExecuteCommandResponse_ExitCode:
8080
if v.ExitCode != 0 {
8181
os.Exit(int(v.ExitCode))

pkg/cmd/compute/pty.go

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package compute
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"io"
@@ -85,6 +86,16 @@ func newComputePty() *cobra.Command {
8586
}
8687
defer term.Restore(fd, oldState) //nolint:errcheck
8788

89+
// Restore terminal on signals that would otherwise leave it raw.
90+
sigCh := make(chan os.Signal, 1)
91+
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
92+
go func() {
93+
<-sigCh
94+
term.Restore(fd, oldState) //nolint:errcheck
95+
os.Exit(1)
96+
}()
97+
defer signal.Stop(sigCh)
98+
8899
client := api.NewComputeClient()
89100
stream := client.OpenPtySession(ctx)
90101
stream.RequestHeader().Set("Authorization", "Bearer "+token)
@@ -105,40 +116,44 @@ func newComputePty() *cobra.Command {
105116
return fmt.Errorf("send pty init: %w", err)
106117
}
107118

108-
// Handle SIGWINCH for terminal resize.
109-
sigwinch := make(chan os.Signal, 1)
110-
signal.Notify(sigwinch, syscall.SIGWINCH)
111-
defer signal.Stop(sigwinch)
119+
ctx, cancel := context.WithCancel(ctx)
120+
defer cancel()
121+
122+
sendCh := make(chan *civ1.OpenPtySessionRequest, 1)
112123

113124
go func() {
114-
for range sigwinch {
115-
w, h, err := term.GetSize(fd)
116-
if err != nil {
117-
continue
125+
for {
126+
select {
127+
case msg := <-sendCh:
128+
_ = stream.Send(msg)
129+
case <-ctx.Done():
130+
_ = stream.CloseRequest()
131+
return
118132
}
119-
_ = stream.Send(&civ1.OpenPtySessionRequest{
120-
Message: &civ1.OpenPtySessionRequest_WindowResize{
121-
WindowResize: &civ1.WindowResize{
122-
Rows: uint32(h),
123-
Cols: uint32(w),
124-
},
125-
},
126-
})
127133
}
128134
}()
129135

136+
// Watch for terminal resize events (no-op on Windows).
137+
stopResize := watchTerminalResize(ctx, fd, sendCh)
138+
defer stopResize()
139+
130140
// Forward stdin to the stream.
131141
go func() {
132142
buf := make([]byte, 4096) //nolint:mnd
133143
for {
134144
n, err := os.Stdin.Read(buf)
135145
if n > 0 {
136-
_ = stream.Send(&civ1.OpenPtySessionRequest{
137-
Message: &civ1.OpenPtySessionRequest_Stdin{Stdin: buf[:n]},
138-
})
146+
data := make([]byte, n)
147+
copy(data, buf[:n])
148+
select {
149+
case sendCh <- &civ1.OpenPtySessionRequest{
150+
Message: &civ1.OpenPtySessionRequest_Stdin{Stdin: data},
151+
}:
152+
case <-ctx.Done():
153+
return
154+
}
139155
}
140156
if err != nil {
141-
_ = stream.CloseRequest()
142157
return
143158
}
144159
}
@@ -159,6 +174,7 @@ func newComputePty() *cobra.Command {
159174
case *civ1.OpenPtySessionResponse_ExitCode:
160175
fmt.Fprintf(os.Stderr, "\r\n[exit %d]\r\n", m.ExitCode)
161176
if m.ExitCode != 0 {
177+
term.Restore(fd, oldState) //nolint:errcheck
162178
os.Exit(int(m.ExitCode))
163179
}
164180
return nil

pkg/cmd/compute/pty_resize_unix.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//go:build !windows
2+
3+
package compute
4+
5+
import (
6+
"context"
7+
"os"
8+
"os/signal"
9+
"syscall"
10+
11+
civ1 "github.com/depot/cli/pkg/proto/depot/ci/v1"
12+
"golang.org/x/term"
13+
)
14+
15+
// watchTerminalResize listens for SIGWINCH and sends resize messages via sendCh.
16+
// Stops when ctx is cancelled. Returns a cleanup function to stop watching.
17+
func watchTerminalResize(
18+
ctx context.Context,
19+
fd int,
20+
sendCh chan<- *civ1.OpenPtySessionRequest,
21+
) func() {
22+
sigwinch := make(chan os.Signal, 1)
23+
signal.Notify(sigwinch, syscall.SIGWINCH)
24+
25+
go func() {
26+
for {
27+
select {
28+
case <-ctx.Done():
29+
return
30+
case _, ok := <-sigwinch:
31+
if !ok {
32+
return
33+
}
34+
w, h, err := term.GetSize(fd)
35+
if err != nil {
36+
continue
37+
}
38+
select {
39+
case sendCh <- &civ1.OpenPtySessionRequest{
40+
Message: &civ1.OpenPtySessionRequest_WindowResize{
41+
WindowResize: &civ1.WindowResize{
42+
Rows: uint32(h),
43+
Cols: uint32(w),
44+
},
45+
},
46+
}:
47+
case <-ctx.Done():
48+
return
49+
}
50+
}
51+
}
52+
}()
53+
54+
return func() { signal.Stop(sigwinch) }
55+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package compute
2+
3+
import (
4+
"context"
5+
6+
civ1 "github.com/depot/cli/pkg/proto/depot/ci/v1"
7+
)
8+
9+
// watchTerminalResize is a no-op on Windows since SIGWINCH is not available.
10+
func watchTerminalResize(
11+
_ context.Context,
12+
_ int,
13+
_ chan<- *civ1.OpenPtySessionRequest,
14+
) func() {
15+
return func() {}
16+
}

0 commit comments

Comments
 (0)