Skip to content

Commit

Permalink
feat: auth remove: add -b flag (#711)
Browse files Browse the repository at this point in the history
* feat: auth remove: add -b flag

Signed-off-by: Pavel Sturc <psturc@redhat.com>

* fix: address comment

Signed-off-by: Pavel Sturc <psturc@redhat.com>

---------

Signed-off-by: Pavel Sturc <psturc@redhat.com>
Co-authored-by: Alex Jones <alexsimonjones@gmail.com>
  • Loading branch information
psturc and AlexsJones committed Nov 10, 2023
1 parent 3bff9cb commit 9dadd18
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down
32 changes: 20 additions & 12 deletions cmd/auth/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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())
Expand All @@ -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)")
}

0 comments on commit 9dadd18

Please sign in to comment.