Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: arduino/go-paths-helper
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 6444974
Choose a base ref
...
head repository: arduino/go-paths-helper
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 73c658f
Choose a head ref
  • 2 commits
  • 1 file changed
  • 1 contributor

Commits on Apr 10, 2025

  1. Added WaitWithinContext method.

    cmaglie committed Apr 10, 2025
    Copy the full SHA
    12020ac View commit details
  2. Merge pull request #33 from arduino/add-wait-ctx

    Added WaitWithinContext method.
    cmaglie authored Apr 10, 2025

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    73c658f View commit details
Showing with 16 additions and 10 deletions.
  1. +16 −10 process.go
26 changes: 16 additions & 10 deletions process.go
Original file line number Diff line number Diff line change
@@ -138,6 +138,21 @@ func (p *Process) Wait() error {
return p.cmd.Wait()
}

// WaitWithinContext wait for the process to complete. If the given context is canceled
// before the normal process termination, the process is killed.
func (p *Process) WaitWithinContext(ctx context.Context) error {
completed := make(chan struct{})
defer close(completed)
go func() {
select {
case <-ctx.Done():
p.Kill()
case <-completed:
}
}()
return p.Wait()
}

// Signal sends a signal to the Process. Sending Interrupt on Windows is not implemented.
func (p *Process) Signal(sig os.Signal) error {
return p.cmd.Process.Signal(sig)
@@ -188,16 +203,7 @@ func (p *Process) RunWithinContext(ctx context.Context) error {
if err := p.Start(); err != nil {
return err
}
completed := make(chan struct{})
defer close(completed)
go func() {
select {
case <-ctx.Done():
p.Kill()
case <-completed:
}
}()
return p.Wait()
return p.WaitWithinContext(ctx)
}

// RunAndCaptureOutput starts the specified command and waits for it to complete. If the given context