Compare commits

...

3 Commits

Author SHA1 Message Date
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
3 changed files with 13 additions and 8 deletions

View File

@ -12,7 +12,7 @@ type Command struct {
Configure func(c *cobra.Command) 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 { if f != nil {
f(c.Command) f(c.Command)
} }

View File

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

View File

@ -1,6 +1,7 @@
package application package application
import ( import (
"fmt"
"io" "io"
"log/slog" "log/slog"
"os" "os"
@ -24,6 +25,10 @@ var (
outWriter io.Writer = os.Stdout outWriter io.Writer = os.Stdout
) )
func SubCommandInitializePrintNameFunc(cmd *cobra.Command) {
fmt.Println("Initializing:", cmd.Name())
}
func HelpFuncE(cmd *cobra.Command, args []string) error { func HelpFuncE(cmd *cobra.Command, args []string) error {
return cmd.Help() 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 // 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())) 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 cmd.RunE = HelpFuncE
@ -96,7 +101,7 @@ func persistentPostRunFuncE(cmd *cobra.Command, args []string) error {
return nil return nil
} }
// appRunE is an empty catch function to allow overrides through persistentPreRunE // RunCatchFuncE is an empty catch function to allow overrides through persistentPreRunE
func runFuncE(cmd *cobra.Command, args []string) error { func RunCatchFuncE(cmd *cobra.Command, args []string) error {
return nil return nil
} }