Skip to content

Commit

Permalink
feat: envs to initialise server
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Jones <alexsimonjones@gmail.com>
  • Loading branch information
AlexsJones committed Apr 15, 2023
1 parent 51b1b35 commit 0071e25
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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.
Expand Down
32 changes: 23 additions & 9 deletions cmd/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down

0 comments on commit 0071e25

Please sign in to comment.