Skip to content

Commit b98d413

Browse files
committed
address PR feedback
1 parent a9ff045 commit b98d413

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

cli/exp_rpty.go

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ func handleRPTY(inv *serpent.Invocation, client *codersdk.Client, args handleRPT
121121
if err := cliui.Agent(ctx, inv.Stderr, agt.ID, cliui.AgentOptions{
122122
FetchInterval: 0,
123123
Fetch: client.WorkspaceAgent,
124-
FetchLogs: client.WorkspaceAgentLogsAfter,
125124
Wait: false,
126125
}); err != nil {
127126
return err
@@ -165,24 +164,19 @@ func handleRPTY(inv *serpent.Invocation, client *codersdk.Client, args handleRPT
165164
defer conn.Close()
166165

167166
cliui.Infof(inv.Stderr, "Connected to %s (agent id: %s)", args.NamedWorkspace, agt.ID)
167+
cliui.Infof(inv.Stderr, "Reconnect ID: %s", reconnectID)
168168
closeUsage := client.UpdateWorkspaceUsageWithBodyContext(ctx, ws.ID, codersdk.PostWorkspaceUsageRequest{
169169
AgentID: agt.ID,
170170
AppName: codersdk.UsageAppNameReconnectingPty,
171171
})
172172
defer closeUsage()
173173

174-
stdinDone := make(chan struct{})
175-
stdoutDone := make(chan struct{})
176-
stderrDone := make(chan struct{})
177-
done := make(chan struct{})
174+
br := bufio.NewScanner(inv.Stdin)
175+
// Split on bytes, otherwise you have to send a newline to flush the buffer.
176+
br.Split(bufio.ScanBytes)
177+
je := json.NewEncoder(conn)
178178

179179
go func() {
180-
defer close(stdinDone)
181-
// This is how we send commands to the agent.
182-
br := bufio.NewScanner(inv.Stdin)
183-
// Split on bytes, otherwise you have to send a newline to flush the buffer.
184-
br.Split(bufio.ScanBytes)
185-
je := json.NewEncoder(conn)
186180
for br.Scan() {
187181
if err := je.Encode(map[string]string{
188182
"data": br.Text(),
@@ -191,27 +185,32 @@ func handleRPTY(inv *serpent.Invocation, client *codersdk.Client, args handleRPT
191185
}
192186
}
193187
}()
188+
189+
windowChange := listenWindowSize(ctx)
194190
go func() {
195-
defer func() {
196-
close(stdoutDone)
197-
}()
198-
_, _ = io.Copy(inv.Stdout, conn)
199-
}()
200-
go func() {
201-
defer func() {
202-
close(stderrDone)
203-
}()
204-
_, _ = io.Copy(inv.Stderr, conn)
205-
}()
206-
go func() {
207-
defer close(done)
208-
<-stdoutDone
209-
<-stderrDone
210-
_ = conn.Close()
211-
_, _ = fmt.Fprintf(inv.Stderr, "Connection closed\n")
191+
for {
192+
select {
193+
case <-ctx.Done():
194+
return
195+
case <-windowChange:
196+
}
197+
width, height, err := term.GetSize(int(stdoutFile.Fd()))
198+
if err != nil {
199+
continue
200+
}
201+
if err := je.Encode(map[string]int{
202+
"width": width,
203+
"height": height,
204+
}); err != nil {
205+
cliui.Errorf(inv.Stderr, "Failed to send window size: %v", err)
206+
}
207+
}
212208
}()
213209

214-
<-done
210+
_, _ = io.Copy(inv.Stdout, conn)
211+
cancel()
212+
_ = conn.Close()
213+
_, _ = fmt.Fprintf(inv.Stderr, "Connection closed\n")
215214

216215
return nil
217216
}

cli/exp_rpty_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ func TestExpRpty(t *testing.T) {
102102
_ = coderdtest.NewWorkspaceAgentWaiter(t, client, workspace.ID).Wait()
103103

104104
pty.ExpectMatch(fmt.Sprintf("Connected to %s", workspace.Name))
105+
pty.ExpectMatch("Reconnect ID: ")
105106
pty.ExpectMatch(" #")
106107
pty.WriteLine("hostname")
107108
pty.ExpectMatch(ct.Container.Config.Hostname)

0 commit comments

Comments
 (0)