Skip to content

Commit facd009

Browse files
committed
devshell: Don't output status if not on a tty
This is just better behavior for the case of e.g. `cosa run -x "somecmd" > out.txt` to execute a command and capture the output - we don't want to intermix status stuff in there. Further, this makes it easier to test the code when not on a tty (in e.g. CI).
1 parent 1b13b39 commit facd009

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

mantle/cmd/kola/devshell.go

+23-11
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ func stripControlCharacters(s string) string {
5050
}, s)
5151
}
5252

53-
func displayStatusMsg(status, msg string, termMaxWidth int) {
53+
func displayStatusMsg(ontty bool, status, msg string, termMaxWidth int) {
54+
if !ontty {
55+
return
56+
}
5457
s := strings.TrimSpace(msg)
5558
if s == "" {
5659
return
@@ -63,8 +66,11 @@ func displayStatusMsg(status, msg string, termMaxWidth int) {
6366
}
6467

6568
func runDevShellSSH(ctx context.Context, builder *platform.QemuBuilder, conf *conf.Conf, sshCommand string) error {
66-
if !term.IsTerminal(0) {
67-
return fmt.Errorf("stdin is not a tty")
69+
ontty := term.IsTerminal(0)
70+
if sshCommand == "" {
71+
if !ontty {
72+
return fmt.Errorf("stdin is not a tty")
73+
}
6874
}
6975
termMaxWidth, _, err := term.GetSize(0)
7076
if err != nil {
@@ -170,6 +176,7 @@ func runDevShellSSH(ctx context.Context, builder *platform.QemuBuilder, conf *co
170176

171177
// Start the SSH client
172178
sc := newSshClient(ip, agent.Socket, sshCommand)
179+
sc.ontty = ontty
173180
go sc.controlStartStop()
174181

175182
ready := false
@@ -187,7 +194,7 @@ func runDevShellSSH(ctx context.Context, builder *platform.QemuBuilder, conf *co
187194
// a status message on the console.
188195
case serialMsg := <-serialChan:
189196
if !ready {
190-
displayStatusMsg(statusMsg, serialMsg, termMaxWidth)
197+
displayStatusMsg(ontty, statusMsg, serialMsg, termMaxWidth)
191198
}
192199
lastMsg = serialMsg
193200
// monitor the err channel
@@ -201,7 +208,9 @@ func runDevShellSSH(ctx context.Context, builder *platform.QemuBuilder, conf *co
201208

202209
// monitor the instance state
203210
case <-qemuWaitChan:
204-
displayStatusMsg("DONE", "QEMU instance terminated", termMaxWidth)
211+
if ontty {
212+
displayStatusMsg(ontty, "DONE", "QEMU instance terminated", termMaxWidth)
213+
}
205214
return nil
206215

207216
// monitor the machine state events from console/serial logs
@@ -232,17 +241,17 @@ func runDevShellSSH(ctx context.Context, builder *platform.QemuBuilder, conf *co
232241
statusMsg = "QEMU guest is booting"
233242
}
234243
}
235-
displayStatusMsg(fmt.Sprintf("EVENT | %s", statusMsg), lastMsg, termMaxWidth)
244+
displayStatusMsg(ontty, fmt.Sprintf("EVENT | %s", statusMsg), lastMsg, termMaxWidth)
236245

237246
// monitor the SSH connection
238247
case err := <-sc.errChan:
239248
if err == nil {
240249
sc.controlChan <- sshNotReady
241-
displayStatusMsg("SESSION", "Clean exit from SSH, terminating instance", termMaxWidth)
250+
displayStatusMsg(ontty, "SESSION", "Clean exit from SSH, terminating instance", termMaxWidth)
242251
return nil
243252
} else if sshCommand != "" {
244253
sc.controlChan <- sshNotReady
245-
displayStatusMsg("SESSION", "SSH command exited, terminating instance", termMaxWidth)
254+
displayStatusMsg(ontty, "SESSION", "SSH command exited, terminating instance", termMaxWidth)
246255
return err
247256
}
248257
if ready {
@@ -455,6 +464,7 @@ type sshClient struct {
455464
port string
456465
agent string
457466
cmd string
467+
ontty bool
458468
controlChan chan sshControlMessage
459469
errChan chan error
460470
sshCmd *exec.Cmd
@@ -511,8 +521,10 @@ func (sc *sshClient) start() {
511521
if sc.cmd != "" {
512522
sshArgs = append(sshArgs, "--", sc.cmd)
513523
}
514-
fmt.Printf("\033[2K\r") // clear serial console line
515-
fmt.Printf("[SESSION] Starting SSH\r") // and stage a status msg which will be erased
524+
if sc.ontty {
525+
fmt.Printf("\033[2K\r") // clear serial console line
526+
fmt.Printf("[SESSION] Starting SSH\r") // and stage a status msg which will be erased
527+
}
516528
sshCmd := exec.Command(sshArgs[0], sshArgs[1:]...)
517529
sshCmd.Stdin = os.Stdin
518530
sshCmd.Stdout = os.Stdout
@@ -531,7 +543,7 @@ func (sc *sshClient) start() {
531543
for scanner.Scan() {
532544
msg := scanner.Text()
533545
if strings.Contains(msg, "Connection to 127.0.0.1 closed") {
534-
displayStatusMsg("SSH", "connection closed", 0)
546+
displayStatusMsg(sc.ontty, "SSH", "connection closed", 0)
535547
}
536548
}
537549
}()

0 commit comments

Comments
 (0)