5 Commits

Author SHA1 Message Date
be9976c6d2 Merge pull request 'dev-centralize' (#1) from dev-centralize into main
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 4s
Reviewed-on: #1
2025-06-09 19:13:37 +00:00
bb83b53b52 Update dependencies
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 35s
Signed-off-by: Jan Tytgat <jan.tytgat@corelayer.eu>
2025-06-05 17:39:55 +02:00
1ef456579a Add debug function to print initializing command names
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 8s
Signed-off-by: Jan Tytgat <jan.tytgat@corelayer.eu>
2025-06-03 14:15:48 +02:00
4112e27ba5 Rename function runFuncE to RunCatchFuncE for reuse
Signed-off-by: Jan Tytgat <jan.tytgat@corelayer.eu>
2025-06-03 14:15:26 +02:00
7f6ea395ee Rename function argument for clarity
Signed-off-by: Jan Tytgat <jan.tytgat@corelayer.eu>
2025-06-03 14:14:46 +02:00
5 changed files with 16 additions and 9 deletions

View File

@ -12,7 +12,7 @@ type Command struct {
Configure func(c *cobra.Command)
}
func (c Command) Initialize(f func(c *cobra.Command)) *cobra.Command {
func (c Command) Initialize(f func(cmd *cobra.Command)) *cobra.Command {
if f != nil {
f(c.Command)
}

View File

@ -48,7 +48,7 @@ func (c Config) getRootCommand() (*cobra.Command, error) {
Long: long,
PersistentPreRunE: persistentPreRunFuncE,
PersistentPostRunE: persistentPostRunFuncE,
RunE: runFuncE,
RunE: RunCatchFuncE,
SilenceErrors: true,
SilenceUsage: true,
}
@ -69,9 +69,9 @@ func (c Config) getRootCommand() (*cobra.Command, error) {
return cmd, nil
}
func (c Config) ParseVersion() (semver.Version, error) {
return semver.Parse(c.Version.Full)
}
// func (c Config) ParseVersion() (semver.Version, error) {
// return semver.Parse(c.Version.Full)
// }
func (c Config) RegisterCommand(cmd Commander, f func(*cobra.Command)) {
appCmd.AddCommand(cmd.Initialize(f))

View File

@ -1,6 +1,7 @@
package application
import (
"fmt"
"io"
"log/slog"
"os"
@ -24,6 +25,10 @@ var (
outWriter io.Writer = os.Stdout
)
func SubCommandInitializePrintNameFunc(cmd *cobra.Command) {
fmt.Println("Initializing:", cmd.Name())
}
func HelpFuncE(cmd *cobra.Command, args []string) error {
return cmd.Help()
}
@ -51,7 +56,7 @@ func persistentPreRunFuncE(cmd *cobra.Command, args []string) error {
}
// Make sure that we show the app help if no commands or flags are passed
if cmd.CalledAs() == appName && runtime.FuncForPC(reflect.ValueOf(cmd.RunE).Pointer()).Name() == runtime.FuncForPC(reflect.ValueOf(runFuncE).Pointer()).Name() {
if cmd.CalledAs() == appName && runtime.FuncForPC(reflect.ValueOf(cmd.RunE).Pointer()).Name() == runtime.FuncForPC(reflect.ValueOf(RunCatchFuncE).Pointer()).Name() {
slogd.FromContext(cmd.Context()).LogAttrs(cmd.Context(), slogd.LevelTrace, "overriding command", slog.String("old_function", runtime.FuncForPC(reflect.ValueOf(cmd.RunE).Pointer()).Name()), slog.String("new_function", runtime.FuncForPC(reflect.ValueOf(HelpFuncE).Pointer()).Name()))
cmd.RunE = HelpFuncE
@ -96,7 +101,7 @@ func persistentPostRunFuncE(cmd *cobra.Command, args []string) error {
return nil
}
// appRunE is an empty catch function to allow overrides through persistentPreRunE
func runFuncE(cmd *cobra.Command, args []string) error {
// RunCatchFuncE is an empty catch function to allow overrides through persistentPreRunE
func RunCatchFuncE(cmd *cobra.Command, args []string) error {
return nil
}

2
go.mod
View File

@ -12,5 +12,5 @@ require (
require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/samber/lo v1.50.0 // indirect
golang.org/x/text v0.25.0 // indirect
golang.org/x/text v0.26.0 // indirect
)

2
go.sum
View File

@ -22,6 +22,8 @@ golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=