From c5b72eee165a29d4ed59b0ec2f19e9f58267840d Mon Sep 17 00:00:00 2001 From: Matthis <99146727+matthisholleville@users.noreply.github.com> Date: Thu, 11 May 2023 09:05:25 +0200 Subject: [PATCH] feat: add remove command to remove a backend AI provider (#395) * feat: add remove command to remove a backend AI provider Signed-off-by: Matthis Holleville * Update cmd/auth/remove.go Co-authored-by: Alex Jones Signed-off-by: Matthis <99146727+matthisholleville@users.noreply.github.com> * feat: update Long remove command Signed-off-by: Matthis Holleville --------- Signed-off-by: Matthis Holleville Signed-off-by: Matthis <99146727+matthisholleville@users.noreply.github.com> Co-authored-by: Alex Jones --- README.md | 6 +++++ cmd/auth/auth.go | 2 ++ cmd/auth/remove.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 cmd/auth/remove.go diff --git a/README.md b/README.md index 2ccc4d9ff3..866ab30f98 100644 --- a/README.md +++ b/README.md @@ -232,6 +232,12 @@ _List configured backends_ k8sgpt auth list ``` +_Remove configured backends_ + +``` +k8sgpt auth remove --backend $MY_BACKEND +``` + _List integrations_ ``` diff --git a/cmd/auth/auth.go b/cmd/auth/auth.go index 6c18645d80..9ae42fcbc1 100644 --- a/cmd/auth/auth.go +++ b/cmd/auth/auth.go @@ -46,4 +46,6 @@ func init() { AuthCmd.AddCommand(listCmd) // add subcommand to create new backend provider AuthCmd.AddCommand(newCmd) + // add subcommand to remove new backend provider + AuthCmd.AddCommand(removeCmd) } diff --git a/cmd/auth/remove.go b/cmd/auth/remove.go new file mode 100644 index 0000000000..33a474d90e --- /dev/null +++ b/cmd/auth/remove.go @@ -0,0 +1,59 @@ +/* +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" + + "github.com/fatih/color" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +var removeCmd = &cobra.Command{ + Use: "remove", + Short: "Remove a provider", + Long: "The command to remove an AI backend provider", + Run: func(cmd *cobra.Command, args []string) { + err := viper.UnmarshalKey("ai", &configAI) + if err != nil { + color.Red("Error: %v", err) + 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 + } + } + 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 { + color.Red("Error writing config file: %s", err.Error()) + os.Exit(1) + } + + }, +} + +func init() { + // add flag for backend + removeCmd.Flags().StringVarP(&backend, "backend", "b", "openai", "Backend AI provider") +}