-
-
Notifications
You must be signed in to change notification settings - Fork 138
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
Hook ignored on nodes tagged with "embed" #90
Comments
I'm not sure about the semantics or usefulness of this. What are the use cases exactly? Regarding semantics, normally for hooks, a The final option is to call the embed callbacks with no context at all, which would work but is it useful? |
Perhaps I'm holding it wrong. This is my use case: I have flag that takes a file path. I want this flag to default to foo.txt if the file foo.txt exists, otherwise I want it to default to "". I want this flag to be shared by multiple commands and I don't want each command to do extra work to deal with this flag otherwise I'll forget to call this logic eventually in some command. |
Ah. In this case I think you want to create your own flag type and implement one of the hook methods on it. eg. type MaybeFile string
func (m *MaybeFile) BeforeApply(...) error {
// ...
} |
FYI there is also some question over whether the |
Yes I know I can use a custom type but I hoped I could not force this custom type to the body of the commands. |
Also, with flag types the logic can only handle one flag at a time |
Right, but you only referred to a single flag originally. That said, I can see that it would be useful. I think the simplest solution would be to support hooks but there would be no context directly associated with the embedded struct. |
I don't understand why can't those structs get the context. I guess usually there will be multiple instances of them, since they will be declared as values. Sure, they could be defined as pointers and they could point to the same instance, but I'm not sure why somebody would end up doing that. |
I think I'm running into the same issue. My usecase might be a bit different, where I'm embedding a struct that has closely-related flags, and I'd like the logic to be at the struct level. For example: type VersionFlag struct {
Enabled bool `short:"v" env:"-" name:"version" help:"prints version information and exits"`
EnabledJSON bool `name:"version-json" env:"-" help:"prints version information in JSON format and exits"`
}
func (v VersionFlag) BeforeApply([...]) error {
// [...]
return nil
}
type CLI struct {
Foo string `etc`
Version VersionFlag `embed:""`
} Ideally, I don't want these flags to be a prefixed parent flag, but I also don't want to have a BeforeApply or similar for each individual flag. The primary reasoning for structuring it like this is that I have a library I currently use with another cli library, and I'd like to move over to Kong. That library has pluggable functionality, like:
Also looked at plugins, but I wasn't able to get plugins to work in the above scenario either (would be very nice just to pass in a plugin that has hooks setup). |
@lrstanley I'm also looking at this, probably a similar approach taken by: https://github.com/alecthomas/mango-kong can be used (?). |
It seems this also makes custom validators not run on nested structs: https://go.dev/play/p/ROe5n4bp1AD I think semantics for validators is clearer than for hooks? |
Will it be fixed? |
The |
Faced with the same issue. I tried to encapsulate some flags with try for reuse and include it with type TestCmd struct {
Query QueryFlags `embed:""`
}
type LogQueryFlags struct {
Source string
}
func (f *QueryFlags) Validate() error {
fmt.Println("!!!")
} |
In the following example I created a struct called
Common
with anAfterApply
hook.When this structure is embedded using Go struct embedding, the AfterApply hook gets invoked as per Go method discovery rules.
However, if the struct is embedded using the kong embedding (via the
embed
tag), the hook is not invoked.I think that would be useful since this allows multiple structures with common flags to be embedded and have each of their hooks invoked. This is currently not possible with Go embedding since it doesn't work if more than one structure (including the containing struct) defines the hook method.
The text was updated successfully, but these errors were encountered: