Skip to content

Instantly share code, notes, and snippets.

@technicalpickles
Last active February 23, 2024 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save technicalpickles/d37b91fc9f43ae258d0afb136e71add3 to your computer and use it in GitHub Desktop.
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
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