Skip to content

Commit 4021fb7

Browse files
authored
fix: enable interactive inputs for windows (#696)
1 parent 0ea9433 commit 4021fb7

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/creack/pty v1.1.21
1010
github.com/gobwas/glob v0.2.3
1111
github.com/google/go-cmp v0.6.0
12+
github.com/mattn/go-tty v0.0.5
1213
github.com/mitchellh/mapstructure v1.5.0
1314
github.com/rogpeppe/go-internal v1.12.0
1415
github.com/spf13/afero v1.11.0

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,20 @@ github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69
3737
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
3838
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
3939
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
40+
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
4041
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
4142
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
43+
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
44+
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
4245
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
4346
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
4447
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
48+
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
4549
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
4650
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
4751
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
52+
github.com/mattn/go-tty v0.0.5 h1:s09uXI7yDbXzzTTfw3zonKFzwGkyYlgU3OMjqA0ddz4=
53+
github.com/mattn/go-tty v0.0.5/go.mod h1:u5GGXBtZU6RQoKV8gY5W6UhMudbR5vXnUe7j3pxse28=
4854
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
4955
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
5056
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
@@ -97,6 +103,9 @@ go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
97103
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
98104
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
99105
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
106+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
107+
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
108+
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
100109
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
101110
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
102111
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=

internal/lefthook/runner/exec/execute_unix.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/creack/pty"
1616
"github.com/mattn/go-isatty"
17+
"github.com/mattn/go-tty"
1718

1819
"github.com/evilmartians/lefthook/internal/log"
1920
)
@@ -34,10 +35,10 @@ func (e CommandExecutor) Execute(ctx context.Context, opts Options, out io.Write
3435
in = os.Stdin
3536
}
3637
if opts.Interactive && !isatty.IsTerminal(os.Stdin.Fd()) {
37-
tty, err := os.Open("/dev/tty")
38+
tty, err := tty.Open()
3839
if err == nil {
3940
defer tty.Close()
40-
in = tty
41+
in = tty.Input()
4142
} else {
4243
log.Errorf("Couldn't enable TTY input: %s\n", err)
4344
}

internal/lefthook/runner/exec/execute_windows.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import (
99
"path/filepath"
1010
"strings"
1111
"syscall"
12+
13+
"github.com/evilmartians/lefthook/internal/log"
14+
"github.com/mattn/go-isatty"
15+
"github.com/mattn/go-tty"
1216
)
1317

1418
type CommandExecutor struct{}
@@ -20,6 +24,20 @@ type executeArgs struct {
2024
}
2125

2226
func (e CommandExecutor) Execute(ctx context.Context, opts Options, out io.Writer) error {
27+
var in io.Reader = nullReader{}
28+
if opts.UseStdin {
29+
in = os.Stdin
30+
}
31+
if opts.Interactive && !isatty.IsTerminal(os.Stdin.Fd()) {
32+
tty, err := tty.Open()
33+
if err == nil {
34+
defer tty.Close()
35+
in = tty.Input()
36+
} else {
37+
log.Errorf("Couldn't enable TTY input: %s\n", err)
38+
}
39+
}
40+
2341
root, _ := filepath.Abs(opts.Root)
2442
envs := make([]string, len(opts.Env))
2543
for name, value := range opts.Env {
@@ -29,11 +47,6 @@ func (e CommandExecutor) Execute(ctx context.Context, opts Options, out io.Write
2947
)
3048
}
3149

32-
var in io.Reader = nullReader{}
33-
if opts.Interactive || opts.UseStdin {
34-
in = os.Stdin
35-
}
36-
3750
args := &executeArgs{
3851
in: in,
3952
out: out,

0 commit comments

Comments
 (0)