From 9dadd186c8d03a4284faff3f0842d6e2d00ebbb8 Mon Sep 17 00:00:00 2001 From: Pavel Sturc Date: Fri, 10 Nov 2023 21:15:24 +0100 Subject: [PATCH] feat: auth remove: add -b flag (#711) * feat: auth remove: add -b flag Signed-off-by: Pavel Sturc * fix: address comment Signed-off-by: Pavel Sturc --------- Signed-off-by: Pavel Sturc Co-authored-by: Alex Jones --- README.md | 2 +- cmd/auth/remove.go | 32 ++++++++++++++++++++------------ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 3fb11081f7..8bb7cd5c02 100644 --- a/README.md +++ b/README.md @@ -246,7 +246,7 @@ k8sgpt auth update $MY_BACKEND1,$MY_BACKEND2.. _Remove configured backends_ ``` -k8sgpt auth remove $MY_BACKEND1,$MY_BACKEND2.. +k8sgpt auth remove -b $MY_BACKEND1,$MY_BACKEND2.. ``` _List integrations_ diff --git a/cmd/auth/remove.go b/cmd/auth/remove.go index c52a02de60..c18066545c 100644 --- a/cmd/auth/remove.go +++ b/cmd/auth/remove.go @@ -23,23 +23,26 @@ import ( ) var removeCmd = &cobra.Command{ - Use: "remove [backend(s)]", - Short: "Remove a provider", - Long: "The command to remove an AI backend provider", - Args: cobra.ExactArgs(1), + Use: "remove", + Short: "Remove provider(s)", + Long: "The command to remove AI backend provider(s)", + PreRun: func(cmd *cobra.Command, args []string) { + _ = cmd.MarkFlagRequired("backends") + }, Run: func(cmd *cobra.Command, args []string) { - inputBackends := strings.Split(args[0], ",") + if backend == "" { + color.Red("Error: backends must be set.") + _ = cmd.Help() + return + } + inputBackends := strings.Split(backend, ",") + err := viper.UnmarshalKey("ai", &configAI) if err != nil { color.Red("Error: %v", err) os.Exit(1) } - 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 { @@ -54,11 +57,11 @@ var removeCmd = &cobra.Command{ } } if !foundBackend { - color.Red("Error: %s does not exist in configuration file. Please use k8sgpt auth new.", backend) + color.Red("Error: %s does not exist in configuration file. Please use k8sgpt auth new.", b) os.Exit(1) } - } + viper.Set("ai", configAI) if err := viper.WriteConfig(); err != nil { color.Red("Error writing config file: %s", err.Error()) @@ -67,3 +70,8 @@ var removeCmd = &cobra.Command{ }, } + +func init() { + // add flag for backends + removeCmd.Flags().StringVarP(&backend, "backends", "b", "", "Backend AI providers to remove (separated by a comma)") +}