Skip to content

Commit

Permalink
fix: provide a fix for *nix and windows executors
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Nov 30, 2023
1 parent 2e3d9ba commit 20c94ae
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
13 changes: 3 additions & 10 deletions internal/lefthook/run/exec/execute_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,11 @@ type executeArgs struct {
interactive, useStdin bool
}

type nullReader struct{}

func (nullReader) Read(b []byte) (int, error) {
if len(b) == 0 {
return 0, nil
}

return 0, io.EOF
}

func (e CommandExecutor) Execute(ctx context.Context, opts Options, out io.Writer) error {
var in io.Reader = nullReader{}
if opts.UseStdin {
in = os.Stdin
}
if opts.Interactive && !isatty.IsTerminal(os.Stdin.Fd()) {
tty, err := os.Open("/dev/tty")
if err == nil {
Expand Down
7 changes: 6 additions & 1 deletion internal/lefthook/run/exec/execute_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ func (e CommandExecutor) Execute(ctx context.Context, opts Options, out io.Write
)
}

var in io.Reader = nullReader{}
if opts.Interactive || opts.UseStdin {
in = os.Stdin
}

args := &executeArgs{
in: os.Stdin,
in: in,
out: out,
envs: envs,
root: root,
Expand Down
10 changes: 10 additions & 0 deletions internal/lefthook/run/exec/nullReader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package exec

import "io"

// nullReader always returns EOF.
type nullReader struct{}

func (nullReader) Read(b []byte) (int, error) {
return 0, io.EOF
}

0 comments on commit 20c94ae

Please sign in to comment.