Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add details flag to auth list command #537

Merged
merged 3 commits into from
Jul 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}