Skip to content

Commit

Permalink
Merge pull request #9 from klowdo/main
Browse files Browse the repository at this point in the history
feat(prompt): add RunContext to pass context to run function
  • Loading branch information
stromland authored Jan 28, 2023
2 parents 5ad2383 + d134165 commit e6f51a2
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion cobra-prompt.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cobraprompt

import (
"context"
"os"
"strings"

Expand Down Expand Up @@ -60,6 +61,11 @@ type CobraPrompt struct {
// Run will automatically generate suggestions for all cobra commands and flags defined by RootCmd
// and execute the selected commands. Run will also reset all given flags by default, see PersistFlagValues
func (co CobraPrompt) Run() {
co.RunContext(nil)
}

// RunContext same as Run but with context
func (co CobraPrompt) RunContext(ctx context.Context) {
if co.RootCmd == nil {
panic("RootCmd is not set. Please set RootCmd")
}
Expand All @@ -70,7 +76,7 @@ func (co CobraPrompt) Run() {
func(in string) {
promptArgs := co.parseArgs(in)
os.Args = append([]string{os.Args[0]}, promptArgs...)
if err := co.RootCmd.Execute(); err != nil {
if err := co.RootCmd.ExecuteContext(ctx); err != nil {
if co.OnErrorFunc != nil {
co.OnErrorFunc(err)
} else {
Expand Down

0 comments on commit e6f51a2

Please sign in to comment.