From cee005ee62349b316defe3cb9c72099f9b118c52 Mon Sep 17 00:00:00 2001 From: marwan-at-work Date: Tue, 6 Aug 2019 12:55:29 -0400 Subject: [PATCH] only create context once --- context.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/context.go b/context.go index 0109f45..427f737 100644 --- a/context.go +++ b/context.go @@ -28,17 +28,21 @@ type Context struct { // NewContext creates a new context. For use in when invoking an App or Command action. func NewContext(app *App, set *flag.FlagSet, parentCtx *Context) *Context { c := &Context{App: app, flagSet: set, parentContext: parentCtx} - ctx, cancel := context.WithCancel(context.Background()) - go func() { - defer cancel() - sigs := make(chan os.Signal, 1) - signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) - <-sigs - }() - c.Context = ctx - if parentCtx != nil { + if parentCtx.Context != nil { + parentCtx.Context = context.Background() + } + c.Context = parentCtx.Context c.shellComplete = parentCtx.shellComplete + } else { + ctx, cancel := context.WithCancel(context.Background()) + go func() { + defer cancel() + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + <-sigs + }() + c.Context = ctx } return c