diff options
| author | s <[email protected]> | 2025-11-10 15:22:32 -0500 |
|---|---|---|
| committer | s <[email protected]> | 2025-11-10 15:22:32 -0500 |
| commit | 8383a241fc3cf5b022c9c53f8f19690edf04177b (patch) | |
| tree | 887a489f7931d07373530c7e053f0343dca65e1d /internal/config | |
| parent | 9a9e79f232b83d3bd2a816287272515863df1299 (diff) | |
| download | dborg-8383a241fc3cf5b022c9c53f8f19690edf04177b.tar.gz dborg-8383a241fc3cf5b022c9c53f8f19690edf04177b.zip | |
refactor: restructure client modules and add config file supportv0.8.1
- Split large osint.go client into focused modules (bssid.go, breachforum.go, buckets.go, etc.)
- Add config file support with init command for API key management
- Remove api-key flag in favor of config file + env var fallback
- Update API paths to remove /osint prefix
- Add crawl endpoint streaming support
- Improve error handling with 402 payment required status
Diffstat (limited to 'internal/config')
| -rw-r--r-- | internal/config/config.go | 71 | ||||
| -rw-r--r-- | internal/config/errors.go | 2 |
2 files changed, 71 insertions, 2 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 +} diff --git a/internal/config/errors.go b/internal/config/errors.go index 4fd3636..b733071 100644 --- a/internal/config/errors.go +++ b/internal/config/errors.go @@ -3,5 +3,5 @@ package config import "errors" var ( - ErrMissingAPIKey = errors.New("API key required: set DBORG_API_KEY environment variable or use --api-key flag") + ErrMissingAPIKey = errors.New("API key required: run 'dborg init' to configure your API key or set DBORG_API_KEY environment variable") ) |
