|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | +) |
| 9 | + |
| 10 | +// Config holds the application configuration |
| 11 | +type Config struct { |
| 12 | + SentryAPIToken string `json:"sentry_api_token"` |
| 13 | +} |
| 14 | + |
| 15 | +// LoadConfig loads configuration from environment variables or config file |
| 16 | +// Environment variables take precedence over config file values |
| 17 | +func LoadConfig() (*Config, error) { |
| 18 | + config := &Config{} |
| 19 | + |
| 20 | + // First, try to load from environment variable |
| 21 | + if token := os.Getenv("SENTRY_API_TOKEN"); token != "" { |
| 22 | + config.SentryAPIToken = token |
| 23 | + return config, nil |
| 24 | + } |
| 25 | + |
| 26 | + // If env var is not set, try to load from config file |
| 27 | + configPath, err := getConfigPath() |
| 28 | + if err != nil { |
| 29 | + return nil, fmt.Errorf("failed to determine config path: %w", err) |
| 30 | + } |
| 31 | + |
| 32 | + if err := loadFromFile(configPath, config); err != nil { |
| 33 | + // If config file doesn't exist or has issues, return error about missing token |
| 34 | + return nil, fmt.Errorf("SENTRY_API_TOKEN environment variable is required (or configure ~/.config/sentire/config.json)") |
| 35 | + } |
| 36 | + |
| 37 | + // Validate that we have a token |
| 38 | + if config.SentryAPIToken == "" { |
| 39 | + return nil, fmt.Errorf("SENTRY_API_TOKEN is required in config file") |
| 40 | + } |
| 41 | + |
| 42 | + return config, nil |
| 43 | +} |
| 44 | + |
| 45 | +// getConfigPath returns the path to the config file |
| 46 | +func getConfigPath() (string, error) { |
| 47 | + homeDir, err := os.UserHomeDir() |
| 48 | + if err != nil { |
| 49 | + return "", fmt.Errorf("failed to get user home directory: %w", err) |
| 50 | + } |
| 51 | + |
| 52 | + return filepath.Join(homeDir, ".config", "sentire", "config.json"), nil |
| 53 | +} |
| 54 | + |
| 55 | +// loadFromFile loads configuration from a JSON file |
| 56 | +func loadFromFile(path string, config *Config) error { |
| 57 | + file, err := os.Open(path) |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("failed to open config file: %w", err) |
| 60 | + } |
| 61 | + defer file.Close() |
| 62 | + |
| 63 | + decoder := json.NewDecoder(file) |
| 64 | + if err := decoder.Decode(config); err != nil { |
| 65 | + return fmt.Errorf("failed to parse config file: %w", err) |
| 66 | + } |
| 67 | + |
| 68 | + return nil |
| 69 | +} |
| 70 | + |
| 71 | +// SaveConfig saves the current configuration to the config file |
| 72 | +// This is mainly for future use when we add more configuration options |
| 73 | +func SaveConfig(config *Config) error { |
| 74 | + configPath, err := getConfigPath() |
| 75 | + if err != nil { |
| 76 | + return fmt.Errorf("failed to determine config path: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + // Create directory if it doesn't exist |
| 80 | + configDir := filepath.Dir(configPath) |
| 81 | + if err := os.MkdirAll(configDir, 0755); err != nil { |
| 82 | + return fmt.Errorf("failed to create config directory: %w", err) |
| 83 | + } |
| 84 | + |
| 85 | + file, err := os.Create(configPath) |
| 86 | + if err != nil { |
| 87 | + return fmt.Errorf("failed to create config file: %w", err) |
| 88 | + } |
| 89 | + defer file.Close() |
| 90 | + |
| 91 | + encoder := json.NewEncoder(file) |
| 92 | + encoder.SetIndent("", " ") |
| 93 | + if err := encoder.Encode(config); err != nil { |
| 94 | + return fmt.Errorf("failed to write config file: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + return nil |
| 98 | +} |
0 commit comments