From 0033fa6e1944840f2950db9a09a8511edd212fb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Stepp=C3=A9?= Date: Sun, 21 May 2023 23:42:32 +0200 Subject: [PATCH 1/4] added update.go + fixed typos MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexandre Steppé --- cmd/auth/auth.go | 2 + cmd/auth/list.go | 2 +- cmd/auth/remove.go | 7 +--- cmd/auth/update.go | 93 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 cmd/auth/update.go diff --git a/cmd/auth/auth.go b/cmd/auth/auth.go index 703717c9d7..75b4b4bebb 100644 --- a/cmd/auth/auth.go +++ b/cmd/auth/auth.go @@ -50,4 +50,6 @@ func init() { AuthCmd.AddCommand(removeCmd) // add subcommand to set default backend provider AuthCmd.AddCommand(defaultCmd) + // add subcommand to update backend provider + AuthCmd.AddCommand(updateCmd) } diff --git a/cmd/auth/list.go b/cmd/auth/list.go index 09a75e381b..eace542861 100644 --- a/cmd/auth/list.go +++ b/cmd/auth/list.go @@ -44,7 +44,7 @@ var listCmd = &cobra.Command{ fmt.Printf("> %s\n", color.BlueString("openai")) } - // Get list of all AI Backends and only print htem if they are not in the provider list + // Get list of all AI Backends and only print them if they are not in the provider list fmt.Print(color.YellowString("Active: \n")) for _, aiBackend := range ai.Backends { providerExists := false diff --git a/cmd/auth/remove.go b/cmd/auth/remove.go index d9a36f1779..c2f82a18eb 100644 --- a/cmd/auth/remove.go +++ b/cmd/auth/remove.go @@ -46,7 +46,7 @@ var removeCmd = &cobra.Command{ 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) + color.Green("%s deleted from the AI backend provider list", b) break } } @@ -64,8 +64,3 @@ var removeCmd = &cobra.Command{ }, } - -func init() { - -} - diff --git a/cmd/auth/update.go b/cmd/auth/update.go new file mode 100644 index 0000000000..1d7367f96b --- /dev/null +++ b/cmd/auth/update.go @@ -0,0 +1,93 @@ +/* +Copyright 2023 The K8sGPT Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package auth + +import ( + "os" + "strings" + + "github.com/fatih/color" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +var updateCmd = &cobra.Command{ + Use: "update", + Short: "Update a backend provider", + Long: "The command to update an AI backend provider", + Args: cobra.ExactArgs(1), + PreRun: func(cmd *cobra.Command, args []string) { + backend, _ := cmd.Flags().GetString("backend") + if strings.ToLower(backend) == "azureopenai" { + _ = cmd.MarkFlagRequired("engine") + _ = cmd.MarkFlagRequired("baseurl") + } + }, + Run: func(cmd *cobra.Command, args []string) { + + // get ai configuration + err := viper.UnmarshalKey("ai", &configAI) + if err != nil { + color.Red("Error: %v", err) + os.Exit(1) + } + + inputBackends := strings.Split(args[0], ",") + + if len(inputBackends) == 0 { + color.Red("Error: backend must be set.") + os.Exit(1) + } + + for _, b := range inputBackends { + foundBackend := false + for i, provider := range configAI.Providers { + if b == provider.Name { + foundBackend = true + configAI.Providers[i].Name = backend + configAI.Providers[i].Model = model + configAI.Providers[i].Password = password + configAI.Providers[i].BaseURL = baseURL + configAI.Providers[i].Engine = engine + color.Green("%s updated in the AI backend provider list", b) + break + } + } + if !foundBackend { + color.Red("Error: %s does not exist in configuration file. Please use k8sgpt auth new.", args[0]) + os.Exit(1) + } + + } + + viper.Set("ai", configAI) + if err := viper.WriteConfig(); err != nil { + color.Red("Error writing config file: %s", err.Error()) + os.Exit(1) + } + }, +} + +func init() { + // update flag for backend + updateCmd.Flags().StringVarP(&backend, "backend", "b", "openai", "Backend AI provider") + // update flag for model + updateCmd.Flags().StringVarP(&model, "model", "m", "gpt-3.5-turbo", "Backend AI model") + // update flag for password + updateCmd.Flags().StringVarP(&password, "password", "p", "", "Backend AI password") + // update flag for url + updateCmd.Flags().StringVarP(&baseURL, "baseurl", "u", "", "URL AI provider, (e.g `http://localhost:8080/v1`)") + // update flag for azure open ai engine/deployment name + updateCmd.Flags().StringVarP(&engine, "engine", "e", "", "Azure AI deployment name") +} From 523ff183778a0d8f1e7af61fb4e5a810020f80b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Stepp=C3=A9?= Date: Sun, 21 May 2023 23:52:58 +0200 Subject: [PATCH 2/4] updated readme MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexandre Steppé --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index a26dbd1f62..fee7226450 100644 --- a/README.md +++ b/README.md @@ -224,12 +224,19 @@ k8sgpt filters remove [filter(s)]
Additional commands + _List configured backends_ ``` k8sgpt auth list ``` +_Update configured backends_ + +``` +k8sgpt auth update $MY_BACKEND1,$MY_BACKEND2.. +``` + _Remove configured backends_ ``` From 925b9e874bab9214ca350a66b3843766a4683a4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Stepp=C3=A9?= Date: Fri, 26 May 2023 21:17:32 +0200 Subject: [PATCH 3/4] fixed default values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexandre Steppé --- cmd/auth/update.go | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/cmd/auth/update.go b/cmd/auth/update.go index 1d7367f96b..effbe0767b 100644 --- a/cmd/auth/update.go +++ b/cmd/auth/update.go @@ -55,13 +55,26 @@ var updateCmd = &cobra.Command{ for i, provider := range configAI.Providers { if b == provider.Name { foundBackend = true - configAI.Providers[i].Name = backend - configAI.Providers[i].Model = model - configAI.Providers[i].Password = password - configAI.Providers[i].BaseURL = baseURL - configAI.Providers[i].Engine = engine + if backend != "" { + configAI.Providers[i].Name = backend + color.Blue("Backend name updated successfully") + } + if model != "" { + configAI.Providers[i].Model = model + color.Blue("Model updated successfully") + } + if password != "" { + configAI.Providers[i].Password = password + color.Blue("Password updated successfully") + } + if baseURL != "" { + configAI.Providers[i].BaseURL = baseURL + color.Blue("Base URL updated successfully") + } + if engine != "" { + configAI.Providers[i].Engine = engine + } color.Green("%s updated in the AI backend provider list", b) - break } } if !foundBackend { @@ -81,9 +94,9 @@ var updateCmd = &cobra.Command{ func init() { // update flag for backend - updateCmd.Flags().StringVarP(&backend, "backend", "b", "openai", "Backend AI provider") + updateCmd.Flags().StringVarP(&backend, "backend", "b", "", "Backend AI provider") // update flag for model - updateCmd.Flags().StringVarP(&model, "model", "m", "gpt-3.5-turbo", "Backend AI model") + updateCmd.Flags().StringVarP(&model, "model", "m", "", "Backend AI model") // update flag for password updateCmd.Flags().StringVarP(&password, "password", "p", "", "Backend AI password") // update flag for url From 45cef81032f5269607da89f9acca31d52ecaac1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Stepp=C3=A9?= Date: Fri, 26 May 2023 21:27:30 +0200 Subject: [PATCH 4/4] fixed typo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexandre Steppé --- cmd/auth/update.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/auth/update.go b/cmd/auth/update.go index effbe0767b..be0561ba63 100644 --- a/cmd/auth/update.go +++ b/cmd/auth/update.go @@ -94,13 +94,13 @@ var updateCmd = &cobra.Command{ func init() { // update flag for backend - updateCmd.Flags().StringVarP(&backend, "backend", "b", "", "Backend AI provider") + updateCmd.Flags().StringVarP(&backend, "backend", "b", "", "Update backend AI provider") // update flag for model - updateCmd.Flags().StringVarP(&model, "model", "m", "", "Backend AI model") + updateCmd.Flags().StringVarP(&model, "model", "m", "", "Update backend AI model") // update flag for password - updateCmd.Flags().StringVarP(&password, "password", "p", "", "Backend AI password") + updateCmd.Flags().StringVarP(&password, "password", "p", "", "Update backend AI password") // update flag for url - updateCmd.Flags().StringVarP(&baseURL, "baseurl", "u", "", "URL AI provider, (e.g `http://localhost:8080/v1`)") + updateCmd.Flags().StringVarP(&baseURL, "baseurl", "u", "", "Update URL AI provider, (e.g `http://localhost:8080/v1`)") // update flag for azure open ai engine/deployment name - updateCmd.Flags().StringVarP(&engine, "engine", "e", "", "Azure AI deployment name") + updateCmd.Flags().StringVarP(&engine, "engine", "e", "", "Update Azure AI deployment name") }