Initial scaffold: project structure, .gitignore, go.mod

This commit is contained in:
Axel Meyer
2026-03-03 21:07:31 +01:00
commit 2256df9dd7
15 changed files with 892 additions and 0 deletions

97
internal/config/config.go Normal file
View File

@@ -0,0 +1,97 @@
package config
import (
"encoding/json"
"os"
"sync"
)
// Config holds SyncWarden configuration.
type Config struct {
// Connection
SyncthingAddress string `json:"syncthing_address"`
SyncthingAPIKey string `json:"syncthing_api_key"`
SyncthingUseTLS bool `json:"syncthing_use_tls"`
// Feature toggles
EnableNotifications bool `json:"enable_notifications"`
EnableRecentFiles bool `json:"enable_recent_files"`
EnableConflictAlerts bool `json:"enable_conflict_alerts"`
EnableTransferRate bool `json:"enable_transfer_rate"`
AutoStartSyncthing bool `json:"auto_start_syncthing"`
StartOnLogin bool `json:"start_on_login"`
// Per-event notification toggles
NotifySyncComplete bool `json:"notify_sync_complete"`
NotifyDeviceConnect bool `json:"notify_device_connect"`
NotifyDeviceDisconnect bool `json:"notify_device_disconnect"`
NotifyNewDevice bool `json:"notify_new_device"`
NotifyConflict bool `json:"notify_conflict"`
// Persisted state
LastEventID int `json:"last_event_id"`
}
var defaults = Config{
SyncthingAddress: "localhost:8384",
SyncthingAPIKey: "",
SyncthingUseTLS: false,
EnableNotifications: true,
EnableRecentFiles: true,
EnableConflictAlerts: true,
EnableTransferRate: true,
AutoStartSyncthing: false,
StartOnLogin: false,
NotifySyncComplete: true,
NotifyDeviceConnect: false,
NotifyDeviceDisconnect: true,
NotifyNewDevice: true,
NotifyConflict: true,
LastEventID: 0,
}
var (
mu sync.Mutex
cached *Config
)
// Load reads config from disk, merging with defaults.
func Load() Config {
mu.Lock()
defer mu.Unlock()
cfg := defaults
data, err := os.ReadFile(ConfigPath())
if err != nil {
return cfg
}
_ = json.Unmarshal(data, &cfg)
cached = &cfg
return cfg
}
// Save writes config to disk.
func Save(cfg Config) error {
mu.Lock()
defer mu.Unlock()
dir := ConfigDir()
if err := os.MkdirAll(dir, 0o755); err != nil {
return err
}
data, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return err
}
cached = &cfg
return os.WriteFile(ConfigPath(), data, 0o644)
}
// BaseURL returns the full Syncthing base URL.
func (c Config) BaseURL() string {
scheme := "http"
if c.SyncthingUseTLS {
scheme = "https"
}
return scheme + "://" + c.SyncthingAddress
}

8
internal/config/paths.go Normal file
View File

@@ -0,0 +1,8 @@
package config
import "path/filepath"
// ConfigPath returns the path to config.json.
func ConfigPath() string {
return filepath.Join(ConfigDir(), "config.json")
}

View File

@@ -0,0 +1,20 @@
//go:build darwin
package config
import (
"os"
"path/filepath"
)
// ConfigDir returns ~/Library/Application Support/syncwarden.
func ConfigDir() string {
home, _ := os.UserHomeDir()
return filepath.Join(home, "Library", "Application Support", "syncwarden")
}
// SyncthingConfigPath returns the default Syncthing config.xml path on macOS.
func SyncthingConfigPath() string {
home, _ := os.UserHomeDir()
return filepath.Join(home, "Library", "Application Support", "Syncthing", "config.xml")
}

View File

@@ -0,0 +1,26 @@
//go:build linux
package config
import (
"os"
"path/filepath"
)
// ConfigDir returns ~/.config/syncwarden.
func ConfigDir() string {
if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" {
return filepath.Join(xdg, "syncwarden")
}
home, _ := os.UserHomeDir()
return filepath.Join(home, ".config", "syncwarden")
}
// SyncthingConfigPath returns the default Syncthing config.xml path on Linux.
func SyncthingConfigPath() string {
if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" {
return filepath.Join(xdg, "syncthing", "config.xml")
}
home, _ := os.UserHomeDir()
return filepath.Join(home, ".config", "syncthing", "config.xml")
}

View File

@@ -0,0 +1,18 @@
//go:build windows
package config
import (
"os"
"path/filepath"
)
// ConfigDir returns %LOCALAPPDATA%\syncwarden.
func ConfigDir() string {
return filepath.Join(os.Getenv("LOCALAPPDATA"), "syncwarden")
}
// SyncthingConfigPath returns the default Syncthing config.xml path on Windows.
func SyncthingConfigPath() string {
return filepath.Join(os.Getenv("LOCALAPPDATA"), "Syncthing", "config.xml")
}