From 8383a241fc3cf5b022c9c53f8f19690edf04177b Mon Sep 17 00:00:00 2001 From: s Date: Mon, 10 Nov 2025 15:22:32 -0500 Subject: refactor: restructure client modules and add config file support - 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 --- internal/config/config.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) (limited to 'internal/config/config.go') 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 +} -- cgit v1.2.3