Skip to content

Commit

Permalink
chore: related timer with ctx.Done
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Mar 5, 2025
1 parent 3b9752b commit afbf734
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
17 changes: 9 additions & 8 deletions cmd/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"time"

"github.com/go-acme/lego/v4/certificate"
"github.com/go-acme/lego/v4/log"
)

const (
Expand Down Expand Up @@ -49,13 +48,15 @@ func launchHook(hook string, timeout time.Duration, meta map[string]string) erro
return fmt.Errorf("start command: %w", err)
}

timer := time.AfterFunc(timeout, func() {
log.Println("hook timed out: killing command")
_ = cmd.Process.Kill()
_ = stdout.Close()
})

defer timer.Stop()
go func() {
select {

Check failure on line 52 in cmd/hook.go

View workflow job for this annotation

GitHub Actions / Main Process

S1000: should use a simple channel send/receive instead of `select` with a single case (gosimple)
case <-ctxCmd.Done():
if ctxCmd.Err() != nil {
_ = cmd.Process.Kill()
_ = stdout.Close()
}
}
}()

scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
Expand Down
5 changes: 5 additions & 0 deletions cmd/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import (
"github.com/stretchr/testify/require"
)

func Test_launchHook(t *testing.T) {
err := launchHook("echo foo", 1*time.Second, map[string]string{})
require.NoError(t, err)
}

func Test_launchHook_errors(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("skipping test on Windows")
Expand Down

0 comments on commit afbf734

Please sign in to comment.