Skip to content

Commit 49dd117

Browse files
committed
Add support for running sub-processes under a PTY
This is really nice for running programs that output colour, such as coloured log lines, etc. I defaulted it to off because some applications when run under a PTY will attempt to move the cursor around, clear lines, and so on, so it should be up to the user to decide whether to enable it.
1 parent ebb9736 commit 49dd117

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ require (
99
gopkg.in/yaml.v3 v3.0.1
1010
)
1111

12-
require github.com/mattn/go-isatty v0.0.17 // indirect
12+
require (
13+
github.com/creack/pty v1.1.18
14+
github.com/mattn/go-isatty v0.0.17 // indirect
15+
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
2+
github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
13
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
24
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
35
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ var exitOnStop = flag.Bool("exit-on-stop", true, "Exit goreman if all subprocess
9898
// show timestamp in log
9999
var logTime = flag.Bool("logtime", true, "show timestamp in log")
100100

101+
// use a PTY for all subprocesses
102+
var usePty = flag.Bool("pty", false, "use PTY (default false)")
103+
101104
var maxProcNameLength = 0
102105

103106
var re = regexp.MustCompile(`\$([a-zA-Z]+[a-zA-Z0-9_]+)`)

proc.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ package main
33
import (
44
"errors"
55
"fmt"
6+
"io"
67
"os"
78
"os/exec"
89
"sync"
910
"time"
11+
12+
"github.com/creack/pty"
1013
)
1114

1215
// spawnProc starts the specified proc, and returns any error from running it.
@@ -17,10 +20,28 @@ func spawnProc(name string, errCh chan<- error) {
1720
cs := append(cmdStart, proc.cmdline)
1821
cmd := exec.Command(cs[0], cs[1:]...)
1922
cmd.Stdin = nil
20-
cmd.Stdout = logger
21-
cmd.Stderr = logger
2223
cmd.SysProcAttr = procAttrs
2324

25+
if *usePty {
26+
p, t, err := pty.Open()
27+
if err != nil {
28+
select {
29+
case errCh <- err:
30+
default:
31+
}
32+
fmt.Fprintf(logger, "Failed to open pty for %s: %s\n", name, err)
33+
return
34+
}
35+
defer p.Close()
36+
defer t.Close()
37+
cmd.Stdout = t
38+
cmd.Stderr = t
39+
go io.Copy(logger, p)
40+
} else {
41+
cmd.Stdout = logger
42+
cmd.Stderr = logger
43+
}
44+
2445
if proc.setPort {
2546
cmd.Env = append(os.Environ(), fmt.Sprintf("PORT=%d", proc.port))
2647
fmt.Fprintf(logger, "Starting %s on port %d\n", name, proc.port)

0 commit comments

Comments
 (0)