From a11c509bb45bc3010291d888f750a9265c203633 Mon Sep 17 00:00:00 2001 From: "cilium-renovate[bot]" <134692979+cilium-renovate[bot]@users.noreply.github.com> Date: Fri, 31 Jan 2025 11:20:35 +0000 Subject: [PATCH] fix(deps): update module github.com/alecthomas/kong to v1.7.0 Signed-off-by: cilium-renovate[bot] <134692979+cilium-renovate[bot]@users.noreply.github.com> --- contrib/tetragon-rthooks/go.mod | 2 +- contrib/tetragon-rthooks/go.sum | 4 +- .../github.com/alecthomas/kong/README.md | 41 +++++++++-- .../github.com/alecthomas/kong/callbacks.go | 71 ++++++++++++++----- .../github.com/alecthomas/kong/context.go | 3 + contrib/tetragon-rthooks/vendor/modules.txt | 2 +- 6 files changed, 99 insertions(+), 24 deletions(-) diff --git a/contrib/tetragon-rthooks/go.mod b/contrib/tetragon-rthooks/go.mod index 81b8c31de77..a70e0121d07 100644 --- a/contrib/tetragon-rthooks/go.mod +++ b/contrib/tetragon-rthooks/go.mod @@ -6,7 +6,7 @@ go 1.23.0 toolchain go1.23.1 require ( - github.com/alecthomas/kong v1.6.1 + github.com/alecthomas/kong v1.7.0 github.com/cilium/lumberjack/v2 v2.4.1 github.com/cilium/tetragon/api v0.0.0-00010101000000-000000000000 github.com/containerd/containerd v1.7.25 diff --git a/contrib/tetragon-rthooks/go.sum b/contrib/tetragon-rthooks/go.sum index f807de80db6..df90eb90a0b 100644 --- a/contrib/tetragon-rthooks/go.sum +++ b/contrib/tetragon-rthooks/go.sum @@ -7,8 +7,8 @@ github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0 github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8vS6K3D0= github.com/alecthomas/assert/v2 v2.11.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k= -github.com/alecthomas/kong v1.6.1 h1:/7bVimARU3uxPD0hbryPE8qWrS3Oz3kPQoxA/H2NKG8= -github.com/alecthomas/kong v1.6.1/go.mod h1:p2vqieVMeTAnaC83txKtXe8FLke2X07aruPWXyMPQrU= +github.com/alecthomas/kong v1.7.0 h1:MnT8+5JxFDCvISeI6vgd/mFbAJwueJ/pqQNzZMsiqZE= +github.com/alecthomas/kong v1.7.0/go.mod h1:p2vqieVMeTAnaC83txKtXe8FLke2X07aruPWXyMPQrU= github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc= github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4= github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI= diff --git a/contrib/tetragon-rthooks/vendor/github.com/alecthomas/kong/README.md b/contrib/tetragon-rthooks/vendor/github.com/alecthomas/kong/README.md index 4a86251ca96..3b623443eda 100644 --- a/contrib/tetragon-rthooks/vendor/github.com/alecthomas/kong/README.md +++ b/contrib/tetragon-rthooks/vendor/github.com/alecthomas/kong/README.md @@ -13,7 +13,8 @@ - [Command handling](#command-handling) - [Switch on the command string](#switch-on-the-command-string) - [Attach a `Run(...) error` method to each command](#attach-a-run-error-method-to-each-command) -- [Hooks: BeforeReset(), BeforeResolve(), BeforeApply(), AfterApply() and the Bind() option](#hooks-beforereset-beforeresolve-beforeapply-afterapply-and-the-bind-option) +- [Hooks: BeforeReset(), BeforeResolve(), BeforeApply(), AfterApply()](#hooks-beforereset-beforeresolve-beforeapply-afterapply) +- [The Bind() option](#the-bind-option) - [Flags](#flags) - [Commands and sub-commands](#commands-and-sub-commands) - [Branching positional arguments](#branching-positional-arguments) @@ -305,7 +306,7 @@ func main() { ``` -## Hooks: BeforeReset(), BeforeResolve(), BeforeApply(), AfterApply() and the Bind() option +## Hooks: BeforeReset(), BeforeResolve(), BeforeApply(), AfterApply() If a node in the CLI, or any of its embedded fields, has a `BeforeReset(...) error`, `BeforeResolve (...) error`, `BeforeApply(...) error` and/or `AfterApply(...) error` method, those @@ -314,8 +315,6 @@ and after validation/assignment, respectively. The `--help` flag is implemented with a `BeforeReset` hook. -Arguments to hooks are provided via the `Run(...)` method or `Bind(...)` option. `*Kong`, `*Context` and `*Path` are also bound and finally, hooks can also contribute bindings via `kong.Context.Bind()` and `kong.Context.BindTo()`. - eg. ```go @@ -341,6 +340,40 @@ func main() { } ``` +## The Bind() option + +Arguments to hooks are provided via the `Run(...)` method or `Bind(...)` option. `*Kong`, `*Context`, `*Path` and parent commands are also bound and finally, hooks can also contribute bindings via `kong.Context.Bind()` and `kong.Context.BindTo()`. + +eg: + +```go +type CLI struct { + Debug bool `help:"Enable debug mode."` + + Rm RmCmd `cmd:"" help:"Remove files."` + Ls LsCmd `cmd:"" help:"List paths."` +} + +type AuthorName string + +// ... +func (l *LsCmd) Run(cli *CLI) error { +// use cli.Debug here !! + return nil +} + +func (r *RmCmD) Run(author AuthorName) error{ +// use binded author here + return nil +} + +func main() { + var cli CLI + + ctx := kong.Parse(&cli, Bind(AuthorName("penguin"))) + err := ctx.Run() +``` + ## Flags Any [mapped](#mapper---customising-how-the-command-line-is-mapped-to-go-values) field in the command structure _not_ tagged with `cmd` or `arg` will be a flag. Flags are optional by default. diff --git a/contrib/tetragon-rthooks/vendor/github.com/alecthomas/kong/callbacks.go b/contrib/tetragon-rthooks/vendor/github.com/alecthomas/kong/callbacks.go index c1fac817814..4644c5487c7 100644 --- a/contrib/tetragon-rthooks/vendor/github.com/alecthomas/kong/callbacks.go +++ b/contrib/tetragon-rthooks/vendor/github.com/alecthomas/kong/callbacks.go @@ -34,8 +34,17 @@ func (b bindings) addTo(impl, iface any) { func (b bindings) addProvider(provider any) error { pv := reflect.ValueOf(provider) t := pv.Type() - if t.Kind() != reflect.Func || t.NumOut() != 2 || t.Out(1) != reflect.TypeOf((*error)(nil)).Elem() { - return fmt.Errorf("%T must be a function with the signature func(...)(T, error)", provider) + if t.Kind() != reflect.Func { + return fmt.Errorf("%T must be a function", provider) + } + + if t.NumOut() == 0 { + return fmt.Errorf("%T must be a function with the signature func(...)(T, error) or func(...) T", provider) + } + if t.NumOut() == 2 { + if t.Out(1) != reflect.TypeOf((*error)(nil)).Elem() { + return fmt.Errorf("missing error; %T must be a function with the signature func(...)(T, error) or func(...) T", provider) + } } rt := pv.Type().Out(0) b[rt] = provider @@ -68,23 +77,53 @@ func getMethod(value reflect.Value, name string) reflect.Value { return method } -// Get methods from the given value and any embedded fields. +// getMethods gets all methods with the given name from the given value +// and any embedded fields. +// +// Returns a slice of bound methods that can be called directly. func getMethods(value reflect.Value, name string) []reflect.Value { - // Collect all possible receivers - receivers := []reflect.Value{value} - if value.Kind() == reflect.Ptr { - value = value.Elem() - } - if value.Kind() == reflect.Struct { - t := value.Type() - for i := 0; i < value.NumField(); i++ { - field := value.Field(i) - fieldType := t.Field(i) - if fieldType.IsExported() && fieldType.Anonymous { - receivers = append(receivers, field) + // Traverses embedded fields of the struct + // starting from the given value to collect all possible receivers + // for the given method name. + var traverse func(value reflect.Value, receivers []reflect.Value) []reflect.Value + traverse = func(value reflect.Value, receivers []reflect.Value) []reflect.Value { + // Always consider the current value for hooks. + receivers = append(receivers, value) + + if value.Kind() == reflect.Ptr { + value = value.Elem() + } + + // If the current value is a struct, also consider embedded fields. + // Two kinds of embedded fields are considered if they're exported: + // + // - standard Go embedded fields + // - fields tagged with `embed:""` + if value.Kind() == reflect.Struct { + t := value.Type() + for i := 0; i < value.NumField(); i++ { + fieldValue := value.Field(i) + field := t.Field(i) + + if !field.IsExported() { + continue + } + + // Consider a field embedded if it's actually embedded + // or if it's tagged with `embed:""`. + _, isEmbedded := field.Tag.Lookup("embed") + isEmbedded = isEmbedded || field.Anonymous + if isEmbedded { + receivers = traverse(fieldValue, receivers) + } } } + + return receivers } + + receivers := traverse(value, nil /* receivers */) + // Search all receivers for methods var methods []reflect.Value for _, receiver := range receivers { @@ -131,7 +170,7 @@ func callAnyFunction(f reflect.Value, bindings bindings) (out []any, err error) if err != nil { return nil, fmt.Errorf("%s: %w", pt, err) } - if ferrv := reflect.ValueOf(argv[len(argv)-1]); ferrv.IsValid() && !ferrv.IsNil() { + if ferrv := reflect.ValueOf(argv[len(argv)-1]); ferrv.IsValid() && ferrv.Type().Implements(callbackReturnSignature) && !ferrv.IsNil() { return nil, ferrv.Interface().(error) //nolint:forcetypeassert } in = append(in, reflect.ValueOf(argv[0])) diff --git a/contrib/tetragon-rthooks/vendor/github.com/alecthomas/kong/context.go b/contrib/tetragon-rthooks/vendor/github.com/alecthomas/kong/context.go index b6a56e33cc5..ebf4c31dd12 100644 --- a/contrib/tetragon-rthooks/vendor/github.com/alecthomas/kong/context.go +++ b/contrib/tetragon-rthooks/vendor/github.com/alecthomas/kong/context.go @@ -119,6 +119,9 @@ func (c *Context) BindTo(impl, iface any) { // // This is useful when the Run() function of different commands require different values that may // not all be initialisable from the main() function. +// +// "provider" must be a function with the signature func(...) (T, error) or func(...) T, where +// ... will be recursively injected with bound values. func (c *Context) BindToProvider(provider any) error { return c.bindings.addProvider(provider) } diff --git a/contrib/tetragon-rthooks/vendor/modules.txt b/contrib/tetragon-rthooks/vendor/modules.txt index 91324eca5a4..95fba9e15fe 100644 --- a/contrib/tetragon-rthooks/vendor/modules.txt +++ b/contrib/tetragon-rthooks/vendor/modules.txt @@ -4,7 +4,7 @@ cel.dev/expr # dario.cat/mergo v1.0.1 ## explicit; go 1.13 dario.cat/mergo -# github.com/alecthomas/kong v1.6.1 +# github.com/alecthomas/kong v1.7.0 ## explicit; go 1.20 github.com/alecthomas/kong # github.com/antlr4-go/antlr/v4 v4.13.0