Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit b93d650

Browse files
committed
Allow for proper terminal restore on error
1 parent d29f7f2 commit b93d650

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

Diff for: cmd/coder/shell.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"golang.org/x/crypto/ssh/terminal"
1212
"golang.org/x/sys/unix"
1313
"golang.org/x/time/rate"
14+
"golang.org/x/xerrors"
1415

1516
"go.coder.com/cli"
1617
"go.coder.com/flog"
@@ -31,17 +32,17 @@ func (cmd *shellCmd) Spec() cli.CommandSpec {
3132
}
3233
}
3334

34-
func enableTerminal(fd int) (restore func()) {
35+
func enableTerminal(fd int) (restore func(), err error) {
3536
state, err := terminal.MakeRaw(fd)
3637
if err != nil {
37-
flog.Fatal("make raw term: %v", err)
38+
return restore, xerrors.Errorf("make raw term: %w", err)
3839
}
3940
return func() {
4041
err := terminal.Restore(fd, state)
4142
if err != nil {
42-
flog.Fatal("restore term state: %v", err)
43+
flog.Error("restore term state: %v", err)
4344
}
44-
}
45+
}, nil
4546
}
4647

4748
func sendResizeEvents(termfd int, client *wush.Client) {
@@ -90,11 +91,14 @@ func (cmd *shellCmd) Run(fl *pflag.FlagSet) {
9091
args = []string{"-c", "exec $(getent passwd $(whoami) | awk -F: '{ print $7 }')"}
9192
}
9293

93-
exitCode := runCommand(envName, command, args)
94+
exitCode, err := runCommand(envName, command, args)
95+
if err != nil {
96+
flog.Fatal("run command: %v", err)
97+
}
9498
os.Exit(exitCode)
9599
}
96100

97-
func runCommand(envName string, command string, args []string) int {
101+
func runCommand(envName string, command string, args []string) (int, error) {
98102
var (
99103
entClient = requireAuth()
100104
env = findEnv(entClient, envName)
@@ -104,7 +108,10 @@ func runCommand(envName string, command string, args []string) int {
104108

105109
tty := terminal.IsTerminal(termfd)
106110
if tty {
107-
restore := enableTerminal(termfd)
111+
restore, err := enableTerminal(termfd)
112+
if err != nil {
113+
return -1, err
114+
}
108115
defer restore()
109116
}
110117

@@ -115,7 +122,7 @@ func runCommand(envName string, command string, args []string) int {
115122
Stdin: true,
116123
}, command, args...)
117124
if err != nil {
118-
flog.Fatal("dial wush: %v", err)
125+
return -1, err
119126
}
120127
ctx := context.Background()
121128

@@ -133,8 +140,8 @@ func runCommand(envName string, command string, args []string) int {
133140

134141
exitCode, err := wc.Wait()
135142
if err != nil {
136-
flog.Fatal("wush error: %v", err)
143+
return -1, err
137144
}
138145

139-
return int(exitCode)
146+
return int(exitCode), nil
140147
}

0 commit comments

Comments
 (0)