Load ~/.config/ollie/env before reading backend env vars
Env file is optional; existing environment variables take precedence. Supports KEY=VALUE format with # comments and blank lines ignored. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
6c9f056ed8
commit
463457cd14
|
|
@ -1,16 +1,52 @@
|
||||||
package backend
|
package backend
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// loadEnvFile reads KEY=VALUE pairs from path and sets any key that is not
|
||||||
|
// already present in the environment. Lines beginning with # and blank lines
|
||||||
|
// are ignored. Errors opening the file are silently ignored (file is optional).
|
||||||
|
func loadEnvFile(path string) {
|
||||||
|
f, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
sc := bufio.NewScanner(f)
|
||||||
|
for sc.Scan() {
|
||||||
|
line := strings.TrimSpace(sc.Text())
|
||||||
|
if line == "" || strings.HasPrefix(line, "#") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
k, v, ok := strings.Cut(line, "=")
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
k = strings.TrimSpace(k)
|
||||||
|
v = strings.TrimSpace(v)
|
||||||
|
if k != "" && os.Getenv(k) == "" {
|
||||||
|
os.Setenv(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// New constructs a Backend from environment variables.
|
// New constructs a Backend from environment variables.
|
||||||
|
// Values are loaded from ~/.config/ollie/env before consulting the environment,
|
||||||
|
// so the file acts as a default; variables already set in the environment take
|
||||||
|
// precedence.
|
||||||
//
|
//
|
||||||
// OLLIE_BACKEND ollama | openai | openrouter (default: ollama)
|
// OLLIE_BACKEND ollama | openai | openrouter (default: ollama)
|
||||||
// OLLIE_API_URL override base URL for the selected backend
|
// OLLIE_API_URL override base URL for the selected backend
|
||||||
// OLLIE_API_KEY API key (required for openai / openrouter)
|
// OLLIE_API_KEY API key (required for openai / openrouter)
|
||||||
func New() (Backend, error) {
|
func New() (Backend, error) {
|
||||||
|
home, _ := os.UserHomeDir()
|
||||||
|
loadEnvFile(home + "/.config/ollie/env")
|
||||||
|
|
||||||
which := os.Getenv("OLLIE_BACKEND")
|
which := os.Getenv("OLLIE_BACKEND")
|
||||||
if which == "" {
|
if which == "" {
|
||||||
which = "ollama"
|
which = "ollama"
|
||||||
|
|
|
||||||
Reference in New Issue