Skip to content

Commit

Permalink
Enabling Config Values to Use ENV Variables
Browse files Browse the repository at this point in the history
  • Loading branch information
diaakasem authored and ido50 committed Oct 29, 2024
1 parent 3055cef commit 55dde84
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ default_backend = "official_openai" # Default backend when one is not selected
[backends.official_openai]
type = "openai"
api_key = "API KEY"
# Or
# api_key = "$OPENAI_API_KEY"
default_model = "gpt-4o" # Default model to use for this backend

[backends.azure_openai]
Expand Down
28 changes: 28 additions & 0 deletions libaiac/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package libaiac

import (
"fmt"
"os"

"github.com/BurntSushi/toml"
"github.com/adrg/xdg"
Expand Down Expand Up @@ -83,5 +84,32 @@ func LoadConfig(path string) (conf Config, err error) {
return conf, fmt.Errorf("failed loading configuration: %w", err)
}

// If any of the config values are env vars, replace them
conf = replaceEnvVars(conf)

return conf, nil
}

// replaceEnvVars replaces any environment variables in the config with their
// actual values.
func replaceEnvVars(conf Config) Config {
for backendName, backendConfig := range conf.Backends {
if backendConfig.APIKey != "" {
backendConfig.APIKey = replaceEnvVar(backendConfig.APIKey)
}

if backendConfig.AWSProfile != "" {
backendConfig.AWSProfile = replaceEnvVar(backendConfig.AWSProfile)
}

conf.Backends[backendName] = backendConfig
}

return conf
}

// replaceEnvVar replaces an environment variable in a string with its actual
// value.
func replaceEnvVar(s string) string {
return os.ExpandEnv(s)
}

0 comments on commit 55dde84

Please sign in to comment.