Skip to content

Commit

Permalink
add before & after hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
avirtopeanu-ionos committed Nov 16, 2023
1 parent 42bd7e3 commit 67a4531
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cobra-prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ type CobraPrompt struct {
// OnErrorFunc handle error for command.Execute, if not set print error and exit
OnErrorFunc func(err error)

// HookAfter is a hook that will be executed after a command has been executed
HookAfter func(input string)

// HookBefore is a hook that will be executed before a command has been executed
HookBefore func(input string)

// InArgsParser adds a custom parser for the command line arguments (default: strings.Fields)
InArgsParser func(args string) []string

Expand Down Expand Up @@ -80,6 +86,7 @@ func (co *CobraPrompt) RunContext(ctx context.Context) {

func (co *CobraPrompt) executeCommand(ctx context.Context) func(string) {
return func(input string) {
co.HookBefore(input)
args := co.parseInput(input)
os.Args = append([]string{os.Args[0]}, args...)
if err := co.RootCmd.ExecuteContext(ctx); err != nil {
Expand All @@ -90,6 +97,7 @@ func (co *CobraPrompt) executeCommand(ctx context.Context) func(string) {
os.Exit(1)
}
}
co.HookAfter(input)
}
}

Expand Down

0 comments on commit 67a4531

Please sign in to comment.