Skip to content

Commit

Permalink
wtf!
Browse files Browse the repository at this point in the history
  • Loading branch information
tamird committed Oct 18, 2024
1 parent bd3221d commit c07bc7e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 deletions.
50 changes: 21 additions & 29 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import (
"bufio"
"context"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
)

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion xtask/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit c07bc7e

Please sign in to comment.