initial
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package settings
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/DeanPDX/dotconfig"
|
||||
)
|
||||
|
||||
type Go4lageSettings struct {
|
||||
OPENROUTER_KEY string `env:"OPENROUTER_KEY"`
|
||||
BRAVE_KEY string `env:"BRAVE_KEY"`
|
||||
TARGET_COST_USD float64 `env:"TARGET_COST_USD"`
|
||||
TARGET_TIME_MINS int `env:"TARGET_TIME_MINS"`
|
||||
NROFAGENTS int `env:"NROFAGENTS"`
|
||||
}
|
||||
|
||||
var (
|
||||
mu sync.RWMutex
|
||||
settings Go4lageSettings
|
||||
)
|
||||
|
||||
func init() {
|
||||
err := Load(".env")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "warnung: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Load reads the settings file. Call once at startup.
|
||||
func Load(path string) error {
|
||||
s, err := dotconfig.FromFileName[Go4lageSettings](path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("settings: load %s: %w", path, err)
|
||||
}
|
||||
mu.Lock()
|
||||
settings = s
|
||||
mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get returns a copy of the current settings.
|
||||
func Get() Go4lageSettings {
|
||||
mu.RLock()
|
||||
defer mu.RUnlock()
|
||||
return settings
|
||||
}
|
||||
|
||||
// Change modifies the settings under the write lock.
|
||||
func Change(fn func(s *Go4lageSettings)) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
fn(&settings)
|
||||
}
|
||||
Reference in New Issue
Block a user