diff --git a/cmd/root.go b/cmd/root.go index b76f1f4046..8aa5780df5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,10 +1,11 @@ package cmd import ( - "github.com/k8sgpt-ai/k8sgpt/cmd/serve" "os" "path/filepath" + "github.com/k8sgpt-ai/k8sgpt/cmd/serve" + "github.com/k8sgpt-ai/k8sgpt/cmd/analyze" "github.com/k8sgpt-ai/k8sgpt/cmd/auth" "github.com/k8sgpt-ai/k8sgpt/cmd/filters" @@ -81,6 +82,7 @@ func initConfig() { viper.Set("kubecontext", kubecontext) viper.Set("kubeconfig", kubeconfig) + viper.SetEnvPrefix("K8SGPT") viper.AutomaticEnv() // read in environment variables that match // If a config file is found, read it in. diff --git a/cmd/serve/serve.go b/cmd/serve/serve.go index a57091b1b4..e84984d219 100644 --- a/cmd/serve/serve.go +++ b/cmd/serve/serve.go @@ -28,17 +28,31 @@ var ServeCmd = &cobra.Command{ color.Red("Error: %v", err) os.Exit(1) } - + var aiProvider *ai.AIProvider if len(configAI.Providers) == 0 { - color.Red("Error: AI provider not specified in configuration. Please run k8sgpt auth") - os.Exit(1) + // Check for env injection + backend = os.Getenv("K8SGPT_BACKEND") + password := os.Getenv("K8SGPT_PASSWORD") + model := os.Getenv("K8SGPT_MODEL") + // If the envs are set, alocate in place to the aiProvider + // else exit with error + if backend != "" || password != "" || model != "" { + aiProvider = &ai.AIProvider{ + Name: backend, + Password: password, + Model: model, + } + } else { + color.Red("Error: AI provider not specified in configuration. Please run k8sgpt auth") + os.Exit(1) + } } - - var aiProvider ai.AIProvider - for _, provider := range configAI.Providers { - if backend == provider.Name { - aiProvider = provider - break + if aiProvider == nil { + for _, provider := range configAI.Providers { + if backend == provider.Name { + aiProvider = &provider + break + } } }