Skip to content

Commit

Permalink
Skip config parsing & actions init for "help" and "completion"
Browse files Browse the repository at this point in the history
  • Loading branch information
nt0xa committed Jul 16, 2024
1 parent 17929e9 commit 140c52e
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"os"
"reflect"
"slices"
"strings"

"github.com/adrg/xdg"
Expand Down Expand Up @@ -38,6 +39,11 @@ func main() {
cmd.AllowFileAccess(true),
cmd.PreExec(func(acts *actions.Actions, root *cobra.Command) {
root.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
// Skip config & actions initialization for "help" and "completion" commands.
if isHelpOrCompletion(cmd.CommandPath()) {
return nil
}

// We have to find the flag value ourselves because flags are not parsed on completion.
if err := initConfig(findFlagValue("config", os.Args), &cfg); err != nil {
return err
Expand Down Expand Up @@ -183,3 +189,8 @@ func findFlagValue(f string, args []string) string {
}
return ""
}

func isHelpOrCompletion(path string) bool {
parts := strings.Split(path, " ")
return slices.Contains(parts, "completion") || slices.Contains(parts, "help")
}

0 comments on commit 140c52e

Please sign in to comment.