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

Ability to add context values #1927

Open
3 tasks done
nejtr0n opened this issue Jun 24, 2024 · 2 comments
Open
3 tasks done

Ability to add context values #1927

nejtr0n opened this issue Jun 24, 2024 · 2 comments
Labels
area/v3 relates to / is being considered for v3 status/triage maintainers still need to look into this

Comments

@nejtr0n
Copy link

nejtr0n commented Jun 24, 2024

Checklist

  • Are you running the latest v3 release? The list of releases is here.
  • Did you check the manual for your release? The v3 manual is here.
  • Did you perform a search about this feature? Here's the GitHub guide about searching.

What problem does this solve?

In old versions we could add context fields in Before function like this

  Before: func(c *cli.Context) error {
      db := "example"
      c.Context = context.WithValue(c.Context, "db", db)
      return nil
  },

It's useful for example for passing logger through all commands.

Solution description

Add some middleware layer to command with signature

func(ctx context.Context, command *cli.Command) func(ctx context.Context, command *cli.Command)

where we could modify passing ctx

Describe alternatives you've considered

Return back Context field to command

@nejtr0n nejtr0n added area/v3 relates to / is being considered for v3 status/triage maintainers still need to look into this labels Jun 24, 2024
@ccoVeille
Copy link
Contributor

I agree context should be available everywhere 🥳

@dearchap
Copy link
Contributor

@nejtr0n What exactly are you trying to achieve ? when calling rootCmd.Run(ctx, args) you can pass in a ctx of your choice with whatever values you define. All subcmds will have a context with parent context set to that so you should have able to retrieve said values. Do you mean you want to pass in a new context to the Action function for the command based on what you set in Before ? I hadnt really thought about that use case. If you can give me a sample of Before/Action functions of your older code I can see what can be done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/v3 relates to / is being considered for v3 status/triage maintainers still need to look into this
Projects
None yet
Development

No branches or pull requests

3 participants