-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Conversation
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. |
Thanks @crochee. |
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: |
Would really like this feature; 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 Needing to use |
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
No description provided.