Add Command.SetContext (#1551)

Increases flexibility in how Contexts can be used with Cobra.
This commit is contained in:
Joshua Carpeggiani
2022-03-18 21:01:58 +11:00
committed by GitHub
parent 5d066b77b5
commit f848943afd
2 changed files with 109 additions and 0 deletions

View File

@ -230,6 +230,12 @@ func (c *Command) Context() context.Context {
return c.ctx
}
// SetContext sets context for the command. It is set to context.Background by default and will be overwritten by
// Command.ExecuteContext or Command.ExecuteContextC
func (c *Command) SetContext(ctx context.Context) {
c.ctx = ctx
}
// SetArgs sets arguments for the command. It is set to os.Args[1:] by default, if desired, can be overridden
// particularly useful when testing.
func (c *Command) SetArgs(a []string) {