Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command.Context() returns nil instead of context.Background() #1578

Closed
katexochen opened this issue Jan 10, 2022 · 1 comment · Fixed by #1639
Closed

Command.Context() returns nil instead of context.Background() #1578

katexochen opened this issue Jan 10, 2022 · 1 comment · Fixed by #1639
Labels
area/cobra-command Core `cobra.Command` implementations lifecycle/active Actively being worked on by a community member or maintainer. Corresponds to someone being assigned

Comments

@katexochen
Copy link
Contributor

katexochen commented Jan 10, 2022

Documentation of the Context() function states a context.Background() is returned, but the returned value is nil.

Context returns underlying command context. If command wasn't executed with ExecuteContext Context returns Background context.

cmd := &cobra.Command{}
fmt.Printf("context is %v", cmd.Context()) // returns "context is <nil>"

Go Playground

It would be really nice if this could return a context.Background(), so functions that take a Command as parameter and use the context from that command could be tested. There is also no possibility to set this context manually right now.

@johnSchnake johnSchnake added area/cobra-command Core `cobra.Command` implementations lifecycle/active Actively being worked on by a community member or maintainer. Corresponds to someone being assigned labels Mar 1, 2022
@marckhouzam
Copy link
Collaborator

For info, setting the context manually has just been added in #1551

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/cobra-command Core `cobra.Command` implementations lifecycle/active Actively being worked on by a community member or maintainer. Corresponds to someone being assigned
Projects
None yet
3 participants