only create context once

This commit is contained in:
marwan-at-work 2019-08-06 12:55:29 -04:00
parent 006fad30ca
commit cee005ee62

View File

@ -28,6 +28,13 @@ type Context struct {
// NewContext creates a new context. For use in when invoking an App or Command action. // 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 { func NewContext(app *App, set *flag.FlagSet, parentCtx *Context) *Context {
c := &Context{App: app, flagSet: set, parentContext: parentCtx} 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()) ctx, cancel := context.WithCancel(context.Background())
go func() { go func() {
defer cancel() defer cancel()
@ -36,9 +43,6 @@ func NewContext(app *App, set *flag.FlagSet, parentCtx *Context) *Context {
<-sigs <-sigs
}() }()
c.Context = ctx c.Context = ctx
if parentCtx != nil {
c.shellComplete = parentCtx.shellComplete
} }
return c return c