only create context once
@@ -28,6 +28,13 @@ 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}
+ 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()
@@ -36,9 +43,6 @@ func NewContext(app *App, set *flag.FlagSet, parentCtx *Context) *Context {
<-sigs
}()
c.Context = ctx
-
- if parentCtx != nil {
- c.shellComplete = parentCtx.shellComplete
}
return c
