Last active
February 23, 2024 16:50
-
-
Save technicalpickles/d37b91fc9f43ae258d0afb136e71add3 to your computer and use it in GitHub Desktop.
lefthook stdin capture reproduction WIP for https://github.com/evilmartians/lefthook/issues/588
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"io" | |
"os" | |
"os/exec" | |
"github.com/creack/pty" | |
) | |
// internal/lefthooik/run/exec/execute_unix.go | |
type executeArgs struct { | |
in io.Reader | |
out io.Writer | |
envs []string | |
root string | |
interactive, useStdin bool | |
} | |
func main() { | |
ctx := context.Background() | |
// internal/lefthook/run.go Lefthook.Run | |
// ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) | |
// defer stop() | |
// https://yourbasic.org/golang/log-to-file/ | |
in, err := os.OpenFile("/dev/null", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | |
if err != nil { | |
println(err.Error()) | |
} | |
defer in.Close() | |
// internal/lefthooik/run/exec/execute_unix.go | |
command := exec.CommandContext(ctx, "sh", "-c", "sleep 2") | |
command.Stdin = in | |
wd, err := os.Getwd() | |
if err != nil { | |
panic(err) | |
} | |
command.Dir = wd | |
command.Env = os.Environ() | |
p, err := pty.Start(command) | |
if err != nil { | |
panic(err) | |
} | |
defer func() { _ = p.Close() }() | |
command.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment