Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add remove command to remove a backend AI provider #395

Merged
merged 3 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}