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

feat: add cobra cmd to context #9

Merged
merged 6 commits into from
Dec 20, 2024
Merged

feat: add cobra cmd to context #9

merged 6 commits into from
Dec 20, 2024

Conversation

raulb
Copy link
Member

@raulb raulb commented Dec 19, 2024

This could be useful on situations such as invoking a native cobra command (e.g.: help):

func (c *RootCommand) Execute(ctx context.Context) error {
	if c.flags.Version {
		_, _ = fmt.Fprintf(os.Stdout, "%s\n", conduit.Version(true))
		return nil
	}

	if cmd, ok := ctx.Value(ecdysis.CobraCtxCmd{}).(*cobra.Command); ok {
		return cmd.Help()
	}

	return nil
}

An alternative I had in mind was having another decorator named ExecuteWithCobraCommand which would be ExecuteWithCobraCommand(ctx context.Context, cmd *cobra.Command) error, but I'm not sure I'm in love with the idea of having two separate Execute methods. For situations like this one, it could be useful simply use the context.

@raulb raulb requested a review from a team as a code owner December 19, 2024 20:34
@raulb raulb self-assigned this Dec 19, 2024
Copy link

@hariso hariso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! I had only one tiny thing to suggest.

decorators.go Outdated Show resolved Hide resolved
@raulb raulb merged commit 4a9a7e1 into main Dec 20, 2024
3 checks passed
@raulb raulb deleted the add-cobra-to-context branch December 20, 2024 12:46
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.

2 participants