Skip to content

Commit

Permalink
feat: validate backend name.
Browse files Browse the repository at this point in the history
Signed-off-by: Aris Boutselis <aris.boutselis@senseon.io>
  • Loading branch information
Aris Boutselis committed Apr 25, 2023
1 parent 04bbb9c commit 876bbdb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
8 changes: 5 additions & 3 deletions cmd/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/ai"
"github.com/k8sgpt-ai/k8sgpt/pkg/util"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/term"
Expand Down Expand Up @@ -58,9 +59,10 @@ var AuthCmd = &cobra.Command{
}
}

// check if backend is not empty
if backend == "" {
color.Red("Error: Backend AI cannot be empty.")
// check if backend is not empty and a valid value
backends := util.Backends()
if backend == "" || !util.ValidBackend(backends, backend) {
color.Red("Error: Backend AI cannot be empty and accepted values are '%v'", strings.Join(backends, ", "))
os.Exit(1)
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,17 @@ func EnsureDirExists(dir string) error {

return err
}

func Backends() []string {
return []string{"openai", "localai", "azureopenai", "noopai"}
}

func ValidBackend(validBackends []string, backend string) bool {
for _, b := range validBackends {
if b == backend {
return true
}
}

return false
}

0 comments on commit 876bbdb

Please sign in to comment.