Skip to content

Add support for context.Context#893

Merged
bep merged 2 commits into
spf13:masterfrom
burdiyan:master
Feb 20, 2020
Merged

Add support for context.Context#893
bep merged 2 commits into
spf13:masterfrom
burdiyan:master

Conversation

@burdiyan

@burdiyan burdiyan commented Jun 21, 2019

Copy link
Copy Markdown
Contributor

This PR adds support for context.Context. It could help people struggling with passing context to the Run functions, that previously had to wrap cobra.Command initialization inside another function that would accept context.

So now instead of doing:

func createFooCommand(ctx context.Context) *cobra.Command {
    return &cobra.Command{
        Use: "foo",
        Run: func(cmd *cobra.Command, args []string) {
            // Use context here
            // ...
        }
    }
}

you would do:

cmd := &cobra.Command{
    Use: "foo",
    Run: func(cmd *cobra.Command, args []string) {
        ctx := cmd.Context()
        // Use context here
        // ...
    }
}


// Get ctx from somewhere else.
cmd.ExecuteContext(ctx)

This PR is fully backward-compatible, and doesn't require users to use context at all.

It renders this PR #727 (which seems to have conficlits, and also goes against the idiomatic way of using context) irrelevant.

@CLAassistant

CLAassistant commented Jun 21, 2019

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@cpuguy83

cpuguy83 commented Jul 1, 2019

Copy link
Copy Markdown

This does seem like a decent solution for shoe-horning context support into cobra.Command.

Comment thread command.go
@burdiyan

burdiyan commented Jul 22, 2019

Copy link
Copy Markdown
Contributor Author

I rebased this PR against origin, so that now the build passes (seems like there were some issues upstream previously). So, hope this can be merged soon.

@burdiyan

burdiyan commented Aug 3, 2019

Copy link
Copy Markdown
Contributor Author

Fixed merge conflicts again.

@burdiyan

Copy link
Copy Markdown
Contributor Author

Can this be merged?

@korya

korya commented Aug 23, 2019

Copy link
Copy Markdown
Contributor

The lack of support of context.Context is a huge headache for us. What blocks this PR from getting merged?

PS @burdiyan thanks a lot for the work!

@snovichkov

Copy link
Copy Markdown

@cpuguy83 could you approve and merge this is PR?

@cpuguy83

Copy link
Copy Markdown

I am not a maintainer on this project.

@snovichkov

Copy link
Copy Markdown

@cpuguy83 sorry... @spf13 could you approve and merge this is PR?

@gpaul

gpaul commented Dec 18, 2019

Copy link
Copy Markdown

Bump?

@hairyhenderson

Copy link
Copy Markdown
Contributor

This'd be pretty useful - I've just found myself abusing some global variables pretty badly because of the lack of context.Context support 😉

/cc @bep or @eparis maybe?

@umarcor

umarcor commented Feb 20, 2020

Copy link
Copy Markdown
Contributor

@hairyhenderson, see #959.

@hypergig

Copy link
Copy Markdown

thank you!

I think something in the root README would be helpful here

@aventurella-honey

Copy link
Copy Markdown

Is it possible to update the context in one command Run/PersistentPreRun so that the altered context is what's passed down to the subcommands?

@burdiyan

burdiyan commented Aug 6, 2020

Copy link
Copy Markdown
Contributor Author

@aventurella-honey I believe it's not possible. Because context is immutable and you'll have to store altered context somewhere to pass it on to the subcommands, and currently the only way to pass the context is to call ExecuteContext.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.