summaryrefslogtreecommitdiffstats
path: root/internal/config/config.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/config/config.go')
-rw-r--r--internal/config/config.go71
1 files changed, 70 insertions, 1 deletions
diff --git a/internal/config/config.go b/internal/config/config.go
index e1da2c2..b8538be 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -1,7 +1,10 @@
package config
import (
+ "encoding/json"
+ "fmt"
"os"
+ "path/filepath"
"time"
)
@@ -13,9 +16,21 @@ type Config struct {
UserAgent string
}
+type FileConfig struct {
+ APIKey string `json:"api_key"`
+}
+
func New() *Config {
+ apiKey := os.Getenv("DBORG_API_KEY")
+
+ if apiKey == "" {
+ if fileCfg, err := LoadConfig(); err == nil && fileCfg.APIKey != "" {
+ apiKey = fileCfg.APIKey
+ }
+ }
+
return &Config{
- APIKey: os.Getenv("DBORG_API_KEY"),
+ APIKey: apiKey,
BaseURL: "https://db.org.ai",
Timeout: 30 * time.Second,
MaxRetries: 3,
@@ -36,3 +51,57 @@ func (c *Config) Validate() error {
}
return nil
}
+
+func GetConfigPath() (string, error) {
+ homeDir, err := os.UserHomeDir()
+ if err != nil {
+ return "", fmt.Errorf("failed to get home directory: %w", err)
+ }
+
+ configDir := filepath.Join(homeDir, ".config", "dborg")
+ if err := os.MkdirAll(configDir, 0755); err != nil {
+ return "", fmt.Errorf("failed to create config directory: %w", err)
+ }
+
+ return filepath.Join(configDir, "config.json"), nil
+}
+
+func LoadConfig() (*FileConfig, error) {
+ configPath, err := GetConfigPath()
+ if err != nil {
+ return nil, err
+ }
+
+ data, err := os.ReadFile(configPath)
+ if err != nil {
+ if os.IsNotExist(err) {
+ return &FileConfig{}, nil
+ }
+ return nil, fmt.Errorf("failed to read config file: %w", err)
+ }
+
+ var cfg FileConfig
+ if err := json.Unmarshal(data, &cfg); err != nil {
+ return nil, fmt.Errorf("failed to parse config file: %w", err)
+ }
+
+ return &cfg, nil
+}
+
+func SaveConfig(cfg *FileConfig) error {
+ configPath, err := GetConfigPath()
+ if err != nil {
+ return err
+ }
+
+ data, err := json.MarshalIndent(cfg, "", " ")
+ if err != nil {
+ return fmt.Errorf("failed to marshal config: %w", err)
+ }
+
+ if err := os.WriteFile(configPath, data, 0600); err != nil {
+ return fmt.Errorf("failed to write config file: %w", err)
+ }
+
+ return nil
+}