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

add WithContext to set context.context #1517

Closed
wants to merge 1 commit into from
Closed

add WithContext to set context.context #1517

wants to merge 1 commit into from

Conversation

crochee
Copy link

@crochee crochee commented Oct 26, 2021

No description provided.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


crochee seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@marckhouzam
Copy link
Collaborator

Thanks @crochee.
To set the context you can use ExecuteContext(..) or ExecuteContextC(..).
Would that be sufficient?

@crochee
Copy link
Author

crochee commented Oct 27, 2021

ExecuteContext or ExecuteContextC executes the contents of the whole process, but I want to set it before executing the run of the whole process, because some flags at this stage already have values, and I will set its values in the context.for example:
func setInfo(cmd *cobra.Command, _ []string) error {
ctx := v2.SetAccountID(cmd.Context(), viper.GetString("account"))
cmd.WithContext(ctx)
return nil
}

@joshcarp
Copy link
Contributor

joshcarp commented Dec 6, 2021

Would really like this feature;
I want to use a functional option pattern and this is an example that I currently have in my code

type GrptlOption func(*cobra.Command) error

func WithFileDescriptors(descriptors ...protoreflect.FileDescriptor) GrptlOption {
	return func(cmd *cobra.Command) error {
		err := CommandFromFileDescriptors(cmd, descriptors...)
		if err != nil {
			return err
		}
		return nil
	}
}

I've got a lot more options and this pattern allows me to have nice abstracted pieces of logic that interact with the cobra command.

The issue I'm facing at the moment is that I can't write a nifty option (something like WithAuthentication) because that needs access to reading and setting the context. Currently I can only read the context.

Needing to use ExecuteContext breaks this abstraction because I need to run it right at the end

@joshcarp joshcarp mentioned this pull request Dec 6, 2021
@crochee crochee closed this Jan 24, 2022
hoshsadiq pushed a commit to zulucmd/zulu that referenced this pull request Dec 31, 2022
Basically the same as spf13/cobra#1517 but
uses the `Set<field>` naming convention instead of `WithContext`

Context setting without execution is important because it means
that more design patterns can be achieved.

Currently I am using functional options in a project and I can add
behaviour through functional options as such:
```go
type GrptlOption func(*cobra.Command) error

func WithFileDescriptors(descriptors ...protoreflect.FileDescriptor) GrptlOption {
	return func(cmd *cobra.Command) error {
		err := CommandFromFileDescriptors(cmd, descriptors...)
		if err != nil {
			return err
		}
		return nil
	}
}
```

I've got a lot more options and this pattern allows me to have nice
abstracted pieces of logic that interact with the cobra command.

This Pattern also allows for adding extra information to a call
through `PreRun` functions:

```go

cmd := &cobra.Command{
		Use: "Foobar",
		PersistentPreRun: func(cmd *cobra.Command, args []string) {
			err :=cmd.SetContext(metadata.AppendToOutgoingContext(context.Background(), "pre", "run"))
		},
	}
```

This is a veer nice abstraction and allows for these functions to be
very modular

The issue I'm facing at the moment is that I can't write a nifty
option (something like `WithAuthentication`) because that needs access
to reading and setting the context. Currently I can only read the
context.

Needing to use `ExecuteContext` breaks this abstraction because I need
to run it right at the end.

Merge spf13/cobra#1551

Fixes spf13/cobra#1517
Fixes spf13/cobra#1118
Fixes spf13/cobra#563
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.

4 participants