From 441b7bfa4856b9a71871465ac2d63e3bccabe9b0 Mon Sep 17 00:00:00 2001 From: Andrei Kvapil Date: Tue, 20 Aug 2024 11:43:58 +0200 Subject: [PATCH] Fix completion command to not require Chart.yaml fixes #20 Signed-off-by: Andrei Kvapil --- main.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 4a6f075..7697254 100644 --- a/main.go +++ b/main.go @@ -81,10 +81,14 @@ func init() { } func initConfig() { - cmd, _, _ := rootCmd.Find(os.Args[1:]) - if cmd == nil { + cmdName := os.Args[1] + cmd, _, err := rootCmd.Find([]string{cmdName}) + if err != nil || cmd == nil { return } + if cmd.HasParent() && cmd.Parent() != rootCmd { + cmd = cmd.Parent() + } if strings.HasPrefix(cmd.Use, "init") { if strings.HasPrefix(Version, "v") { commands.Config.InitOptions.Version = strings.TrimPrefix(Version, `v`) @@ -92,10 +96,12 @@ func initConfig() { commands.Config.InitOptions.Version = "0.1.0" } } else { - configFile := filepath.Join(commands.Config.RootDir, "Chart.yaml") - if err := loadConfig(configFile); err != nil { - fmt.Fprintf(os.Stderr, "Error loading configuration: %v\n", err) - os.Exit(1) + if !strings.HasPrefix(cmd.Use, "completion") { + configFile := filepath.Join(commands.Config.RootDir, "Chart.yaml") + if err := loadConfig(configFile); err != nil { + fmt.Fprintf(os.Stderr, "Error loading configuration: %v\n", err) + os.Exit(1) + } } } }