From c659a875fc296849a3703bfd7f0c4796f44bdf5a Mon Sep 17 00:00:00 2001
From: Matthis <99146727+matthisholleville@users.noreply.github.com>
Date: Thu, 18 May 2023 16:50:29 +0200
Subject: [PATCH] feat: rework auth commands (#438)

* feat: rename "auth new" to "auth add"

This change allows to be more consistent with the rest of the code

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: rework "auth remove" to be more consistent with other remove commands like "filters remove"

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: update documentation

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

---------

Signed-off-by: Matthis Holleville <matthish29@gmail.com>
---
 README.md                   | 12 ++++++------
 cmd/auth/{new.go => add.go} | 16 ++++++++--------
 cmd/auth/auth.go            |  2 +-
 cmd/auth/remove.go          | 38 ++++++++++++++++++++-----------------
 4 files changed, 36 insertions(+), 32 deletions(-)
 rename cmd/auth/{new.go => add.go} (87%)

diff --git a/README.md b/README.md
index 17407d32a4..5b8ae918bb 100644
--- a/README.md
+++ b/README.md
@@ -121,7 +121,7 @@ _This mode of operation is ideal for continuous monitoring of your cluster and c
 
 * Currently the default AI provider is OpenAI, you will need to generate an API key from [OpenAI](https://openai.com)
   * You can do this by running `k8sgpt generate` to open a browser link to generate it
-* Run `k8sgpt auth new` to set it in k8sgpt.
+* Run `k8sgpt auth add` to set it in k8sgpt.
   * You can provide the password directly using the `--password` flag.
 * Run `k8sgpt filters` to manage the active filters used by the analyzer. By default, all filters are executed during analysis.
 * Run `k8sgpt analyze` to run a scan.
@@ -161,7 +161,7 @@ _Run a scan with the default analyzers_
 
 ```
 k8sgpt generate
-k8sgpt auth new
+k8sgpt auth add
 k8sgpt analyze --explain
 ```
 
@@ -235,7 +235,7 @@ k8sgpt auth list
 _Remove configured backends_
 
 ```
-k8sgpt auth remove --backend $MY_BACKEND
+k8sgpt auth remove $MY_BACKEND1,$MY_BACKEND2..
 ```
 
 _List integrations_
@@ -318,7 +318,7 @@ To authenticate with k8sgpt, you will need the Azure OpenAI endpoint of your ten
 ### Run k8sgpt  
 To run k8sgpt, run `k8sgpt auth` with the `azureopenai` backend:  
 ```
-k8sgpt auth new --backend azureopenai --baseurl https://<your Azure OpenAI endpoint> --engine <deployment_name> --model <model_name>
+k8sgpt auth add --backend azureopenai --baseurl https://<your Azure OpenAI endpoint> --engine <deployment_name> --model <model_name>
 ```
 Lastly, enter your Azure API key, after the prompt.
 
@@ -343,10 +343,10 @@ To start the API server, follow the instruction in [LocalAI](https://github.com/
 
 ### Run k8sgpt
 
-To run k8sgpt, run `k8sgpt auth new` with the `localai` backend:
+To run k8sgpt, run `k8sgpt auth add` with the `localai` backend:
 
 ```
-k8sgpt auth new --backend localai --model <model_name> --baseurl http://localhost:8080/v1
+k8sgpt auth add --backend localai --model <model_name> --baseurl http://localhost:8080/v1
 ```
 
 Now you can analyze with the `localai` backend:
diff --git a/cmd/auth/new.go b/cmd/auth/add.go
similarity index 87%
rename from cmd/auth/new.go
rename to cmd/auth/add.go
index 0cf31283db..1466feeaeb 100644
--- a/cmd/auth/new.go
+++ b/cmd/auth/add.go
@@ -26,8 +26,8 @@ import (
 	"golang.org/x/term"
 )
 
-var newCmd = &cobra.Command{
-	Use:   "new",
+var addCmd = &cobra.Command{
+	Use:   "add",
 	Short: "Configure new provider",
 	Long:  "The new command allows to configure a new backend AI provider",
 	PreRun: func(cmd *cobra.Command, args []string) {
@@ -107,20 +107,20 @@ var newCmd = &cobra.Command{
 			color.Green("%s added to the AI backend provider list", backend)
 		} else {
 			// provider with same name exists, update provider info
-			color.Yellow("Provider with same name already exists, use update command to modify an existing provider configuration")
+			color.Yellow("Provider with same name already exists.")
 		}
 	},
 }
 
 func init() {
 	// add flag for backend
-	newCmd.Flags().StringVarP(&backend, "backend", "b", "openai", "Backend AI provider")
+	addCmd.Flags().StringVarP(&backend, "backend", "b", "openai", "Backend AI provider")
 	// add flag for model
-	newCmd.Flags().StringVarP(&model, "model", "m", "gpt-3.5-turbo", "Backend AI model")
+	addCmd.Flags().StringVarP(&model, "model", "m", "gpt-3.5-turbo", "Backend AI model")
 	// add flag for password
-	newCmd.Flags().StringVarP(&password, "password", "p", "", "Backend AI password")
+	addCmd.Flags().StringVarP(&password, "password", "p", "", "Backend AI password")
 	// add flag for url
-	newCmd.Flags().StringVarP(&baseURL, "baseurl", "u", "", "URL AI provider, (e.g `http://localhost:8080/v1`)")
+	addCmd.Flags().StringVarP(&baseURL, "baseurl", "u", "", "URL AI provider, (e.g `http://localhost:8080/v1`)")
 	// add flag for azure open ai engine/deployment name
-	newCmd.Flags().StringVarP(&engine, "engine", "e", "", "Azure AI deployment name")
+	addCmd.Flags().StringVarP(&engine, "engine", "e", "", "Azure AI deployment name")
 }
diff --git a/cmd/auth/auth.go b/cmd/auth/auth.go
index 45727de5d0..703717c9d7 100644
--- a/cmd/auth/auth.go
+++ b/cmd/auth/auth.go
@@ -45,7 +45,7 @@ func init() {
 	// add subcommand to list backends
 	AuthCmd.AddCommand(listCmd)
 	// add subcommand to create new backend provider
-	AuthCmd.AddCommand(newCmd)
+	AuthCmd.AddCommand(addCmd)
 	// add subcommand to remove new backend provider
 	AuthCmd.AddCommand(removeCmd)
 	// add subcommand to set default backend provider
diff --git a/cmd/auth/remove.go b/cmd/auth/remove.go
index 3e4b133aab..358f82e103 100644
--- a/cmd/auth/remove.go
+++ b/cmd/auth/remove.go
@@ -15,6 +15,7 @@ package auth
 
 import (
 	"os"
+	"strings"
 
 	"github.com/fatih/color"
 	"github.com/spf13/cobra"
@@ -22,32 +23,38 @@ import (
 )
 
 var removeCmd = &cobra.Command{
-	Use:   "remove",
+	Use:   "remove [backend(s)]",
 	Short: "Remove a provider",
 	Long:  "The command to remove an AI backend provider",
+	Args:  cobra.ExactArgs(1),
 	Run: func(cmd *cobra.Command, args []string) {
+		inputBackends := strings.Split(args[0], ",")
 		err := viper.UnmarshalKey("ai", &configAI)
 		if err != nil {
 			color.Red("Error: %v", err)
 			os.Exit(1)
 		}
 
-		if backend == "" {
-			color.Red("Error: --backend option must be set.")
+		if len(inputBackends) == 0 {
+			color.Red("Error: backend must be set.")
 			os.Exit(1)
 		}
 
-		foundBackend := false
-		for i, provider := range configAI.Providers {
-			if backend == provider.Name {
-				foundBackend = true
-				configAI.Providers = append(configAI.Providers[:i], configAI.Providers[i+1:]...)
-				break
+		for _, b := range inputBackends {
+			foundBackend := false
+			for i, provider := range configAI.Providers {
+				if b == provider.Name {
+					foundBackend = true
+					configAI.Providers = append(configAI.Providers[:i], configAI.Providers[i+1:]...)
+					color.Green("%s deleted to the AI backend provider list", b)
+					break
+				}
 			}
-		}
-		if !foundBackend {
-			color.Red("Error: %s does not exist in configuration file. Please use k8sgpt auth new.", backend)
-			os.Exit(1)
+			if !foundBackend {
+				color.Red("Error: %s does not exist in configuration file. Please use k8sgpt auth new.", backend)
+				os.Exit(1)
+			}
+
 		}
 		viper.Set("ai", configAI)
 		if err := viper.WriteConfig(); err != nil {
@@ -58,7 +65,4 @@ var removeCmd = &cobra.Command{
 	},
 }
 
-func init() {
-	// add flag for backend
-	removeCmd.Flags().StringVarP(&backend, "backend", "b", "", "Backend AI provider")
-}
+func init() {}