From 17479e8b6f1d1f7ad5e654afa693913fb042577d Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Fri, 31 Jan 2025 02:22:45 +0000 Subject: [PATCH] Renovate: Update module github.com/alecthomas/kong to v1.7.0 --- go.mod | 2 +- go.sum | 4 +- vendor/github.com/alecthomas/kong/README.md | 41 +++++++++-- .../github.com/alecthomas/kong/callbacks.go | 71 ++++++++++++++----- vendor/github.com/alecthomas/kong/context.go | 3 + vendor/modules.txt | 2 +- 6 files changed, 99 insertions(+), 24 deletions(-) diff --git a/go.mod b/go.mod index 542753c..5c90f4e 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/sapcc/swift-health-exporter go 1.23 require ( - github.com/alecthomas/kong v1.6.1 + github.com/alecthomas/kong v1.7.0 github.com/gorilla/mux v1.8.1 github.com/prometheus/client_golang v1.20.5 github.com/sapcc/go-api-declarations v1.13.2 diff --git a/go.sum b/go.sum index 6f78e16..2a3f26a 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ 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/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= diff --git a/vendor/github.com/alecthomas/kong/README.md b/vendor/github.com/alecthomas/kong/README.md index 4a86251..3b62344 100644 --- a/vendor/github.com/alecthomas/kong/README.md +++ b/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/vendor/github.com/alecthomas/kong/callbacks.go b/vendor/github.com/alecthomas/kong/callbacks.go index c1fac81..4644c54 100644 --- a/vendor/github.com/alecthomas/kong/callbacks.go +++ b/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/vendor/github.com/alecthomas/kong/context.go b/vendor/github.com/alecthomas/kong/context.go index b6a56e3..ebf4c31 100644 --- a/vendor/github.com/alecthomas/kong/context.go +++ b/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/vendor/modules.txt b/vendor/modules.txt index 719b40b..2a1b50c 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,4 +1,4 @@ -# github.com/alecthomas/kong v1.6.1 +# github.com/alecthomas/kong v1.7.0 ## explicit; go 1.20 github.com/alecthomas/kong # github.com/beorn7/perks v1.0.1