Skip to content

Commit

Permalink
feat: pre, postrun hooks now have access to the current command (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
avirtopeanu-ionos authored Nov 24, 2023
1 parent 8561f73 commit d9e3a35
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.3] - 2023-11-24

### Changed
* changed `HookBefore` and `HookAfter` to have access to the command object that is about to be / has been executed.


## [1.0.2] - 2023-11-24

### Fixed
Expand Down
24 changes: 13 additions & 11 deletions comptplus.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ type CobraPrompt struct {
// PersistFlagValues will persist flags. For example have verbose turned on every command.
PersistFlagValues bool

// CustomFlagResetBehaviour allows you to specify custom behaviour which will be ran after each command, if PersistFlagValues is false
CustomFlagResetBehaviour func(pflag *pflag.Flag)
// CustomFlagResetBehaviour allows you to specify custom behaviour which will be run after each command, if PersistFlagValues is false
CustomFlagResetBehaviour func(flag *pflag.Flag)

// ShowHelpCommandAndFlags will make help command and flag for every command available.
ShowHelpCommandAndFlags bool
Expand All @@ -55,10 +55,10 @@ type CobraPrompt struct {
OnErrorFunc func(err error)

// HookAfter is a hook that will be executed every time after a command has been executed
HookAfter func(input string)
HookAfter func(cmd *cobra.Command, input string)

// HookBefore is a hook that will be executed every time a command has been executed
HookBefore func(input string)
// HookBefore is a hook that will be executed every time before a command is executed
HookBefore func(cmd *cobra.Command, input string)

// InArgsParser adds a custom parser for the command line arguments (default: strings.Fields)
InArgsParser func(args string) []string
Expand All @@ -83,11 +83,11 @@ func (co *CobraPrompt) RunContext(ctx context.Context) {
}

if co.HookBefore == nil {
co.HookBefore = func(_ string) {}
co.HookBefore = func(_ *cobra.Command, _ string) {}
}

if co.HookAfter == nil {
co.HookAfter = func(_ string) {}
co.HookAfter = func(_ *cobra.Command, _ string) {}
}

if co.CustomFlagResetBehaviour == nil {
Expand All @@ -107,7 +107,7 @@ func (co *CobraPrompt) RunContext(ctx context.Context) {
// If there's an error parsing defaultSlice as a slice, try this workaround
errShouldNeverHappenButWeAreProfessionals := sliceValue.Replace([]string{})
if errShouldNeverHappenButWeAreProfessionals == nil {
// If this check wouldn't exist and we would have some error parsing the nil value,
// If this check wouldn't exist, and we would have some error parsing the nil value,
// it would actually append the default value to the previous user's value
flag.Value.Set(flag.DefValue)
}
Expand Down Expand Up @@ -145,9 +145,12 @@ func (co *CobraPrompt) resetFlagsToDefault(cmd *cobra.Command) {

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...)
executedCmd, _, _ := co.RootCmd.Find(os.Args[1:])

co.HookBefore(executedCmd, input)

if err := co.RootCmd.ExecuteContext(ctx); err != nil {
if co.OnErrorFunc != nil {
co.OnErrorFunc(err)
Expand All @@ -157,10 +160,9 @@ func (co *CobraPrompt) executeCommand(ctx context.Context) func(string) {
}
}
if !co.PersistFlagValues {
executedCmd, _, _ := co.RootCmd.Find(os.Args[1:])
co.resetFlagsToDefault(executedCmd)
}
co.HookAfter(input)
co.HookAfter(executedCmd, input)
}
}

Expand Down

0 comments on commit d9e3a35

Please sign in to comment.