From 6ae998a2127ead519eb3a7baa2de7dec3b81d882 Mon Sep 17 00:00:00 2001 From: Nate Finch Date: Fri, 24 Aug 2018 09:31:33 -0400 Subject: [PATCH] initial code not working yet --- mage/template.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/mage/template.go b/mage/template.go index 3ee2537b..8256b57b 100644 --- a/mage/template.go +++ b/mage/template.go @@ -46,21 +46,36 @@ func main() { if os.Getenv("MAGEFILE_TIMEOUT") != "" { timeout, err := time.ParseDuration(os.Getenv("MAGEFILE_TIMEOUT")) if err != nil { - fmt.Printf("timeout error: %v\n", err) + fmt.Printf("error parsing MAGEFILE_TIMEOUT: %v\n", err) os.Exit(1) } ctx, ctxCancel = context.WithTimeout(context.Background(), timeout) } else { - ctx = context.Background() - ctxCancel = func() {} + ctx, ctxCancel = context.WithCancel(context.Background()) } + return ctx, ctxCancel } runTarget := func(fn func(context.Context) error) interface{} { var err interface{} ctx, cancel := getContext() + + // trap Ctrl+C and call cancel on the context + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + defer func() { + signal.Stop(c) + }() + go func() { + select { + case <-c: + cancel() + case <-ctx.Done(): + } + }() + d := make(chan interface{}) go func() { defer func() {