Skip to content

Instantly share code, notes, and snippets.

@technicalpickles
Last active February 23, 2024 16:50
Show Gist options
  • Save technicalpickles/d37b91fc9f43ae258d0afb136e71add3 to your computer and use it in GitHub Desktop.
Save technicalpickles/d37b91fc9f43ae258d0afb136e71add3 to your computer and use it in GitHub Desktop.

Revisions

  1. technicalpickles revised this gist Feb 23, 2024. No changes.
  2. technicalpickles renamed this gist Feb 23, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. technicalpickles created this gist Feb 23, 2024.
    56 changes: 56 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    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()
    }