Skip to content

Commit

Permalink
feat: add remove command to remove a backend AI provider (#395)
Browse files Browse the repository at this point in the history
* feat: add remove command to remove a backend AI provider

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

* Update cmd/auth/remove.go

Co-authored-by: Alex Jones <alex@k8sgpt.ai>
Signed-off-by: Matthis <99146727+matthisholleville@users.noreply.github.com>

* feat: update Long remove command

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

---------

Signed-off-by: Matthis Holleville <matthish29@gmail.com>
Signed-off-by: Matthis <99146727+matthisholleville@users.noreply.github.com>
Co-authored-by: Alex Jones <alex@k8sgpt.ai>
  • Loading branch information
matthisholleville and AlexsJones committed May 11, 2023
1 parent 8cfb717 commit c5b72ee
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ _List configured backends_
k8sgpt auth list
```

_Remove configured backends_

```
k8sgpt auth remove --backend $MY_BACKEND
```

_List integrations_

```
Expand Down
2 changes: 2 additions & 0 deletions cmd/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
59 changes: 59 additions & 0 deletions cmd/auth/remove.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit c5b72ee

Please sign in to comment.