- Remove old database Config struct and related functions - Update database.New() to accept config.Config parameter - Update routingtable.New() to accept config.Config parameter - Update snapshotter.New() to accept config.Config parameter - Simplify fx module providers in app.go - Fix truthiness check for environment variables - Handle empty state directory gracefully in routing table and snapshotter - Update all tests to use empty state directory for testing
92 lines
2.3 KiB
Go
92 lines
2.3 KiB
Go
// Package config provides centralized configuration management for RouteWatch
|
|
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
// AppIdentifier is the reverse domain name identifier for the app
|
|
AppIdentifier = "berlin.sneak.app.routewatch"
|
|
|
|
// dirPermissions for creating directories
|
|
dirPermissions = 0750 // rwxr-x---
|
|
)
|
|
|
|
// Config holds configuration for the entire application
|
|
type Config struct {
|
|
// StateDir is the directory for all application state (database, snapshots)
|
|
StateDir string
|
|
|
|
// MaxRuntime is the maximum runtime (0 = run forever)
|
|
MaxRuntime time.Duration
|
|
}
|
|
|
|
// New creates a new Config with default paths based on the OS
|
|
func New() (*Config, error) {
|
|
stateDir, err := getStateDirectory()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to determine state directory: %w", err)
|
|
}
|
|
|
|
return &Config{
|
|
StateDir: stateDir,
|
|
MaxRuntime: 0, // Run forever by default
|
|
}, nil
|
|
}
|
|
|
|
// GetStateDir returns the state directory path
|
|
func (c *Config) GetStateDir() string {
|
|
return c.StateDir
|
|
}
|
|
|
|
// getStateDirectory returns the appropriate state directory based on the OS
|
|
func getStateDirectory() (string, error) {
|
|
switch runtime.GOOS {
|
|
case "darwin":
|
|
// macOS: ~/Library/Application Support/berlin.sneak.app.routewatch
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return filepath.Join(home, "Library", "Application Support", AppIdentifier), nil
|
|
|
|
case "linux", "freebsd", "openbsd", "netbsd":
|
|
// Unix-like: /var/lib/berlin.sneak.app.routewatch if root, else XDG_DATA_HOME
|
|
if os.Geteuid() == 0 {
|
|
return filepath.Join("/var/lib", AppIdentifier), nil
|
|
}
|
|
|
|
// Check XDG_DATA_HOME first
|
|
if xdgData := os.Getenv("XDG_DATA_HOME"); xdgData != "" {
|
|
return filepath.Join(xdgData, AppIdentifier), nil
|
|
}
|
|
|
|
// Fall back to ~/.local/share/berlin.sneak.app.routewatch
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return filepath.Join(home, ".local", "share", AppIdentifier), nil
|
|
|
|
default:
|
|
return "", fmt.Errorf("unsupported operating system: %s", runtime.GOOS)
|
|
}
|
|
}
|
|
|
|
// EnsureDirectories creates all necessary directories if they don't exist
|
|
func (c *Config) EnsureDirectories() error {
|
|
// Ensure state directory exists
|
|
if err := os.MkdirAll(c.StateDir, dirPermissions); err != nil {
|
|
return fmt.Errorf("failed to create state directory: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|