Skip to content

Commit

Permalink
Add execute context support
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen committed Sep 18, 2021
1 parent c1be94e commit 3bd62bc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
Expand Down
10 changes: 8 additions & 2 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func CreateDefaultCmd(target, alias string) *cobra.Command {
return &cobra.Command{
Use: alias,
Use: alias,
RunE: DefaultRunE(target),
}
}
Expand All @@ -29,7 +29,7 @@ func DefaultRunE(targetCLI string) func(cmd *cobra.Command, args []string) (err
targetCLI = targetCLIArray[0]
targetArgs = targetCLIArray[1:]
targetArgs = append(targetArgs, args...)
} else{
} else {
targetArgs = args
}

Expand Down Expand Up @@ -121,11 +121,17 @@ func AddAliasCmd(cmd *cobra.Command, defaultAlias []pkg.Alias) {
}

func Execute(cmd *cobra.Command, target string, aliasList []pkg.Alias, preHook func([]string)) {
ExecuteContext(cmd, context.TODO(), target, aliasList, preHook)
}

func ExecuteContext(cmd *cobra.Command, ctx context.Context, target string, aliasList []pkg.Alias, preHook func([]string)) {
cmd.SilenceErrors = true
var err error
// this is very trick way, looking to improve it
if len(strings.Split(target, " ")) > 1 {
err = errors.New("unknown command")
} else if ctx != nil {
err = cmd.ExecuteContext(ctx)
} else {
err = cmd.Execute()
}
Expand Down

0 comments on commit 3bd62bc

Please sign in to comment.