Skip to content

Commit

Permalink
feat: rework auth commands (#438)
Browse files Browse the repository at this point in the history
* feat: rename "auth new" to "auth add"

This change allows to be more consistent with the rest of the code

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

* feat: rework "auth remove" to be more consistent with other remove commands like "filters remove"

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

* feat: update documentation

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

---------

Signed-off-by: Matthis Holleville <matthish29@gmail.com>
  • Loading branch information
matthisholleville committed May 18, 2023
1 parent f0d3f36 commit c659a87
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 32 deletions.
12 changes: 6 additions & 6 deletions README.md
Expand Up @@ -121,7 +121,7 @@ _This mode of operation is ideal for continuous monitoring of your cluster and c

* Currently the default AI provider is OpenAI, you will need to generate an API key from [OpenAI](https://openai.com)
* You can do this by running `k8sgpt generate` to open a browser link to generate it
* Run `k8sgpt auth new` to set it in k8sgpt.
* Run `k8sgpt auth add` to set it in k8sgpt.
* You can provide the password directly using the `--password` flag.
* Run `k8sgpt filters` to manage the active filters used by the analyzer. By default, all filters are executed during analysis.
* Run `k8sgpt analyze` to run a scan.
Expand Down Expand Up @@ -161,7 +161,7 @@ _Run a scan with the default analyzers_

```
k8sgpt generate
k8sgpt auth new
k8sgpt auth add
k8sgpt analyze --explain
```

Expand Down Expand Up @@ -235,7 +235,7 @@ k8sgpt auth list
_Remove configured backends_

```
k8sgpt auth remove --backend $MY_BACKEND
k8sgpt auth remove $MY_BACKEND1,$MY_BACKEND2..
```

_List integrations_
Expand Down Expand Up @@ -318,7 +318,7 @@ To authenticate with k8sgpt, you will need the Azure OpenAI endpoint of your ten
### Run k8sgpt
To run k8sgpt, run `k8sgpt auth` with the `azureopenai` backend:
```
k8sgpt auth new --backend azureopenai --baseurl https://<your Azure OpenAI endpoint> --engine <deployment_name> --model <model_name>
k8sgpt auth add --backend azureopenai --baseurl https://<your Azure OpenAI endpoint> --engine <deployment_name> --model <model_name>
```
Lastly, enter your Azure API key, after the prompt.

Expand All @@ -343,10 +343,10 @@ To start the API server, follow the instruction in [LocalAI](https://github.com/

### Run k8sgpt

To run k8sgpt, run `k8sgpt auth new` with the `localai` backend:
To run k8sgpt, run `k8sgpt auth add` with the `localai` backend:

```
k8sgpt auth new --backend localai --model <model_name> --baseurl http://localhost:8080/v1
k8sgpt auth add --backend localai --model <model_name> --baseurl http://localhost:8080/v1
```

Now you can analyze with the `localai` backend:
Expand Down
16 changes: 8 additions & 8 deletions cmd/auth/new.go → cmd/auth/add.go
Expand Up @@ -26,8 +26,8 @@ import (
"golang.org/x/term"
)

var newCmd = &cobra.Command{
Use: "new",
var addCmd = &cobra.Command{
Use: "add",
Short: "Configure new provider",
Long: "The new command allows to configure a new backend AI provider",
PreRun: func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -107,20 +107,20 @@ var newCmd = &cobra.Command{
color.Green("%s added to the AI backend provider list", backend)
} else {
// provider with same name exists, update provider info
color.Yellow("Provider with same name already exists, use update command to modify an existing provider configuration")
color.Yellow("Provider with same name already exists.")
}
},
}

func init() {
// add flag for backend
newCmd.Flags().StringVarP(&backend, "backend", "b", "openai", "Backend AI provider")
addCmd.Flags().StringVarP(&backend, "backend", "b", "openai", "Backend AI provider")
// add flag for model
newCmd.Flags().StringVarP(&model, "model", "m", "gpt-3.5-turbo", "Backend AI model")
addCmd.Flags().StringVarP(&model, "model", "m", "gpt-3.5-turbo", "Backend AI model")
// add flag for password
newCmd.Flags().StringVarP(&password, "password", "p", "", "Backend AI password")
addCmd.Flags().StringVarP(&password, "password", "p", "", "Backend AI password")
// add flag for url
newCmd.Flags().StringVarP(&baseURL, "baseurl", "u", "", "URL AI provider, (e.g `http://localhost:8080/v1`)")
addCmd.Flags().StringVarP(&baseURL, "baseurl", "u", "", "URL AI provider, (e.g `http://localhost:8080/v1`)")
// add flag for azure open ai engine/deployment name
newCmd.Flags().StringVarP(&engine, "engine", "e", "", "Azure AI deployment name")
addCmd.Flags().StringVarP(&engine, "engine", "e", "", "Azure AI deployment name")
}
2 changes: 1 addition & 1 deletion cmd/auth/auth.go
Expand Up @@ -45,7 +45,7 @@ func init() {
// add subcommand to list backends
AuthCmd.AddCommand(listCmd)
// add subcommand to create new backend provider
AuthCmd.AddCommand(newCmd)
AuthCmd.AddCommand(addCmd)
// add subcommand to remove new backend provider
AuthCmd.AddCommand(removeCmd)
// add subcommand to set default backend provider
Expand Down
38 changes: 21 additions & 17 deletions cmd/auth/remove.go
Expand Up @@ -15,39 +15,46 @@ package auth

import (
"os"
"strings"

"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var removeCmd = &cobra.Command{
Use: "remove",
Use: "remove [backend(s)]",
Short: "Remove a provider",
Long: "The command to remove an AI backend provider",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
inputBackends := strings.Split(args[0], ",")
err := viper.UnmarshalKey("ai", &configAI)
if err != nil {
color.Red("Error: %v", err)
os.Exit(1)
}

if backend == "" {
color.Red("Error: --backend option must be set.")
if len(inputBackends) == 0 {
color.Red("Error: backend must be set.")
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
for _, b := range inputBackends {
foundBackend := false
for i, provider := range configAI.Providers {
if b == provider.Name {
foundBackend = true
configAI.Providers = append(configAI.Providers[:i], configAI.Providers[i+1:]...)
color.Green("%s deleted to the AI backend provider list", b)
break
}
}
}
if !foundBackend {
color.Red("Error: %s does not exist in configuration file. Please use k8sgpt auth new.", backend)
os.Exit(1)
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 {
Expand All @@ -58,7 +65,4 @@ var removeCmd = &cobra.Command{
},
}

func init() {
// add flag for backend
removeCmd.Flags().StringVarP(&backend, "backend", "b", "", "Backend AI provider")
}
func init() {}

0 comments on commit c659a87

Please sign in to comment.