Skip to content

Commit

Permalink
feat: details flag to list command (#537)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Steppé <alexandre.steppe@gmail.com>
Co-authored-by: Alex Jones <alexsimonjones@gmail.com>
  • Loading branch information
HatiCode and AlexsJones committed Jul 3, 2023
1 parent 1f5462c commit 2309b0d
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions cmd/auth/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ package auth
import (
"fmt"
"os"
"strings"
"unicode/utf8"

"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/ai"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var details bool
var userInput string

var listCmd = &cobra.Command{
Use: "list",
Short: "List configured providers",
Expand All @@ -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 != "" {
Expand All @@ -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"))
Expand All @@ -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)
}
}

0 comments on commit 2309b0d

Please sign in to comment.