From 2309b0dfe20e27b6afe283a6be21ad7a0652ac99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Stepp=C3=A9?= Date: Mon, 3 Jul 2023 12:47:38 +0200 Subject: [PATCH] feat: details flag to list command (#537) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexandre Steppé Co-authored-by: Alex Jones --- cmd/auth/list.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/cmd/auth/list.go b/cmd/auth/list.go index eace542861..60524810cf 100644 --- a/cmd/auth/list.go +++ b/cmd/auth/list.go @@ -16,6 +16,8 @@ package auth import ( "fmt" "os" + "strings" + "unicode/utf8" "github.com/fatih/color" "github.com/k8sgpt-ai/k8sgpt/pkg/ai" @@ -23,6 +25,9 @@ import ( "github.com/spf13/viper" ) +var details bool +var userInput string + var listCmd = &cobra.Command{ Use: "list", Short: "List configured providers", @@ -36,6 +41,11 @@ var listCmd = &cobra.Command{ os.Exit(1) } + if details { + fmt.Println("Show password ? (y/n)") + fmt.Scan(&userInput) + } + // Print the default if it is set fmt.Print(color.YellowString("Default: \n")) if configAI.DefaultProvider != "" { @@ -55,6 +65,13 @@ var listCmd = &cobra.Command{ } if providerExists { fmt.Printf("> %s\n", color.GreenString(aiBackend)) + if details { + for _, provider := range configAI.Providers { + if provider.Name == aiBackend { + printDetails(provider, userInput) + } + } + } } } fmt.Print(color.YellowString("Unused: \n")) @@ -71,3 +88,33 @@ var listCmd = &cobra.Command{ } }, } + +func init() { + listCmd.Flags().BoolVar(&details, "details", false, "Print active provider configuration details") +} + +func printDetails(provider ai.AIProvider, userInput string) { + if provider.Model != "" { + fmt.Printf(" - Model: %s\n", provider.Model) + } + switch userInput { + case "y": + if provider.Password != "" { + fmt.Printf(" - Password: %s\n", provider.Password) + } + case "n": + if provider.Password != "" { + nc := utf8.RuneCountInString(provider.Password) + newStr := strings.Repeat("*", nc) + fmt.Printf(" - Password: %s\n", newStr) + } + default: + break + } + if provider.Engine != "" { + fmt.Printf(" - Engine: %s\n", provider.Engine) + } + if provider.BaseURL != "" { + fmt.Printf(" - BaseURL: %s\n", provider.BaseURL) + } +}