Compare commits
3 Commits
f6e2ff6bbe
...
1ef456579a
Author | SHA1 | Date | |
---|---|---|---|
1ef456579a | |||
4112e27ba5 | |||
7f6ea395ee |
@ -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)
|
||||
}
|
||||
|
@ -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))
|
||||
|
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user