Skip to content

Commit f561971

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 f561971

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

mantle/cmd/kola/devshell.go

Lines changed: 21 additions & 11 deletions
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,7 @@ 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+
displayStatusMsg(ontty, "DONE", "QEMU instance terminated", termMaxWidth)
205212
return nil
206213

207214
// monitor the machine state events from console/serial logs
@@ -232,17 +239,17 @@ func runDevShellSSH(ctx context.Context, builder *platform.QemuBuilder, conf *co
232239
statusMsg = "QEMU guest is booting"
233240
}
234241
}
235-
displayStatusMsg(fmt.Sprintf("EVENT | %s", statusMsg), lastMsg, termMaxWidth)
242+
displayStatusMsg(ontty, fmt.Sprintf("EVENT | %s", statusMsg), lastMsg, termMaxWidth)
236243

237244
// monitor the SSH connection
238245
case err := <-sc.errChan:
239246
if err == nil {
240247
sc.controlChan <- sshNotReady
241-
displayStatusMsg("SESSION", "Clean exit from SSH, terminating instance", termMaxWidth)
248+
displayStatusMsg(ontty, "SESSION", "Clean exit from SSH, terminating instance", termMaxWidth)
242249
return nil
243250
} else if sshCommand != "" {
244251
sc.controlChan <- sshNotReady
245-
displayStatusMsg("SESSION", "SSH command exited, terminating instance", termMaxWidth)
252+
displayStatusMsg(ontty, "SESSION", "SSH command exited, terminating instance", termMaxWidth)
246253
return err
247254
}
248255
if ready {
@@ -455,6 +462,7 @@ type sshClient struct {
455462
port string
456463
agent string
457464
cmd string
465+
ontty bool
458466
controlChan chan sshControlMessage
459467
errChan chan error
460468
sshCmd *exec.Cmd
@@ -511,8 +519,10 @@ func (sc *sshClient) start() {
511519
if sc.cmd != "" {
512520
sshArgs = append(sshArgs, "--", sc.cmd)
513521
}
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
522+
if sc.ontty {
523+
fmt.Printf("\033[2K\r") // clear serial console line
524+
fmt.Printf("[SESSION] Starting SSH\r") // and stage a status msg which will be erased
525+
}
516526
sshCmd := exec.Command(sshArgs[0], sshArgs[1:]...)
517527
sshCmd.Stdin = os.Stdin
518528
sshCmd.Stdout = os.Stdout
@@ -531,7 +541,7 @@ func (sc *sshClient) start() {
531541
for scanner.Scan() {
532542
msg := scanner.Text()
533543
if strings.Contains(msg, "Connection to 127.0.0.1 closed") {
534-
displayStatusMsg("SSH", "connection closed", 0)
544+
displayStatusMsg(sc.ontty, "SSH", "connection closed", 0)
535545
}
536546
}
537547
}()

0 commit comments

Comments
 (0)