From fe8a00c9e73461f07714409f69d605193dc10272 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Fri, 18 Oct 2024 08:52:38 -0400 Subject: [PATCH] wtf! --- main.go | 50 ++++++++++++++++++------------------------------ xtask/src/run.rs | 2 +- 2 files changed, 20 insertions(+), 32 deletions(-) diff --git a/main.go b/main.go index 726ab06..3578a6f 100644 --- a/main.go +++ b/main.go @@ -4,13 +4,12 @@ import ( "bufio" "context" "fmt" - "io" "os" "os/exec" "path/filepath" "runtime" "strings" - "sync" + "syscall" "time" ) @@ -170,47 +169,36 @@ func run() error { cmd := exec.CommandContext(ctx, "cargo", "xtask", "run") cmd.Dir = projectDir + cmd.Stderr = os.Stderr + // Prevent the child process from being in our process group so that we can send it a SIGINT + // without sending one to ourselves. + cmd.SysProcAttr = &syscall.SysProcAttr{ + Setpgid: true, + } stdoutPipe, err := cmd.StdoutPipe() if err != nil { return fmt.Errorf("Failed to get stdout pipe: %w", err) } - stderrPipe, err := cmd.StderrPipe() - if err != nil { - return fmt.Errorf("Failed to get stderr pipe: %w", err) - } - fmt.Printf("Running %s in %s\n", cmd, cmd.Dir) if err := cmd.Start(); err != nil { return fmt.Errorf("%s failed to start: %w", cmd, err) } - var wg sync.WaitGroup - for pipe, stream := range map[io.Reader]*os.File{stdoutPipe: os.Stdout, stderrPipe: os.Stderr} { - pipe, stream := pipe, stream - wg.Add(1) - go func() { - defer wg.Done() - - scanner := bufio.NewScanner(pipe) - for scanner.Scan() { - text := scanner.Text() - if _, err := fmt.Fprintln(stream, text); err != nil { - panic(err) - } - if strings.Contains(text, "Waiting for Ctrl-C") { - if err := cmd.Process.Signal(os.Interrupt); err != nil { - panic(err) - } - } - } - if err := scanner.Err(); err != nil { - panic(err) - } - }() + scanner := bufio.NewScanner(stdoutPipe) + for scanner.Scan() { + text := scanner.Text() + if _, err := fmt.Fprintln(os.Stdout, text); err != nil { + panic(err) + } + if strings.Contains(text, "Waiting for Ctrl-C") { + syscall.Kill(-cmd.Process.Pid, syscall.SIGINT) + } + } + if err := scanner.Err(); err != nil { + panic(err) } - wg.Wait() if err := cmd.Wait(); err != nil { return fmt.Errorf("%s failed: %w", cmd, err) diff --git a/xtask/src/run.rs b/xtask/src/run.rs index 16c80a8..d454826 100644 --- a/xtask/src/run.rs +++ b/xtask/src/run.rs @@ -10,7 +10,7 @@ pub struct Options { #[clap(long)] release: bool, /// The command used to wrap your application. - #[clap(short, long, default_value = "sudo -E")] + #[clap(short, long, default_value = "sudo -ES")] runner: String, /// Arguments to pass to your application. #[clap(global = true, last = true)]