Skip to content

Commit

Permalink
fix: autocomplete considers command flags
Browse files Browse the repository at this point in the history
chore: add ForceCallPreRun (for autocomplete)

when using cobra.Command.ValidArgsFunction
call wrappers.ForceCallPreRun() from inside it.
This will force parsing the flags
in order to consider flag overriding flags
(eg: --targets)
  • Loading branch information
tcarreira committed Jul 31, 2023
1 parent a259a8b commit 65a7921
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions internal/wrappers/cobra.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright © 2023 tsuru-client authors
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package wrappers

import "github.com/spf13/cobra"

// ForceCallPreRun climbs the parent tree until it finds and runs one of:
// - PreRun()
// - PreRunE()
// - PersistentPreRun()
// - PersistentPreRunE()
func ForceCallPreRun(cmd *cobra.Command, args []string) {
curr := cmd
for curr != nil {
if curr.PreRun != nil {
curr.PreRun(cmd, args)
return
}
if curr.PreRunE != nil {
_ = curr.PreRunE(cmd, args)
return
}
if curr.PersistentPreRun != nil {
curr.PersistentPreRun(cmd, args)
return
}
if curr.PersistentPreRunE != nil {
_ = curr.PersistentPreRunE(cmd, args)
return
}

if curr == curr.Parent() {
return
}
curr = curr.Parent()
}
}

0 comments on commit 65a7921

Please sign in to comment.