Files
wild-central/internal/storage/storage.go
Paul Payne 428d47f876 Refactor architecture: extract reconciler, add interfaces, reduce complexity, improve test coverage
Architecture:
- Extract reconcileNetworking into internal/reconcile package with 7 consumer-side interfaces
- Add locked modifyState helper to fix state.yaml read-modify-write race condition
- Extract CrowdSec and VPN handler groups with interfaces documenting dependency surface
- Replace raw map[string]any YAML manipulation with typed AddDHCPStaticLease/RemoveDHCPStaticLease
- Extract Cloudflare API functions into cfClient struct, eliminating repeated auth boilerplate

Complexity reduction:
- haproxy.GenerateWithOpts: 50 → 6 (extracted 8 focused helpers)
- reconcile.Reconcile: 42 → 11 (extracted buildRoutes, buildDNSEntries, writeHAProxyConfig)
- AutheliaUpdateConfig: 25 → 10 (extracted enableAuthelia/disableAuthelia)

Test coverage improvements:
- reconcile: 4.5% → 56.8% (stub-based tests for route building, DNS entries, orchestration)
- dnsfilter: 10.7% → 45.6% (Manager.Compile, AddList, ToggleList, custom entries)
- config: 67.3% → 89.8% (DHCP static lease mutation tests)
2026-07-14 04:21:30 +00:00

111 lines
2.5 KiB
Go

package storage
import (
"fmt"
"os"
"path/filepath"
"syscall"
)
// EnsureDir creates a directory with specified permissions if it doesn't exist
func EnsureDir(path string, perm os.FileMode) error {
if err := os.MkdirAll(path, perm); err != nil {
return fmt.Errorf("creating directory %s: %w", path, err)
}
return nil
}
// FileExists checks if a file exists
func FileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
// WriteFile writes content to a file with specified permissions
func WriteFile(path string, content []byte, perm os.FileMode) error {
if err := os.WriteFile(path, content, perm); err != nil {
return fmt.Errorf("writing file %s: %w", path, err)
}
return nil
}
// ReadFile reads content from a file
func ReadFile(path string) ([]byte, error) {
content, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("reading file %s: %w", path, err)
}
return content, nil
}
// Lock represents a file lock
type Lock struct {
file *os.File
path string
}
// AcquireLock acquires an exclusive lock on a file
func AcquireLock(lockPath string) (*Lock, error) {
// Ensure lock directory exists
if err := EnsureDir(filepath.Dir(lockPath), 0755); err != nil {
return nil, err
}
// Open or create lock file
file, err := os.OpenFile(lockPath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return nil, fmt.Errorf("opening lock file %s: %w", lockPath, err)
}
// Acquire exclusive lock with flock
if err := syscall.Flock(int(file.Fd()), syscall.LOCK_EX); err != nil {
file.Close()
return nil, fmt.Errorf("acquiring lock on %s: %w", lockPath, err)
}
return &Lock{
file: file,
path: lockPath,
}, nil
}
// Release releases the file lock
func (l *Lock) Release() error {
if l.file == nil {
return nil
}
// Release flock
if err := syscall.Flock(int(l.file.Fd()), syscall.LOCK_UN); err != nil {
l.file.Close()
return fmt.Errorf("releasing lock on %s: %w", l.path, err)
}
// Close file
if err := l.file.Close(); err != nil {
return fmt.Errorf("closing lock file %s: %w", l.path, err)
}
l.file = nil
return nil
}
// WithLock executes a function while holding a lock
func WithLock(lockPath string, fn func() error) error {
lock, err := AcquireLock(lockPath)
if err != nil {
return err
}
defer func() { _ = lock.Release() }()
return fn()
}
// EnsureFilePermissions ensures a file has the correct permissions
func EnsureFilePermissions(path string, perm os.FileMode) error {
if err := os.Chmod(path, perm); err != nil {
return fmt.Errorf("setting permissions on %s: %w", path, err)
}
return nil
}