diff --git a/main.go b/main.go index 726ab06..7a83760 100644 --- a/main.go +++ b/main.go @@ -4,13 +4,11 @@ import ( "bufio" "context" "fmt" - "io" "os" "os/exec" "path/filepath" "runtime" "strings" - "sync" "time" ) @@ -170,47 +168,41 @@ func run() error { cmd := exec.CommandContext(ctx, "cargo", "xtask", "run") cmd.Dir = projectDir + cmd.Stderr = os.Stderr + prevCancel := cmd.Cancel + cmd.Cancel = func() error { + fmt.Println("!!! Canceling") + return prevCancel() + } 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 { + 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") { + fmt.Println("!!! Sending interrupt !!!") + if err := cmd.Process.Signal(os.Interrupt); err != nil { panic(err) } - }() + } else { + fmt.Println("!!! Not sending interrupt !!!") + } + } + 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)]