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

AfterFunc execution details #1917

Closed
shrimalmadhur opened this issue May 26, 2024 · 7 comments
Closed

AfterFunc execution details #1917

shrimalmadhur opened this issue May 26, 2024 · 7 comments
Labels
area/v2 relates to / is being considered for v2 kind/question someone asking a question status/triage maintainers still need to look into this

Comments

@shrimalmadhur
Copy link

I am using AfterFunc feature and adding it to the root command

// AfterFunc is an action to execute after any subcommands are run, but after the
// subcommand has finished it is run even if Action() panics
type AfterFunc func(*Context) error

According to documentation it will execute after every sub command too. The issue I'm facing is that the *Context doesn't have knowledge of full command.

For example if my command is cmd opreration1 operatione the ctx.Command.FullName() gives back cmd. I am wondering if that's the expected behavior and I have to add After in all the leaf commands to get that. If that's the expected behavior, my question is, why not have the current subcommand execution context in After function so that I can get command full name by only attaching AfterFunc to root command?

@shrimalmadhur shrimalmadhur added area/v2 relates to / is being considered for v2 kind/question someone asking a question status/triage maintainers still need to look into this labels May 26, 2024
@dearchap
Copy link
Contributor

@shrimalmadhur The AfterFunc will be run only once after the subcommand actions have been completed. So in your case of cmd operation1 operation2 the AfterFunc defined for "cmd" will be executed once in the same command context after actions for operation1, operation2 have been completed. I am not entirely clear what you want to achieve. If you want a single function to handle all your actions you could attach the same function to all the Actions/After/Before for each of the subcommands individually. There is no way for the cli library to do this automatically for you

@shrimalmadhur
Copy link
Author

@dearchap thanks for the answer. my detailed case is I want to send telemetry data to an endpoint after any command gets executed - I need the whole command path while sending telemetry.
From what you are saying every I add a new command I will have to mention that function in After field. The thing which I wanted to achieve is that I just define that in root command and since it gets executed every time - the context would have that information.
But I guess I understand that the context is very specific to each command for AfterFunc

@dearchap
Copy link
Contributor

Yes that's correct. If you pass in the same function in all the places the context for the command will be passed to you

@shrimalmadhur
Copy link
Author

shrimalmadhur commented May 26, 2024

I see but interestingly I found an issue in v2.27.2

// FullName returns the full name of the command.
// For subcommands this ensures that parent commands are part of the command path
func (c *Command) FullName() string {
	if c.commandNamePath == nil {
		return c.Name
	}
	return strings.Join(c.commandNamePath, " ")
}

This always returns only the leaf command path but it's supposed to return whole command path. I don't see c.commandNamePath set somewhere.

In the latest master branch, the code is

// FullName returns the full name of the command.
// For commands with parents this ensures that the parent commands
// are part of the command path.
func (cmd *Command) FullName() string {
	namePath := []string{}

	if cmd.parent != nil {
		namePath = append(namePath, cmd.parent.FullName())
	}

	return strings.Join(append(namePath, cmd.Name), " ")
}

This will insure parent command path is also included

@shrimalmadhur
Copy link
Author

ok c.Command.HelpName actually gives the full command I need. I can wait for v3 to get released for using the FullName() feature.

@dearchap
Copy link
Contributor

@shrimalmadhur you can start using v3, It is almost production ready.

@shrimalmadhur
Copy link
Author

@dearchap oh awesome. excited for using v3. Thanks for the update, I will close this issue now.

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

No branches or pull requests

2 participants