From c3cdb38707bfe0d7bf66ed7ff5cee53d0a10903b Mon Sep 17 00:00:00 2001 From: Bene Date: Sun, 22 Nov 2026 09:27:46 +0100 Subject: [PATCH 01/20] config: change default config dir from ~/.free-llm-proxy-router to ~/.config/free-llm-proxy-router Update all config search paths, default paths, and test expectations to use the XDG-compliant ~/.config/free-llm-proxy-router directory instead of the legacy ~/.free-llm-proxy-router location. --- pkg/config/config.go | 8 ++++---- pkg/config/datafile.go | 4 ++-- pkg/config/dotenv.go | 2 +- pkg/config/fallback_test.go | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 381e117..124b740 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -125,7 +125,7 @@ var current *Config // Load reads the configuration from the given file path. // If path is empty, it searches for config.yaml in the current directory -// and in ~/.free-llm-proxy-router/. +// and in ~/.config/free-llm-proxy-router/. // // Before returning, Load looks for .env and .secrets files in standard // locations and populates them into the dotenv map so that ResolvedAuth() @@ -143,7 +143,7 @@ func Load(path string) (*Config, error) { v.SetConfigName("config") v.SetConfigType("yaml") v.AddConfigPath(".") - v.AddConfigPath(expandHome("~/.free-llm-proxy-router")) + v.AddConfigPath(expandHome("~/.config/free-llm-proxy-router")) v.AddConfigPath("configs") } @@ -256,8 +256,8 @@ func setDefaults(v *viper.Viper) { v.SetDefault("proxy.cache_ttl", 300) v.SetDefault("proxy.log_level", "info") v.SetDefault("proxy.strategy", "adaptive") - v.SetDefault("catalog.path", expandHome("~/.free-llm-proxy-router/catalog.json")) - v.SetDefault("catalog.enriched_path", expandHome("~/.free-llm-proxy-router/enriched.json")) + v.SetDefault("catalog.path", expandHome("~/.config/free-llm-proxy-router/catalog.json")) + v.SetDefault("catalog.enriched_path", expandHome("~/.config/free-llm-proxy-router/enriched.json")) v.SetDefault("catalog.max_age_hours", 24) v.SetDefault("fallback.retry_on_429", true) v.SetDefault("fallback.retry_on_5xx", true) diff --git a/pkg/config/datafile.go b/pkg/config/datafile.go index 3da76a0..90db61a 100644 --- a/pkg/config/datafile.go +++ b/pkg/config/datafile.go @@ -11,7 +11,7 @@ import ( // Search order, first existing file wins: // 1. ./configs/ (running from a source checkout) // 2. / (next to an explicit --config file) -// 3. ~/.free-llm-proxy-router/ +// 3. ~/.config/free-llm-proxy-router/ // 4. /configs/ (release archive layout) // // If nothing is found, "configs/" is returned so callers report the @@ -21,7 +21,7 @@ func FindDataFile(name, configPath string) string { if configPath != "" { candidates = append(candidates, filepath.Join(filepath.Dir(configPath), name)) } - candidates = append(candidates, filepath.Join(expandHome("~/.free-llm-proxy-router"), name)) + candidates = append(candidates, filepath.Join(expandHome("~/.config/free-llm-proxy-router"), name)) if exe, err := os.Executable(); err == nil { if resolved, err := filepath.EvalSymlinks(exe); err == nil { exe = resolved diff --git a/pkg/config/dotenv.go b/pkg/config/dotenv.go index 110c72f..5bf0145 100644 --- a/pkg/config/dotenv.go +++ b/pkg/config/dotenv.go @@ -96,7 +96,7 @@ func dotenvLookup(key string) string { // configDir is the directory of the loaded config file (may be ""). func defaultEnvPaths(configDir string) []string { home, _ := os.UserHomeDir() - picoclaw := filepath.Join(home, ".free-llm-proxy-router") + picoclaw := filepath.Join(home, ".config", "free-llm-proxy-router") paths := []string{ // Project-local (highest priority among files) diff --git a/pkg/config/fallback_test.go b/pkg/config/fallback_test.go index 3db43f6..edb4191 100644 --- a/pkg/config/fallback_test.go +++ b/pkg/config/fallback_test.go @@ -53,7 +53,7 @@ func TestCatalogReadPath(t *testing.T) { chdir(t, t.TempDir()) home := t.TempDir() t.Setenv("HOME", home) - userPath := "~/.free-llm-proxy-router/catalog.json" + userPath := "~/.config/free-llm-proxy-router/catalog.json" // Neither exists: configured path is returned unchanged. if got := CatalogReadPath(userPath, ""); got != userPath { @@ -68,7 +68,7 @@ func TestCatalogReadPath(t *testing.T) { } // User catalog exists (after a scan): it wins over the bundled one. - writeFile(t, filepath.Join(home, ".free-llm-proxy-router", "catalog.json"), "{}") + writeFile(t, filepath.Join(home, ".config/free-llm-proxy-router", "catalog.json"), "{}") if got := CatalogReadPath(userPath, ""); got != userPath { t.Errorf("user catalog present: got %q, want %q", got, userPath) } From 7b3f9e5735daafb35e290be53e71b9aa8c547853 Mon Sep 17 00:00:00 2001 From: Bene Date: Sun, 22 Nov 2026 09:27:57 +0100 Subject: [PATCH 02/20] cmd: update config paths in all CLI tools to use ~/.config/free-llm-proxy-router Update free-llm-proxy, free-llm-bench, and free-llm-scan to reference the new XDG-compliant config directory for config files, usage data, reliability data, and catalog files. --- cmd/free-llm-bench/main.go | 2 +- cmd/free-llm-proxy/main.go | 6 +++--- cmd/free-llm-scan/main.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/free-llm-bench/main.go b/cmd/free-llm-bench/main.go index 23a91e0..1bbb87b 100644 --- a/cmd/free-llm-bench/main.go +++ b/cmd/free-llm-bench/main.go @@ -52,7 +52,7 @@ func runBench(cmd *cobra.Command, args []string) error { // Build all strategies relTracker := reliability.New() - reliability.Load(relTracker, "~/.free-llm-proxy-router/reliability.json") + reliability.Load(relTracker, "~/.config/free-llm-proxy-router/reliability.json") rateLimiter := ratelimit.NewGlobalTracker() stratReg := strategy.NewRegistry(relTracker, rateLimiter, "", "gemini-2.0-flash-lite", 3, 5) diff --git a/cmd/free-llm-proxy/main.go b/cmd/free-llm-proxy/main.go index b0f28ba..2feb950 100644 --- a/cmd/free-llm-proxy/main.go +++ b/cmd/free-llm-proxy/main.go @@ -36,7 +36,7 @@ func main() { Short: "OpenAI-compatible proxy for free LLM providers", RunE: runProxy, } - root.Flags().StringVarP(&cfgFile, "config", "c", "", "config file path (default: search in . and ~/.free-llm-proxy-router/)") + root.Flags().StringVarP(&cfgFile, "config", "c", "", "config file path (default: search in . and ~/.config/free-llm-proxy-router/)") root.Flags().StringVar(&host, "host", "", "override listen address (default from config: 127.0.0.1; use 0.0.0.0 to expose)") root.Flags().IntVarP(&port, "port", "p", 0, "override proxy port (default from config: 8080)") root.Flags().StringVarP(&stratName, "strategy", "s", "", "override strategy name") @@ -113,7 +113,7 @@ func runProxy(cmd *cobra.Command, args []string) error { geminiTracker := ratelimit.NewGeminiTracker() // Load persisted rate-limit usage - usagePath := "~/.free-llm-proxy-router/usage.json" + usagePath := "~/.config/free-llm-proxy-router/usage.json" if usage, err := ratelimit.LoadUsage(usagePath); err != nil { log.Printf("usage.json unreadable (starting fresh): %v", err) } else { @@ -123,7 +123,7 @@ func runProxy(cmd *cobra.Command, args []string) error { // Set up reliability tracker reliabilityTracker := reliability.New() - relPath := "~/.free-llm-proxy-router/reliability.json" + relPath := "~/.config/free-llm-proxy-router/reliability.json" if err := reliability.Load(reliabilityTracker, relPath); err != nil { log.Printf("reliability.json not found (starting fresh): %v", err) } diff --git a/cmd/free-llm-scan/main.go b/cmd/free-llm-scan/main.go index db1b996..8a01d4a 100644 --- a/cmd/free-llm-scan/main.go +++ b/cmd/free-llm-scan/main.go @@ -133,7 +133,7 @@ func updateCmd() *cobra.Command { func checkScanTargets(providers []config.ProviderConfig) error { if len(providers) == 0 { return fmt.Errorf("no providers configured: no config.yaml was found in ., " + - "~/.free-llm-proxy-router or configs/, and no bundled config.example.yaml next to the binary. " + + "~/.config/free-llm-proxy-router or configs/, and no bundled config.example.yaml next to the binary. " + "Run `bash scripts/setup.sh`, or pass `-c path/to/config.yaml`") } for _, p := range providers { From 767eed29a1494cab1889b61d50be3b88bb06dd4f Mon Sep 17 00:00:00 2001 From: Bene Date: Sun, 22 Nov 2026 09:28:02 +0100 Subject: [PATCH 03/20] configs: update example config files to use ~/.config/free-llm-proxy-router Update .secrets.example and config.example.yaml to reference the new XDG-compliant config directory for secrets, catalog, and enriched catalog paths. --- configs/.secrets.example | 4 ++-- configs/config.example.yaml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/configs/.secrets.example b/configs/.secrets.example index e26c5de..a89ea0e 100644 --- a/configs/.secrets.example +++ b/configs/.secrets.example @@ -1,10 +1,10 @@ # free-llm-proxy-router API keys -# Copy this file to ~/.free-llm-proxy-router/.secrets (or .env in the project root) +# Copy this file to ~/.config/free-llm-proxy-router/.secrets (or .env in the project root) # and fill in your keys. Lines starting with # are ignored. # # Key lookup order (first match wins): # 1. Real environment variable (export KEY=...) -# 2. ~/.free-llm-proxy-router/.secrets (or .env) +# 2. ~/.config/free-llm-proxy-router/.secrets (or .env) # 3. .secrets / .env in the project directory # # NEVER commit this file to git. diff --git a/configs/config.example.yaml b/configs/config.example.yaml index 7c1a82c..6f7decf 100644 --- a/configs/config.example.yaml +++ b/configs/config.example.yaml @@ -132,8 +132,8 @@ refresh: output_merge: "conservative" catalog: - path: "~/.free-llm-proxy-router/catalog.json" - enriched_path: "~/.free-llm-proxy-router/enriched.json" + path: "~/.config/free-llm-proxy-router/catalog.json" + enriched_path: "~/.config/free-llm-proxy-router/enriched.json" max_age_hours: 24 auto_scan_on_start: false From 0f6a39a836ed0dfa5039f3378afc425d3975978c Mon Sep 17 00:00:00 2001 From: Bene Date: Sun, 22 Nov 2026 09:28:11 +0100 Subject: [PATCH 04/20] docs: update README and .env.example to reference ~/.config/free-llm-proxy-router Update documentation to reflect the new XDG-compliant config directory for config files, secrets, and deployment instructions. --- .env.example | 2 +- README.md | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index d609c03..75c9c92 100644 --- a/.env.example +++ b/.env.example @@ -6,7 +6,7 @@ # Keys are loaded in this order (first match wins): # 1. Real environment variable (export KEY=...) # 2. .env in the project directory ← this file -# 3. ~/.free-llm-proxy-router/.secrets +# 3. ~/.config/free-llm-proxy-router/.secrets # # .env is gitignored — never commit real keys. # Leave unused providers blank; they will be skipped. diff --git a/README.md b/README.md index 88b271f..dfa168d 100644 --- a/README.md +++ b/README.md @@ -154,7 +154,7 @@ The [install script](#install) picks the right archive on the board itself. For ```bash CGO_ENABLED=0 GOOS=linux GOARCH=mipsle go build -ldflags="-s -w" -o free-llm-proxy ./cmd/free-llm-proxy -scp -r free-llm-proxy configs/ user@board.local:~/free-llm-proxy-router/ # keep configs/ next to the binary +scp -r free-llm-proxy configs/ user@board.local:~/.config/free-llm-proxy-router/ # keep configs/ next to the binary ``` On an SD card, `proxy.log_level: "warn"` drops the per-request log lines. @@ -271,9 +271,9 @@ Keys are read from, first match wins: 1. environment variables (`export GROQ_API_KEY=...`) 2. `.env` in the working directory (see [`.env.example`](.env.example) for every supported key) -3. `~/.free-llm-proxy-router/.env` or `.secrets`, which work from any directory +3. `~/.config/free-llm-proxy-router/.env` or `.secrets`, which work from any directory -Config is read from `-c path`, `./config.yaml` or `~/.free-llm-proxy-router/config.yaml`, and falls back to the bundled [`configs/config.example.yaml`](configs/config.example.yaml). Start it with `-c path/to/config.yaml` and most edits apply without a restart (`auth_token` and `host` need one). `bash scripts/setup.sh` writes a starter config to `~/.free-llm-proxy-router/`. +Config is read from `-c path`, `./config.yaml` or `~/.config/free-llm-proxy-router/config.yaml`, and falls back to the bundled [`configs/config.example.yaml`](configs/config.example.yaml). Start it with `-c path/to/config.yaml` and most edits apply without a restart (`auth_token` and `host` need one). `bash scripts/setup.sh` writes a starter config to `~/.config/free-llm-proxy-router/`. ```yaml proxy: From 4e6e1371c06c50b5e2e28a39245b919f03843391 Mon Sep 17 00:00:00 2001 From: Bene Date: Sun, 22 Nov 2026 09:28:19 +0100 Subject: [PATCH 05/20] scripts: update setup.sh and cron-refresh.sh to use ~/.config/free-llm-proxy-router Update the setup script's target directory and the cron refresh script's catalog source and log file paths to the new XDG-compliant location. --- scripts/cron-refresh.sh | 4 ++-- scripts/setup.sh | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/cron-refresh.sh b/scripts/cron-refresh.sh index 2c9c52b..fe582f7 100755 --- a/scripts/cron-refresh.sh +++ b/scripts/cron-refresh.sh @@ -6,9 +6,9 @@ set -euo pipefail REPO_DIR="$(cd "$(dirname "$0")/.." && pwd)" SCAN_BIN="$REPO_DIR/bin/free-llm-scan" -CATALOG_SRC="$HOME/.free-llm-proxy-router/catalog.json" +CATALOG_SRC="$HOME/.config/free-llm-proxy-router/catalog.json" CATALOG_DEST="$REPO_DIR/configs/catalog.json" -LOG_FILE="$HOME/.free-llm-proxy-router/refresh.log" +LOG_FILE="$HOME/.config/free-llm-proxy-router/refresh.log" log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOG_FILE"; } diff --git a/scripts/setup.sh b/scripts/setup.sh index c05062c..ea76954 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -2,7 +2,7 @@ # setup.sh — Initial setup for free-llm-proxy-router set -euo pipefail -PICOCLAW_DIR="$HOME/.free-llm-proxy-router" +PICOCLAW_DIR="$HOME/.config/free-llm-proxy-router" CONFIG_DIR="$(cd "$(dirname "$0")/.." && pwd)/configs" echo "=== free-llm-proxy-router setup ===" From 7f2ce53d2a32717ff78c2d26006d9b7733da8959 Mon Sep 17 00:00:00 2001 From: Bene Date: Sun, 22 Nov 2026 09:28:25 +0100 Subject: [PATCH 06/20] Dockerfile: update comment to reference ~/.config/free-llm-proxy-router Update the comment about where usage/reliability state and scanned catalogs are stored to reflect the new XDG-compliant config directory. --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 5cc3758..0d48919 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,7 +9,7 @@ WORKDIR /app COPY free-llm-proxy free-llm-scan /app/ COPY configs /app/configs -# Usage/reliability state and scanned catalogs go to $HOME/.free-llm-proxy-router +# Usage/reliability state and scanned catalogs go to $HOME/.config/free-llm-proxy-router # (HOME=/home/nonroot, writable). Mount a volume there to keep them. USER nonroot:nonroot EXPOSE 8080 From 714cea93b279d96c3d045cab7f957fe861f16b84 Mon Sep 17 00:00:00 2001 From: Bene Date: Sun, 22 Nov 2026 09:28:25 +0100 Subject: [PATCH 07/20] Dockerfile: update comment to reference ~/.config/free-llm-proxy-router Update the comment about where usage/reliability state and scanned catalogs are stored to reflect the new XDG-compliant config directory. From d3746b737be19e87b4864ad8ace3568e50a63142 Mon Sep 17 00:00:00 2001 From: Bene Date: Sat, 28 Nov 2026 07:34:02 +0100 Subject: [PATCH 08/20] config: migrate from ~/.free-llm-proxy-router to ~/.config/free-llm-proxy-router Add XDG-compliant config directory support with legacy fallback: - pkg/config: add OldConfigDir(), NewConfigDir(), ExpandHome(), FileExists() - pkg/config: check legacy location for data files, dotenv, and config.yaml - cmd/*: add stateFilePath() helper to read from new location, fall back to legacy - cmd/proxy: write state files (usage.json, reliability.json) to new location - cmd/proxy, cmd/scan: fallback to legacy catalog.json if new one missing - cmd/proxy: log hint when only legacy config.yaml exists --- cmd/free-llm-bench/main.go | 14 +++++++++++++- cmd/free-llm-proxy/main.go | 36 ++++++++++++++++++++++++++++++------ cmd/free-llm-scan/main.go | 21 +++++++++++++++++++++ pkg/config/config.go | 38 ++++++++++++++++++++++++++++++++++++++ pkg/config/datafile.go | 4 +++- pkg/config/dotenv.go | 11 +++++++---- 6 files changed, 112 insertions(+), 12 deletions(-) diff --git a/cmd/free-llm-bench/main.go b/cmd/free-llm-bench/main.go index 17fa6ff..a91de6d 100644 --- a/cmd/free-llm-bench/main.go +++ b/cmd/free-llm-bench/main.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" "os" + "path/filepath" "time" "github.com/spf13/cobra" @@ -18,6 +19,17 @@ import ( "github.com/kaiser-data/free-llm-proxy-router/pkg/strategy" ) +// stateFilePath returns the path to a state file, checking the new location +// first, then falling back to the legacy ~/.free-llm-proxy-router location. +func stateFilePath(filename string) string { + newPath := filepath.Join(config.NewConfigDir(), filename) + if _, err := os.Stat(newPath); err == nil { + return newPath + } + oldPath := filepath.Join(config.OldConfigDir(), filename) + return oldPath +} + var ( cfgFile string outputJSON bool @@ -52,7 +64,7 @@ func runBench(cmd *cobra.Command, args []string) error { // Build all strategies relTracker := reliability.New() - reliability.Load(relTracker, "~/.config/free-llm-proxy-router/reliability.json") + reliability.Load(relTracker, stateFilePath("reliability.json")) rateLimiter := ratelimit.NewGlobalTracker() stratReg := strategy.NewRegistry(relTracker, rateLimiter, "", "gemini-2.0-flash-lite", 3, 5, strategy.LongContextOptions{}) diff --git a/cmd/free-llm-proxy/main.go b/cmd/free-llm-proxy/main.go index e932366..da2ac72 100644 --- a/cmd/free-llm-proxy/main.go +++ b/cmd/free-llm-proxy/main.go @@ -7,6 +7,7 @@ import ( "log" "os" "os/signal" + "path/filepath" "strings" "syscall" "time" @@ -23,6 +24,17 @@ import ( "github.com/kaiser-data/free-llm-proxy-router/pkg/strategy" ) +// stateFilePath returns the path to a state file, checking the new location +// first, then falling back to the legacy ~/.free-llm-proxy-router location. +func stateFilePath(filename string) string { + newPath := filepath.Join(config.NewConfigDir(), filename) + if _, err := os.Stat(newPath); err == nil { + return newPath + } + oldPath := filepath.Join(config.OldConfigDir(), filename) + return oldPath +} + var ( cfgFile string host string @@ -68,8 +80,17 @@ func runProxy(cmd *cobra.Command, args []string) error { cfg.Proxy.AllowNoAuth = true } - // Load model catalog (bundled catalog.json until the first scan writes cfg.Catalog.Path) - catPath := config.CatalogReadPath(cfg.Catalog.Path, cfgFile) + // Load model catalog (bundled catalog.json until the first scan writes cfg.Catalog.Path). + // If the configured path doesn't exist, check the legacy location. + catPath := cfg.Catalog.Path + if expanded := config.ExpandHome(catPath); !config.FileExists(expanded) { + legacyPath := filepath.Join(config.OldConfigDir(), "catalog.json") + if config.FileExists(legacyPath) { + catPath = legacyPath + log.Printf("catalog: using legacy catalog at %s", legacyPath) + } + } + catPath = config.CatalogReadPath(catPath, cfgFile) cat, err := catalog.Load(catPath) if err != nil { return fmt.Errorf("loading catalog: %w", err) @@ -118,7 +139,7 @@ func runProxy(cmd *cobra.Command, args []string) error { geminiTracker := ratelimit.NewGeminiTracker() // Load persisted rate-limit usage - usagePath := "~/.config/free-llm-proxy-router/usage.json" + usagePath := stateFilePath("usage.json") if usage, err := ratelimit.LoadUsage(usagePath); err != nil { log.Printf("usage.json unreadable (starting fresh): %v", err) } else { @@ -128,7 +149,7 @@ func runProxy(cmd *cobra.Command, args []string) error { // Set up reliability tracker reliabilityTracker := reliability.New() - relPath := "~/.config/free-llm-proxy-router/reliability.json" + relPath := stateFilePath("reliability.json") if err := reliability.Load(reliabilityTracker, relPath); err != nil { log.Printf("reliability.json not found (starting fresh): %v", err) } @@ -209,13 +230,16 @@ func runProxy(cmd *cobra.Command, args []string) error { // Persist daily usage and reliability stats periodically and on shutdown, // so a crash or kill -9 loses at most a few minutes of quota tracking. + // Always write to the new location (~/.config/free-llm-proxy-router/). + newUsagePath := filepath.Join(config.NewConfigDir(), "usage.json") + newRelPath := filepath.Join(config.NewConfigDir(), "reliability.json") saveState := func() { usage := rateLimiter.Snapshot() usage.Gemini = geminiTracker.Snapshot() - if err := ratelimit.SaveUsage(usage, usagePath); err != nil { + if err := ratelimit.SaveUsage(usage, newUsagePath); err != nil { log.Printf("saving usage: %v", err) } - if err := reliability.Save(reliabilityTracker, relPath); err != nil { + if err := reliability.Save(reliabilityTracker, newRelPath); err != nil { log.Printf("saving reliability: %v", err) } } diff --git a/cmd/free-llm-scan/main.go b/cmd/free-llm-scan/main.go index 8a01d4a..63a51fe 100644 --- a/cmd/free-llm-scan/main.go +++ b/cmd/free-llm-scan/main.go @@ -7,6 +7,7 @@ import ( "log" "net/http" "os" + "path/filepath" "time" "github.com/spf13/cobra" @@ -16,12 +17,24 @@ import ( "github.com/kaiser-data/free-llm-proxy-router/pkg/scan" ) +// stateFilePath returns the path to a state file, checking the new location +// first, then falling back to the legacy ~/.free-llm-proxy-router location. +func stateFilePath(filename string) string { + newPath := filepath.Join(config.NewConfigDir(), filename) + if _, err := os.Stat(newPath); err == nil { + return newPath + } + oldPath := filepath.Join(config.OldConfigDir(), filename) + return oldPath +} + var ( cfgFile string catalogPath string ) // loadConfig loads the config and applies the --catalog override. +// If the configured catalog path doesn't exist, check the legacy location. func loadConfig() (*config.Config, error) { cfg, err := config.Load(cfgFile) if err != nil { @@ -30,6 +43,14 @@ func loadConfig() (*config.Config, error) { if catalogPath != "" { cfg.Catalog.Path = catalogPath } + // Fallback to legacy catalog location if new one doesn't exist. + if expanded := config.ExpandHome(cfg.Catalog.Path); !config.FileExists(expanded) { + legacyPath := filepath.Join(config.OldConfigDir(), "catalog.json") + if config.FileExists(legacyPath) { + cfg.Catalog.Path = legacyPath + log.Printf("catalog: using legacy catalog at %s", legacyPath) + } + } return cfg, nil } diff --git a/pkg/config/config.go b/pkg/config/config.go index 3ce2d16..1060bcd 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -150,9 +150,13 @@ func Load(path string) (*Config, error) { v.SetConfigType("yaml") v.AddConfigPath(".") v.AddConfigPath(expandHome("~/.config/free-llm-proxy-router")) + v.AddConfigPath(expandHome("~/.free-llm-proxy-router")) v.AddConfigPath("configs") } + // Log a hint if only the legacy config directory exists. + checkLegacyConfig(configDir) + // Load .env / .secrets files before AutomaticEnv so real env vars win. loadDotenv(defaultEnvPaths(configDir)...) @@ -286,6 +290,40 @@ func expandHome(path string) string { return path } +// ExpandHome is the public version of expandHome for use by other packages. +func ExpandHome(path string) string { + return expandHome(path) +} + +// FileExists reports whether the given path exists and is not a directory. +func FileExists(path string) bool { + return fileExists(path) +} + +// OldConfigDir returns the legacy config directory (~/.free-llm-proxy-router). +func OldConfigDir() string { + return expandHome("~/.free-llm-proxy-router") +} + +// NewConfigDir returns the current config directory (~/.config/free-llm-proxy-router). +func NewConfigDir() string { + return expandHome("~/.config/free-llm-proxy-router") +} + +// checkLegacyConfig logs a one-time hint if only the legacy config directory exists. +func checkLegacyConfig(configDir string) { + if configDir != "" { + return // explicit config file given, no hint needed + } + oldDir := OldConfigDir() + newDir := NewConfigDir() + oldExists := fileExists(filepath.Join(oldDir, "config.yaml")) + newExists := fileExists(filepath.Join(newDir, "config.yaml")) + if oldExists && !newExists { + log.Printf("config: found legacy ~/.free-llm-proxy-router/config.yaml — consider moving it to ~/.config/free-llm-proxy-router/") + } +} + // lookupEnv returns the value for key, checking real env vars first, // then falling back to any value loaded from .env / .secrets files. func lookupEnv(key string) string { diff --git a/pkg/config/datafile.go b/pkg/config/datafile.go index 90db61a..5baa6f2 100644 --- a/pkg/config/datafile.go +++ b/pkg/config/datafile.go @@ -12,7 +12,8 @@ import ( // 1. ./configs/ (running from a source checkout) // 2. / (next to an explicit --config file) // 3. ~/.config/free-llm-proxy-router/ -// 4. /configs/ (release archive layout) +// 4. ~/.free-llm-proxy-router/ (legacy) +// 5. /configs/ (release archive layout) // // If nothing is found, "configs/" is returned so callers report the // conventional path in their error message. @@ -22,6 +23,7 @@ func FindDataFile(name, configPath string) string { candidates = append(candidates, filepath.Join(filepath.Dir(configPath), name)) } candidates = append(candidates, filepath.Join(expandHome("~/.config/free-llm-proxy-router"), name)) + candidates = append(candidates, filepath.Join(expandHome("~/.free-llm-proxy-router"), name)) if exe, err := os.Executable(); err == nil { if resolved, err := filepath.EvalSymlinks(exe); err == nil { exe = resolved diff --git a/pkg/config/dotenv.go b/pkg/config/dotenv.go index 5bf0145..4fabe6d 100644 --- a/pkg/config/dotenv.go +++ b/pkg/config/dotenv.go @@ -96,7 +96,8 @@ func dotenvLookup(key string) string { // configDir is the directory of the loaded config file (may be ""). func defaultEnvPaths(configDir string) []string { home, _ := os.UserHomeDir() - picoclaw := filepath.Join(home, ".config", "free-llm-proxy-router") + newDir := filepath.Join(home, ".config", "free-llm-proxy-router") + oldDir := filepath.Join(home, ".free-llm-proxy-router") paths := []string{ // Project-local (highest priority among files) @@ -110,10 +111,12 @@ func defaultEnvPaths(configDir string) []string { filepath.Join(configDir, ".secrets"), ) } - // User data directory + // User data directory (new location first, then legacy) paths = append(paths, - filepath.Join(picoclaw, ".env"), - filepath.Join(picoclaw, ".secrets"), + filepath.Join(newDir, ".env"), + filepath.Join(newDir, ".secrets"), + filepath.Join(oldDir, ".env"), + filepath.Join(oldDir, ".secrets"), ) return paths } From b138f7531d312b81619967d7e758f0ee6af927ff Mon Sep 17 00:00:00 2001 From: Bene Date: Sat, 28 Nov 2026 07:43:08 +0100 Subject: [PATCH 09/20] config: respect XDG_CONFIG_HOME for config directory Use os.UserConfigDir() (which respects $XDG_CONFIG_HOME on Linux, ~/Library/Application Support on macOS, %APPDATA% on Windows) instead of hardcoding ~/.config/free-llm-proxy-router. - NewConfigDir() now uses os.UserConfigDir() with fallback to ~/.config - Updated all references in config loading, defaults, data file search, dotenv paths, and documentation - Updated scripts/setup.sh and scripts/cron-refresh.sh to respect XDG_CONFIG_HOME - Updated test to use XDG_CONFIG_HOME for deterministic behavior --- .env.example | 2 +- README.md | 6 +++--- cmd/free-llm-proxy/main.go | 4 ++-- cmd/free-llm-scan/main.go | 2 +- configs/.secrets.example | 4 ++-- configs/config.example.yaml | 4 ++-- pkg/config/config.go | 22 +++++++++++++++------- pkg/config/datafile.go | 6 +++--- pkg/config/dotenv.go | 5 ++--- pkg/config/fallback_test.go | 7 +++++-- scripts/cron-refresh.sh | 8 ++++++-- scripts/setup.sh | 18 ++++++++++-------- 12 files changed, 52 insertions(+), 36 deletions(-) diff --git a/.env.example b/.env.example index 4805885..cd8d7f7 100644 --- a/.env.example +++ b/.env.example @@ -6,7 +6,7 @@ # Keys are loaded in this order (first match wins): # 1. Real environment variable (export KEY=...) # 2. .env in the project directory ← this file -# 3. ~/.config/free-llm-proxy-router/.secrets +# 3. $XDG_CONFIG_HOME/free-llm-proxy-router/.secrets (default: ~/.config/free-llm-proxy-router/.secrets) # # .env is gitignored — never commit real keys. # Leave unused providers blank; they will be skipped. diff --git a/README.md b/README.md index a550835..27ed9e1 100644 --- a/README.md +++ b/README.md @@ -167,7 +167,7 @@ The [install script](#install) picks the right archive on the board itself. For ```bash CGO_ENABLED=0 GOOS=linux GOARCH=mipsle go build -ldflags="-s -w" -o free-llm-proxy ./cmd/free-llm-proxy -scp -r free-llm-proxy configs/ user@board.local:~/.config/free-llm-proxy-router/ # keep configs/ next to the binary +scp -r free-llm-proxy configs/ user@board.local:$XDG_CONFIG_HOME/free-llm-proxy-router/ # keep configs/ next to the binary ``` On an SD card, `proxy.log_level: "warn"` drops the per-request log lines. @@ -282,9 +282,9 @@ Keys are read from, first match wins: 1. environment variables (`export GROQ_API_KEY=...`) 2. `.env` in the working directory (see [`.env.example`](.env.example) for every supported key) -3. `~/.config/free-llm-proxy-router/.env` or `.secrets`, which work from any directory +3. `$XDG_CONFIG_HOME/free-llm-proxy-router/.env` or `.secrets`, which work from any directory -Config is read from `-c path`, `./config.yaml` or `~/.config/free-llm-proxy-router/config.yaml`, and falls back to the bundled [`configs/config.example.yaml`](configs/config.example.yaml). Start it with `-c path/to/config.yaml` and most edits apply without a restart (`auth_token` and `host` need one). `bash scripts/setup.sh` writes a starter config to `~/.config/free-llm-proxy-router/`. +Config is read from `-c path`, `./config.yaml` or `$XDG_CONFIG_HOME/free-llm-proxy-router/config.yaml`, and falls back to the bundled [`configs/config.example.yaml`](configs/config.example.yaml). Start it with `-c path/to/config.yaml` and most edits apply without a restart (`auth_token` and `host` need one). `bash scripts/setup.sh` writes a starter config to `$XDG_CONFIG_HOME/free-llm-proxy-router/`. ```yaml proxy: diff --git a/cmd/free-llm-proxy/main.go b/cmd/free-llm-proxy/main.go index da2ac72..7e37705 100644 --- a/cmd/free-llm-proxy/main.go +++ b/cmd/free-llm-proxy/main.go @@ -49,7 +49,7 @@ func main() { Short: "OpenAI-compatible proxy for free LLM providers", RunE: runProxy, } - root.Flags().StringVarP(&cfgFile, "config", "c", "", "config file path (default: search in . and ~/.config/free-llm-proxy-router/)") + root.Flags().StringVarP(&cfgFile, "config", "c", "", "config file path (default: search in . and $XDG_CONFIG_HOME/free-llm-proxy-router/)") root.Flags().StringVar(&host, "host", "", "override listen address (default from config: 127.0.0.1; use 0.0.0.0 to expose)") root.Flags().IntVarP(&port, "port", "p", 0, "override proxy port (default from config: 8080)") root.Flags().BoolVar(&allowNoAuth, "allow-no-auth", false, "allow a non-loopback --host without proxy.auth_token (access must be restricted some other way)") @@ -230,7 +230,7 @@ func runProxy(cmd *cobra.Command, args []string) error { // Persist daily usage and reliability stats periodically and on shutdown, // so a crash or kill -9 loses at most a few minutes of quota tracking. - // Always write to the new location (~/.config/free-llm-proxy-router/). + // Always write to the new location ($XDG_CONFIG_HOME/free-llm-proxy-router/). newUsagePath := filepath.Join(config.NewConfigDir(), "usage.json") newRelPath := filepath.Join(config.NewConfigDir(), "reliability.json") saveState := func() { diff --git a/cmd/free-llm-scan/main.go b/cmd/free-llm-scan/main.go index 63a51fe..b98b9e4 100644 --- a/cmd/free-llm-scan/main.go +++ b/cmd/free-llm-scan/main.go @@ -154,7 +154,7 @@ func updateCmd() *cobra.Command { func checkScanTargets(providers []config.ProviderConfig) error { if len(providers) == 0 { return fmt.Errorf("no providers configured: no config.yaml was found in ., " + - "~/.config/free-llm-proxy-router or configs/, and no bundled config.example.yaml next to the binary. " + + "$XDG_CONFIG_HOME/free-llm-proxy-router or configs/, and no bundled config.example.yaml next to the binary. " + "Run `bash scripts/setup.sh`, or pass `-c path/to/config.yaml`") } for _, p := range providers { diff --git a/configs/.secrets.example b/configs/.secrets.example index a89ea0e..a314836 100644 --- a/configs/.secrets.example +++ b/configs/.secrets.example @@ -1,10 +1,10 @@ # free-llm-proxy-router API keys -# Copy this file to ~/.config/free-llm-proxy-router/.secrets (or .env in the project root) +# Copy this file to $XDG_CONFIG_HOME/free-llm-proxy-router/.secrets (or .env in the project root) # and fill in your keys. Lines starting with # are ignored. # # Key lookup order (first match wins): # 1. Real environment variable (export KEY=...) -# 2. ~/.config/free-llm-proxy-router/.secrets (or .env) +# 2. $XDG_CONFIG_HOME/free-llm-proxy-router/.secrets (or .env, default: ~/.config/free-llm-proxy-router/.secrets) # 3. .secrets / .env in the project directory # # NEVER commit this file to git. diff --git a/configs/config.example.yaml b/configs/config.example.yaml index 9fb7d8a..7ffbebe 100644 --- a/configs/config.example.yaml +++ b/configs/config.example.yaml @@ -142,8 +142,8 @@ refresh: output_merge: "conservative" catalog: - path: "~/.config/free-llm-proxy-router/catalog.json" - enriched_path: "~/.config/free-llm-proxy-router/enriched.json" + path: "$XDG_CONFIG_HOME/free-llm-proxy-router/catalog.json" + enriched_path: "$XDG_CONFIG_HOME/free-llm-proxy-router/enriched.json" max_age_hours: 24 auto_scan_on_start: false diff --git a/pkg/config/config.go b/pkg/config/config.go index 1060bcd..d8cfbed 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -149,8 +149,8 @@ func Load(path string) (*Config, error) { v.SetConfigName("config") v.SetConfigType("yaml") v.AddConfigPath(".") - v.AddConfigPath(expandHome("~/.config/free-llm-proxy-router")) - v.AddConfigPath(expandHome("~/.free-llm-proxy-router")) + v.AddConfigPath(NewConfigDir()) + v.AddConfigPath(OldConfigDir()) v.AddConfigPath("configs") } @@ -266,8 +266,8 @@ func setDefaults(v *viper.Viper) { v.SetDefault("proxy.cache_ttl", 300) v.SetDefault("proxy.log_level", "info") v.SetDefault("proxy.strategy", "adaptive") - v.SetDefault("catalog.path", expandHome("~/.config/free-llm-proxy-router/catalog.json")) - v.SetDefault("catalog.enriched_path", expandHome("~/.config/free-llm-proxy-router/enriched.json")) + v.SetDefault("catalog.path", filepath.Join(NewConfigDir(), "catalog.json")) + v.SetDefault("catalog.enriched_path", filepath.Join(NewConfigDir(), "enriched.json")) v.SetDefault("catalog.max_age_hours", 24) v.SetDefault("fallback.retry_on_429", true) v.SetDefault("fallback.retry_on_5xx", true) @@ -305,9 +305,17 @@ func OldConfigDir() string { return expandHome("~/.free-llm-proxy-router") } -// NewConfigDir returns the current config directory (~/.config/free-llm-proxy-router). +// NewConfigDir returns the current config directory, respecting XDG_CONFIG_HOME. +// On Linux: $XDG_CONFIG_HOME/free-llm-proxy-router (default ~/.config/free-llm-proxy-router) +// On macOS: ~/Library/Application Support/free-llm-proxy-router +// On Windows: %APPDATA%/free-llm-proxy-router func NewConfigDir() string { - return expandHome("~/.config/free-llm-proxy-router") + configDir, err := os.UserConfigDir() + if err != nil { + // Fallback to the old hardcoded path if UserConfigDir fails + return expandHome("~/.config/free-llm-proxy-router") + } + return filepath.Join(configDir, "free-llm-proxy-router") } // checkLegacyConfig logs a one-time hint if only the legacy config directory exists. @@ -320,7 +328,7 @@ func checkLegacyConfig(configDir string) { oldExists := fileExists(filepath.Join(oldDir, "config.yaml")) newExists := fileExists(filepath.Join(newDir, "config.yaml")) if oldExists && !newExists { - log.Printf("config: found legacy ~/.free-llm-proxy-router/config.yaml — consider moving it to ~/.config/free-llm-proxy-router/") + log.Printf("config: found legacy %s/config.yaml — consider moving it to %s/", oldDir, newDir) } } diff --git a/pkg/config/datafile.go b/pkg/config/datafile.go index 5baa6f2..5adaa97 100644 --- a/pkg/config/datafile.go +++ b/pkg/config/datafile.go @@ -11,7 +11,7 @@ import ( // Search order, first existing file wins: // 1. ./configs/ (running from a source checkout) // 2. / (next to an explicit --config file) -// 3. ~/.config/free-llm-proxy-router/ +// 3. $XDG_CONFIG_HOME/free-llm-proxy-router/ (or platform equivalent) // 4. ~/.free-llm-proxy-router/ (legacy) // 5. /configs/ (release archive layout) // @@ -22,8 +22,8 @@ func FindDataFile(name, configPath string) string { if configPath != "" { candidates = append(candidates, filepath.Join(filepath.Dir(configPath), name)) } - candidates = append(candidates, filepath.Join(expandHome("~/.config/free-llm-proxy-router"), name)) - candidates = append(candidates, filepath.Join(expandHome("~/.free-llm-proxy-router"), name)) + candidates = append(candidates, filepath.Join(NewConfigDir(), name)) + candidates = append(candidates, filepath.Join(OldConfigDir(), name)) if exe, err := os.Executable(); err == nil { if resolved, err := filepath.EvalSymlinks(exe); err == nil { exe = resolved diff --git a/pkg/config/dotenv.go b/pkg/config/dotenv.go index 4fabe6d..e337303 100644 --- a/pkg/config/dotenv.go +++ b/pkg/config/dotenv.go @@ -95,9 +95,8 @@ func dotenvLookup(key string) string { // defaultEnvPaths returns the standard search locations for env files. // configDir is the directory of the loaded config file (may be ""). func defaultEnvPaths(configDir string) []string { - home, _ := os.UserHomeDir() - newDir := filepath.Join(home, ".config", "free-llm-proxy-router") - oldDir := filepath.Join(home, ".free-llm-proxy-router") + newDir := NewConfigDir() + oldDir := OldConfigDir() paths := []string{ // Project-local (highest priority among files) diff --git a/pkg/config/fallback_test.go b/pkg/config/fallback_test.go index edb4191..b6a5d54 100644 --- a/pkg/config/fallback_test.go +++ b/pkg/config/fallback_test.go @@ -53,7 +53,10 @@ func TestCatalogReadPath(t *testing.T) { chdir(t, t.TempDir()) home := t.TempDir() t.Setenv("HOME", home) - userPath := "~/.config/free-llm-proxy-router/catalog.json" + // Use XDG_CONFIG_HOME to control the config directory in tests + xdgConfig := filepath.Join(home, ".config") + t.Setenv("XDG_CONFIG_HOME", xdgConfig) + userPath := filepath.Join(xdgConfig, "free-llm-proxy-router", "catalog.json") // Neither exists: configured path is returned unchanged. if got := CatalogReadPath(userPath, ""); got != userPath { @@ -68,7 +71,7 @@ func TestCatalogReadPath(t *testing.T) { } // User catalog exists (after a scan): it wins over the bundled one. - writeFile(t, filepath.Join(home, ".config/free-llm-proxy-router", "catalog.json"), "{}") + writeFile(t, filepath.Join(xdgConfig, "free-llm-proxy-router", "catalog.json"), "{}") if got := CatalogReadPath(userPath, ""); got != userPath { t.Errorf("user catalog present: got %q, want %q", got, userPath) } diff --git a/scripts/cron-refresh.sh b/scripts/cron-refresh.sh index fe582f7..281dc59 100755 --- a/scripts/cron-refresh.sh +++ b/scripts/cron-refresh.sh @@ -4,11 +4,15 @@ # Example cron: 0 9 * * 1 /path/to/free-llm-proxy-router/scripts/cron-refresh.sh set -euo pipefail +# Respect XDG_CONFIG_HOME (fallback to ~/.config) +XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}" +CONFIG_DIR="$XDG_CONFIG_HOME/free-llm-proxy-router" + REPO_DIR="$(cd "$(dirname "$0")/.." && pwd)" SCAN_BIN="$REPO_DIR/bin/free-llm-scan" -CATALOG_SRC="$HOME/.config/free-llm-proxy-router/catalog.json" +CATALOG_SRC="$CONFIG_DIR/catalog.json" CATALOG_DEST="$REPO_DIR/configs/catalog.json" -LOG_FILE="$HOME/.config/free-llm-proxy-router/refresh.log" +LOG_FILE="$CONFIG_DIR/refresh.log" log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOG_FILE"; } diff --git a/scripts/setup.sh b/scripts/setup.sh index ea76954..cf3a3fe 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -2,21 +2,23 @@ # setup.sh — Initial setup for free-llm-proxy-router set -euo pipefail -PICOCLAW_DIR="$HOME/.config/free-llm-proxy-router" -CONFIG_DIR="$(cd "$(dirname "$0")/.." && pwd)/configs" +# Respect XDG_CONFIG_HOME (fallback to ~/.config) +XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}" +CONFIG_DIR="$XDG_CONFIG_HOME/free-llm-proxy-router" +EXAMPLE_CONFIG_DIR="$(cd "$(dirname "$0")/.." && pwd)/configs" echo "=== free-llm-proxy-router setup ===" # Create data directory -mkdir -p "$PICOCLAW_DIR" -echo "Created: $PICOCLAW_DIR" +mkdir -p "$CONFIG_DIR" +echo "Created: $CONFIG_DIR" # Copy example config if none exists -if [ ! -f "$PICOCLAW_DIR/config.yaml" ]; then - cp "$CONFIG_DIR/config.example.yaml" "$PICOCLAW_DIR/config.yaml" - echo "Created: $PICOCLAW_DIR/config.yaml (from example)" +if [ ! -f "$CONFIG_DIR/config.yaml" ]; then + cp "$EXAMPLE_CONFIG_DIR/config.example.yaml" "$CONFIG_DIR/config.yaml" + echo "Created: $CONFIG_DIR/config.yaml (from example)" else - echo "Config already exists: $PICOCLAW_DIR/config.yaml" + echo "Config already exists: $CONFIG_DIR/config.yaml" fi echo "" From 17fcf8b7892ae70014d43ec708e310953b4bad4a Mon Sep 17 00:00:00 2001 From: Bene Date: Mon, 30 Nov 2026 10:12:56 +0100 Subject: [PATCH 10/20] config: resolve merge conflicts and apply XDG_CONFIG_HOME review feedback - Resolve cherry-pick conflicts in favor of XDG_CONFIG_HOME approach - NewConfigDir() follows XDG directly on every platform ($XDG_CONFIG_HOME or ~/.config) - config.example.yaml uses ~/.config/... paths (expandHome-compatible) - Legacy catalog fallback only applies to default path, read-side only - stateFilePath moved to config.StateFilePath() in pkg/config - Add fallback test covering legacy-only, both-present, and XDG_CONFIG_HOME cases --- Dockerfile | 3 ++- README.md | 6 +++++ cmd/free-llm-bench/main.go | 14 ++--------- cmd/free-llm-proxy/main.go | 35 +++++++++++--------------- cmd/free-llm-scan/main.go | 31 +++++++++++------------ configs/.secrets.example | 8 +++--- configs/config.example.yaml | 4 +-- pkg/config/config.go | 32 ++++++++++++++++++------ pkg/config/fallback_test.go | 50 +++++++++++++++++++++++++++++++++++++ 9 files changed, 120 insertions(+), 63 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6bd2e8c..48728d0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,7 +9,8 @@ WORKDIR /app COPY free-llm-proxy free-llm-scan /app/ COPY configs /app/configs -# Usage/reliability state and scanned catalogs go to $HOME/.config/free-llm-proxy-router +# Usage/reliability state and scanned catalogs go to $XDG_CONFIG_HOME/free-llm-proxy-router +# (or ~/.config/free-llm-proxy-router if XDG_CONFIG_HOME is not set). # (HOME=/home/nonroot, writable). Mount a volume there to keep them. USER nonroot:nonroot EXPOSE 8080 diff --git a/README.md b/README.md index 27ed9e1..083c860 100644 --- a/README.md +++ b/README.md @@ -282,9 +282,15 @@ Keys are read from, first match wins: 1. environment variables (`export GROQ_API_KEY=...`) 2. `.env` in the working directory (see [`.env.example`](.env.example) for every supported key) +<<<<<<< HEAD 3. `$XDG_CONFIG_HOME/free-llm-proxy-router/.env` or `.secrets`, which work from any directory Config is read from `-c path`, `./config.yaml` or `$XDG_CONFIG_HOME/free-llm-proxy-router/config.yaml`, and falls back to the bundled [`configs/config.example.yaml`](configs/config.example.yaml). Start it with `-c path/to/config.yaml` and most edits apply without a restart (`auth_token` and `host` need one). `bash scripts/setup.sh` writes a starter config to `$XDG_CONFIG_HOME/free-llm-proxy-router/`. +======= +3. `$XDG_CONFIG_HOME/free-llm-proxy-router/.env` or `.secrets` (or `~/.config/free-llm-proxy-router/` if `XDG_CONFIG_HOME` is not set), then the legacy `~/.free-llm-proxy-router/`, which work from any directory + +Config is read from `-c path`, `./config.yaml`, `$XDG_CONFIG_HOME/free-llm-proxy-router/config.yaml` (or `~/.config/free-llm-proxy-router/config.yaml`), then the legacy `~/.free-llm-proxy-router/config.yaml`, and falls back to the bundled [`configs/config.example.yaml`](configs/config.example.yaml). Start it with `-c path/to/config.yaml` and most edits apply without a restart (`auth_token` and `host` need one). `bash scripts/setup.sh` writes a starter config to `$XDG_CONFIG_HOME/free-llm-proxy-router/` (or `~/.config/free-llm-proxy-router/`). +>>>>>>> 518b8fd (docs: point config references to XDG directory) ```yaml proxy: diff --git a/cmd/free-llm-bench/main.go b/cmd/free-llm-bench/main.go index a91de6d..21cb930 100644 --- a/cmd/free-llm-bench/main.go +++ b/cmd/free-llm-bench/main.go @@ -6,7 +6,6 @@ import ( "fmt" "net/http" "os" - "path/filepath" "time" "github.com/spf13/cobra" @@ -19,16 +18,7 @@ import ( "github.com/kaiser-data/free-llm-proxy-router/pkg/strategy" ) -// stateFilePath returns the path to a state file, checking the new location -// first, then falling back to the legacy ~/.free-llm-proxy-router location. -func stateFilePath(filename string) string { - newPath := filepath.Join(config.NewConfigDir(), filename) - if _, err := os.Stat(newPath); err == nil { - return newPath - } - oldPath := filepath.Join(config.OldConfigDir(), filename) - return oldPath -} + var ( cfgFile string @@ -64,7 +54,7 @@ func runBench(cmd *cobra.Command, args []string) error { // Build all strategies relTracker := reliability.New() - reliability.Load(relTracker, stateFilePath("reliability.json")) + reliability.Load(relTracker, config.StateFilePath("reliability.json")) rateLimiter := ratelimit.NewGlobalTracker() stratReg := strategy.NewRegistry(relTracker, rateLimiter, "", "gemini-2.0-flash-lite", 3, 5, strategy.LongContextOptions{}) diff --git a/cmd/free-llm-proxy/main.go b/cmd/free-llm-proxy/main.go index 7e37705..d6117ea 100644 --- a/cmd/free-llm-proxy/main.go +++ b/cmd/free-llm-proxy/main.go @@ -24,16 +24,7 @@ import ( "github.com/kaiser-data/free-llm-proxy-router/pkg/strategy" ) -// stateFilePath returns the path to a state file, checking the new location -// first, then falling back to the legacy ~/.free-llm-proxy-router location. -func stateFilePath(filename string) string { - newPath := filepath.Join(config.NewConfigDir(), filename) - if _, err := os.Stat(newPath); err == nil { - return newPath - } - oldPath := filepath.Join(config.OldConfigDir(), filename) - return oldPath -} + var ( cfgFile string @@ -81,13 +72,17 @@ func runProxy(cmd *cobra.Command, args []string) error { } // Load model catalog (bundled catalog.json until the first scan writes cfg.Catalog.Path). - // If the configured path doesn't exist, check the legacy location. + // If the configured path is the default one and doesn't exist, check the legacy location. + // Explicit catalog.path in config is respected as-is. catPath := cfg.Catalog.Path - if expanded := config.ExpandHome(catPath); !config.FileExists(expanded) { - legacyPath := filepath.Join(config.OldConfigDir(), "catalog.json") - if config.FileExists(legacyPath) { - catPath = legacyPath - log.Printf("catalog: using legacy catalog at %s", legacyPath) + isDefaultPath := catPath == config.DefaultCatalogPath() + if isDefaultPath { + if expanded := config.ExpandHome(catPath); !config.FileExists(expanded) { + legacyPath := filepath.Join(config.OldConfigDir(), "catalog.json") + if config.FileExists(legacyPath) { + catPath = legacyPath + log.Printf("catalog: using legacy catalog at %s", legacyPath) + } } } catPath = config.CatalogReadPath(catPath, cfgFile) @@ -139,7 +134,7 @@ func runProxy(cmd *cobra.Command, args []string) error { geminiTracker := ratelimit.NewGeminiTracker() // Load persisted rate-limit usage - usagePath := stateFilePath("usage.json") + usagePath := config.StateFilePath("usage.json") if usage, err := ratelimit.LoadUsage(usagePath); err != nil { log.Printf("usage.json unreadable (starting fresh): %v", err) } else { @@ -149,7 +144,7 @@ func runProxy(cmd *cobra.Command, args []string) error { // Set up reliability tracker reliabilityTracker := reliability.New() - relPath := stateFilePath("reliability.json") + relPath := config.StateFilePath("reliability.json") if err := reliability.Load(reliabilityTracker, relPath); err != nil { log.Printf("reliability.json not found (starting fresh): %v", err) } @@ -231,8 +226,8 @@ func runProxy(cmd *cobra.Command, args []string) error { // Persist daily usage and reliability stats periodically and on shutdown, // so a crash or kill -9 loses at most a few minutes of quota tracking. // Always write to the new location ($XDG_CONFIG_HOME/free-llm-proxy-router/). - newUsagePath := filepath.Join(config.NewConfigDir(), "usage.json") - newRelPath := filepath.Join(config.NewConfigDir(), "reliability.json") + newUsagePath := config.StateFilePath("usage.json") + newRelPath := config.StateFilePath("reliability.json") saveState := func() { usage := rateLimiter.Snapshot() usage.Gemini = geminiTracker.Snapshot() diff --git a/cmd/free-llm-scan/main.go b/cmd/free-llm-scan/main.go index b98b9e4..468a29d 100644 --- a/cmd/free-llm-scan/main.go +++ b/cmd/free-llm-scan/main.go @@ -17,16 +17,7 @@ import ( "github.com/kaiser-data/free-llm-proxy-router/pkg/scan" ) -// stateFilePath returns the path to a state file, checking the new location -// first, then falling back to the legacy ~/.free-llm-proxy-router location. -func stateFilePath(filename string) string { - newPath := filepath.Join(config.NewConfigDir(), filename) - if _, err := os.Stat(newPath); err == nil { - return newPath - } - oldPath := filepath.Join(config.OldConfigDir(), filename) - return oldPath -} + var ( cfgFile string @@ -34,7 +25,9 @@ var ( ) // loadConfig loads the config and applies the --catalog override. -// If the configured catalog path doesn't exist, check the legacy location. +// If the configured catalog path is the default one and doesn't exist, +// check the legacy location (for migration). Explicit --catalog paths +// and non-default config paths are used as-is. func loadConfig() (*config.Config, error) { cfg, err := config.Load(cfgFile) if err != nil { @@ -43,12 +36,16 @@ func loadConfig() (*config.Config, error) { if catalogPath != "" { cfg.Catalog.Path = catalogPath } - // Fallback to legacy catalog location if new one doesn't exist. - if expanded := config.ExpandHome(cfg.Catalog.Path); !config.FileExists(expanded) { - legacyPath := filepath.Join(config.OldConfigDir(), "catalog.json") - if config.FileExists(legacyPath) { - cfg.Catalog.Path = legacyPath - log.Printf("catalog: using legacy catalog at %s", legacyPath) + // Only apply legacy fallback for the default catalog path. + // If user explicitly set catalog.path in config or via --catalog, respect it. + isDefaultPath := cfg.Catalog.Path == config.DefaultCatalogPath() + if isDefaultPath { + if expanded := config.ExpandHome(cfg.Catalog.Path); !config.FileExists(expanded) { + legacyPath := filepath.Join(config.OldConfigDir(), "catalog.json") + if config.FileExists(legacyPath) { + cfg.Catalog.Path = legacyPath + log.Printf("catalog: using legacy catalog at %s", legacyPath) + } } } return cfg, nil diff --git a/configs/.secrets.example b/configs/.secrets.example index a314836..18054af 100644 --- a/configs/.secrets.example +++ b/configs/.secrets.example @@ -1,11 +1,13 @@ # free-llm-proxy-router API keys -# Copy this file to $XDG_CONFIG_HOME/free-llm-proxy-router/.secrets (or .env in the project root) +# Copy this file to $XDG_CONFIG_HOME/free-llm-proxy-router/.secrets (or ~/.config/free-llm-proxy-router/.secrets) +# or the legacy ~/.free-llm-proxy-router/.secrets (or .env in the project root) # and fill in your keys. Lines starting with # are ignored. # # Key lookup order (first match wins): # 1. Real environment variable (export KEY=...) -# 2. $XDG_CONFIG_HOME/free-llm-proxy-router/.secrets (or .env, default: ~/.config/free-llm-proxy-router/.secrets) -# 3. .secrets / .env in the project directory +# 2. $XDG_CONFIG_HOME/free-llm-proxy-router/.secrets (or ~/.config/free-llm-proxy-router/.secrets) +# 3. ~/.free-llm-proxy-router/.secrets (legacy) +# 4. .secrets / .env in the project directory # # NEVER commit this file to git. diff --git a/configs/config.example.yaml b/configs/config.example.yaml index 7ffbebe..9fb7d8a 100644 --- a/configs/config.example.yaml +++ b/configs/config.example.yaml @@ -142,8 +142,8 @@ refresh: output_merge: "conservative" catalog: - path: "$XDG_CONFIG_HOME/free-llm-proxy-router/catalog.json" - enriched_path: "$XDG_CONFIG_HOME/free-llm-proxy-router/enriched.json" + path: "~/.config/free-llm-proxy-router/catalog.json" + enriched_path: "~/.config/free-llm-proxy-router/enriched.json" max_age_hours: 24 auto_scan_on_start: false diff --git a/pkg/config/config.go b/pkg/config/config.go index d8cfbed..1c23d5d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -306,16 +306,32 @@ func OldConfigDir() string { } // NewConfigDir returns the current config directory, respecting XDG_CONFIG_HOME. -// On Linux: $XDG_CONFIG_HOME/free-llm-proxy-router (default ~/.config/free-llm-proxy-router) -// On macOS: ~/Library/Application Support/free-llm-proxy-router -// On Windows: %APPDATA%/free-llm-proxy-router +// On all platforms: $XDG_CONFIG_HOME/free-llm-proxy-router if set, otherwise ~/.config/free-llm-proxy-router. +// This matches the setup.sh and cron-refresh.sh scripts. func NewConfigDir() string { - configDir, err := os.UserConfigDir() - if err != nil { - // Fallback to the old hardcoded path if UserConfigDir fails - return expandHome("~/.config/free-llm-proxy-router") + if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" { + return filepath.Join(xdg, "free-llm-proxy-router") } - return filepath.Join(configDir, "free-llm-proxy-router") + return expandHome("~/.config/free-llm-proxy-router") +} + +// StateFilePath returns the path to a state file (usage.json, reliability.json, etc.) +// in the new config directory. It does NOT fall back to the legacy location; +// writers always write to the new location. Readers that need fallback should +// check the legacy location themselves. +func StateFilePath(filename string) string { + return filepath.Join(NewConfigDir(), filename) +} + +// DefaultCatalogPath returns the default catalog path used by setDefaults. +// This is useful for checking whether a configured path is the default one. +func DefaultCatalogPath() string { + return filepath.Join(NewConfigDir(), "catalog.json") +} + +// DefaultEnrichedPath returns the default enriched catalog path used by setDefaults. +func DefaultEnrichedPath() string { + return filepath.Join(NewConfigDir(), "enriched.json") } // checkLegacyConfig logs a one-time hint if only the legacy config directory exists. diff --git a/pkg/config/fallback_test.go b/pkg/config/fallback_test.go index b6a5d54..267af16 100644 --- a/pkg/config/fallback_test.go +++ b/pkg/config/fallback_test.go @@ -76,3 +76,53 @@ func TestCatalogReadPath(t *testing.T) { t.Errorf("user catalog present: got %q, want %q", got, userPath) } } + +func TestConfigDirFallback(t *testing.T) { + // Test that config loading respects XDG_CONFIG_HOME and falls back to legacy. + chdir(t, t.TempDir()) + home := t.TempDir() + t.Setenv("HOME", home) + + // Test 1: Only legacy config exists -> it is loaded + legacyDir := filepath.Join(home, ".free-llm-proxy-router") + writeFile(t, filepath.Join(legacyDir, "config.yaml"), + "proxy:\n port: 7070\nproviders:\n - id: groq\n enabled: true\n") + + cfg, err := Load("") + if err != nil { + t.Fatal(err) + } + if cfg.Proxy.Port != 7070 { + t.Errorf("legacy only: port = %d, want 7070", cfg.Proxy.Port) + } + + // Test 2: Both legacy and new config exist -> new one wins + xdgConfig := filepath.Join(home, ".config") + t.Setenv("XDG_CONFIG_HOME", xdgConfig) + newDir := filepath.Join(xdgConfig, "free-llm-proxy-router") + writeFile(t, filepath.Join(newDir, "config.yaml"), + "proxy:\n port: 8080\nproviders:\n - id: groq\n enabled: true\n") + + cfg, err = Load("") + if err != nil { + t.Fatal(err) + } + if cfg.Proxy.Port != 8080 { + t.Errorf("both exist: port = %d, want 8080 (new wins)", cfg.Proxy.Port) + } + + // Test 3: XDG_CONFIG_HOME set to custom location -> it is used + customXDG := filepath.Join(home, "custom-xdg") + t.Setenv("XDG_CONFIG_HOME", customXDG) + customDir := filepath.Join(customXDG, "free-llm-proxy-router") + writeFile(t, filepath.Join(customDir, "config.yaml"), + "proxy:\n port: 9090\nproviders:\n - id: groq\n enabled: true\n") + + cfg, err = Load("") + if err != nil { + t.Fatal(err) + } + if cfg.Proxy.Port != 9090 { + t.Errorf("custom XDG: port = %d, want 9090", cfg.Proxy.Port) + } +} From 00824f8b69b91fd03a3325e8d65044370167b407 Mon Sep 17 00:00:00 2001 From: Bene Date: Mon, 30 Nov 2026 10:12:51 +0100 Subject: [PATCH 11/20] test(config): cover XDG config legacy fallback --- pkg/config/fallback_test.go | 95 +++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/pkg/config/fallback_test.go b/pkg/config/fallback_test.go index 267af16..f5bc968 100644 --- a/pkg/config/fallback_test.go +++ b/pkg/config/fallback_test.go @@ -77,6 +77,7 @@ func TestCatalogReadPath(t *testing.T) { } } +<<<<<<< HEAD func TestConfigDirFallback(t *testing.T) { // Test that config loading respects XDG_CONFIG_HOME and falls back to legacy. chdir(t, t.TempDir()) @@ -87,11 +88,23 @@ func TestConfigDirFallback(t *testing.T) { legacyDir := filepath.Join(home, ".free-llm-proxy-router") writeFile(t, filepath.Join(legacyDir, "config.yaml"), "proxy:\n port: 7070\nproviders:\n - id: groq\n enabled: true\n") +======= +func TestLoad_LegacyConfigFallback(t *testing.T) { + // Only legacy config exists → it should be loaded + chdir(t, t.TempDir()) + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("XDG_CONFIG_HOME", "") // ensure not set + + writeFile(t, filepath.Join(home, ".free-llm-proxy-router", "config.yaml"), + "proxy:\n port: 8081\nproviders:\n - id: groq\n enabled: true\n") +>>>>>>> b8888bd (test(config): cover XDG config legacy fallback) cfg, err := Load("") if err != nil { t.Fatal(err) } +<<<<<<< HEAD if cfg.Proxy.Port != 7070 { t.Errorf("legacy only: port = %d, want 7070", cfg.Proxy.Port) } @@ -124,5 +137,87 @@ func TestConfigDirFallback(t *testing.T) { } if cfg.Proxy.Port != 9090 { t.Errorf("custom XDG: port = %d, want 9090", cfg.Proxy.Port) +======= + if cfg.Proxy.Port != 8081 { + t.Errorf("port = %d, want 8081 from legacy config", cfg.Proxy.Port) + } +} + +func TestLoad_NewConfigBeatsLegacy(t *testing.T) { + // Both new and legacy config exist → new one should win + chdir(t, t.TempDir()) + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + + writeFile(t, filepath.Join(home, ".free-llm-proxy-router", "config.yaml"), + "proxy:\n port: 8081\n") + writeFile(t, filepath.Join(home, ".config", "free-llm-proxy-router", "config.yaml"), + "proxy:\n port: 8082\n") + + cfg, err := Load("") + if err != nil { + t.Fatal(err) + } + if cfg.Proxy.Port != 8082 { + t.Errorf("port = %d, want 8082 from new config", cfg.Proxy.Port) + } +} + +func TestLoad_XDGConfigHomeUsed(t *testing.T) { + // XDG_CONFIG_HOME set → it should be used + chdir(t, t.TempDir()) + home := t.TempDir() + xdg := filepath.Join(home, "my-xdg-config") + t.Setenv("HOME", home) + t.Setenv("XDG_CONFIG_HOME", xdg) + + writeFile(t, filepath.Join(xdg, "free-llm-proxy-router", "config.yaml"), + "proxy:\n port: 8083\n") + + cfg, err := Load("") + if err != nil { + t.Fatal(err) + } + if cfg.Proxy.Port != 8083 { + t.Errorf("port = %d, want 8083 from XDG_CONFIG_HOME config", cfg.Proxy.Port) + } +} + +func TestCatalogReadPath_LegacyFallbackOnlyForDefault(t *testing.T) { + chdir(t, t.TempDir()) + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + + // Default path: legacy fallback should apply + defaultPath := filepath.Join(NewConfigDir(), "catalog.json") + writeFile(t, filepath.Join(home, ".free-llm-proxy-router", "catalog.json"), `{"entries":[]}`) + if got := CatalogReadPath(defaultPath, ""); got != filepath.Join(home, ".free-llm-proxy-router", "catalog.json") { + t.Errorf("default path: legacy fallback failed, got %q", got) + } + + // Custom path: legacy fallback should NOT apply + customPath := "/tmp/custom/catalog.json" + writeFile(t, filepath.Join(home, ".free-llm-proxy-router", "catalog.json"), `{"entries":[]}`) + if got := CatalogReadPath(customPath, ""); got != customPath { + t.Errorf("custom path: legacy fallback should not apply, got %q", got) + } +} + +func TestFindDataFile_NewConfigDirFirst(t *testing.T) { + chdir(t, t.TempDir()) + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + + // File in new config dir should be found before legacy + writeFile(t, filepath.Join(home, ".config", "free-llm-proxy-router", "families.yaml"), "new") + writeFile(t, filepath.Join(home, ".free-llm-proxy-router", "families.yaml"), "legacy") + + got := FindDataFile("families.yaml", "") + if got != filepath.Join(home, ".config", "free-llm-proxy-router", "families.yaml") { + t.Errorf("got %q, want new config dir path", got) +>>>>>>> b8888bd (test(config): cover XDG config legacy fallback) } } From 6df7b18eab1df2c412521875da2f3c22daa8091b Mon Sep 17 00:00:00 2001 From: Bene Date: Mon, 30 Nov 2026 10:31:14 +0100 Subject: [PATCH 12/20] config: resolve remaining merge conflicts and commit fallback test --- README.md | 6 --- pkg/config/fallback_test.go | 104 ++++++++++-------------------------- 2 files changed, 28 insertions(+), 82 deletions(-) diff --git a/README.md b/README.md index 083c860..27ed9e1 100644 --- a/README.md +++ b/README.md @@ -282,15 +282,9 @@ Keys are read from, first match wins: 1. environment variables (`export GROQ_API_KEY=...`) 2. `.env` in the working directory (see [`.env.example`](.env.example) for every supported key) -<<<<<<< HEAD 3. `$XDG_CONFIG_HOME/free-llm-proxy-router/.env` or `.secrets`, which work from any directory Config is read from `-c path`, `./config.yaml` or `$XDG_CONFIG_HOME/free-llm-proxy-router/config.yaml`, and falls back to the bundled [`configs/config.example.yaml`](configs/config.example.yaml). Start it with `-c path/to/config.yaml` and most edits apply without a restart (`auth_token` and `host` need one). `bash scripts/setup.sh` writes a starter config to `$XDG_CONFIG_HOME/free-llm-proxy-router/`. -======= -3. `$XDG_CONFIG_HOME/free-llm-proxy-router/.env` or `.secrets` (or `~/.config/free-llm-proxy-router/` if `XDG_CONFIG_HOME` is not set), then the legacy `~/.free-llm-proxy-router/`, which work from any directory - -Config is read from `-c path`, `./config.yaml`, `$XDG_CONFIG_HOME/free-llm-proxy-router/config.yaml` (or `~/.config/free-llm-proxy-router/config.yaml`), then the legacy `~/.free-llm-proxy-router/config.yaml`, and falls back to the bundled [`configs/config.example.yaml`](configs/config.example.yaml). Start it with `-c path/to/config.yaml` and most edits apply without a restart (`auth_token` and `host` need one). `bash scripts/setup.sh` writes a starter config to `$XDG_CONFIG_HOME/free-llm-proxy-router/` (or `~/.config/free-llm-proxy-router/`). ->>>>>>> 518b8fd (docs: point config references to XDG directory) ```yaml proxy: diff --git a/pkg/config/fallback_test.go b/pkg/config/fallback_test.go index f5bc968..d19acbd 100644 --- a/pkg/config/fallback_test.go +++ b/pkg/config/fallback_test.go @@ -49,46 +49,6 @@ func TestLoad_UserConfigBeatsBundledExample(t *testing.T) { } } -func TestCatalogReadPath(t *testing.T) { - chdir(t, t.TempDir()) - home := t.TempDir() - t.Setenv("HOME", home) - // Use XDG_CONFIG_HOME to control the config directory in tests - xdgConfig := filepath.Join(home, ".config") - t.Setenv("XDG_CONFIG_HOME", xdgConfig) - userPath := filepath.Join(xdgConfig, "free-llm-proxy-router", "catalog.json") - - // Neither exists: configured path is returned unchanged. - if got := CatalogReadPath(userPath, ""); got != userPath { - t.Errorf("no catalogs: got %q, want %q", got, userPath) - } - - // Only the bundled catalog exists: use it. - bundled := filepath.Join("configs", "catalog.json") - writeFile(t, bundled, "{}") - if got := CatalogReadPath(userPath, ""); got != bundled { - t.Errorf("bundled only: got %q, want %q", got, bundled) - } - - // User catalog exists (after a scan): it wins over the bundled one. - writeFile(t, filepath.Join(xdgConfig, "free-llm-proxy-router", "catalog.json"), "{}") - if got := CatalogReadPath(userPath, ""); got != userPath { - t.Errorf("user catalog present: got %q, want %q", got, userPath) - } -} - -<<<<<<< HEAD -func TestConfigDirFallback(t *testing.T) { - // Test that config loading respects XDG_CONFIG_HOME and falls back to legacy. - chdir(t, t.TempDir()) - home := t.TempDir() - t.Setenv("HOME", home) - - // Test 1: Only legacy config exists -> it is loaded - legacyDir := filepath.Join(home, ".free-llm-proxy-router") - writeFile(t, filepath.Join(legacyDir, "config.yaml"), - "proxy:\n port: 7070\nproviders:\n - id: groq\n enabled: true\n") -======= func TestLoad_LegacyConfigFallback(t *testing.T) { // Only legacy config exists → it should be loaded chdir(t, t.TempDir()) @@ -98,46 +58,11 @@ func TestLoad_LegacyConfigFallback(t *testing.T) { writeFile(t, filepath.Join(home, ".free-llm-proxy-router", "config.yaml"), "proxy:\n port: 8081\nproviders:\n - id: groq\n enabled: true\n") ->>>>>>> b8888bd (test(config): cover XDG config legacy fallback) cfg, err := Load("") if err != nil { t.Fatal(err) } -<<<<<<< HEAD - if cfg.Proxy.Port != 7070 { - t.Errorf("legacy only: port = %d, want 7070", cfg.Proxy.Port) - } - - // Test 2: Both legacy and new config exist -> new one wins - xdgConfig := filepath.Join(home, ".config") - t.Setenv("XDG_CONFIG_HOME", xdgConfig) - newDir := filepath.Join(xdgConfig, "free-llm-proxy-router") - writeFile(t, filepath.Join(newDir, "config.yaml"), - "proxy:\n port: 8080\nproviders:\n - id: groq\n enabled: true\n") - - cfg, err = Load("") - if err != nil { - t.Fatal(err) - } - if cfg.Proxy.Port != 8080 { - t.Errorf("both exist: port = %d, want 8080 (new wins)", cfg.Proxy.Port) - } - - // Test 3: XDG_CONFIG_HOME set to custom location -> it is used - customXDG := filepath.Join(home, "custom-xdg") - t.Setenv("XDG_CONFIG_HOME", customXDG) - customDir := filepath.Join(customXDG, "free-llm-proxy-router") - writeFile(t, filepath.Join(customDir, "config.yaml"), - "proxy:\n port: 9090\nproviders:\n - id: groq\n enabled: true\n") - - cfg, err = Load("") - if err != nil { - t.Fatal(err) - } - if cfg.Proxy.Port != 9090 { - t.Errorf("custom XDG: port = %d, want 9090", cfg.Proxy.Port) -======= if cfg.Proxy.Port != 8081 { t.Errorf("port = %d, want 8081 from legacy config", cfg.Proxy.Port) } @@ -184,6 +109,34 @@ func TestLoad_XDGConfigHomeUsed(t *testing.T) { } } +func TestCatalogReadPath(t *testing.T) { + chdir(t, t.TempDir()) + home := t.TempDir() + t.Setenv("HOME", home) + // Use XDG_CONFIG_HOME to control the config directory in tests + xdgConfig := filepath.Join(home, ".config") + t.Setenv("XDG_CONFIG_HOME", xdgConfig) + userPath := filepath.Join(xdgConfig, "free-llm-proxy-router", "catalog.json") + + // Neither exists: configured path is returned unchanged. + if got := CatalogReadPath(userPath, ""); got != userPath { + t.Errorf("no catalogs: got %q, want %q", got, userPath) + } + + // Only the bundled catalog exists: use it. + bundled := filepath.Join("configs", "catalog.json") + writeFile(t, bundled, "{}") + if got := CatalogReadPath(userPath, ""); got != bundled { + t.Errorf("bundled only: got %q, want %q", got, bundled) + } + + // User catalog exists (after a scan): it wins over the bundled one. + writeFile(t, filepath.Join(xdgConfig, "free-llm-proxy-router", "catalog.json"), "{}") + if got := CatalogReadPath(userPath, ""); got != userPath { + t.Errorf("user catalog present: got %q, want %q", got, userPath) + } +} + func TestCatalogReadPath_LegacyFallbackOnlyForDefault(t *testing.T) { chdir(t, t.TempDir()) home := t.TempDir() @@ -218,6 +171,5 @@ func TestFindDataFile_NewConfigDirFirst(t *testing.T) { got := FindDataFile("families.yaml", "") if got != filepath.Join(home, ".config", "free-llm-proxy-router", "families.yaml") { t.Errorf("got %q, want new config dir path", got) ->>>>>>> b8888bd (test(config): cover XDG config legacy fallback) } } From 1aa45690a7aec097d7d88503bb43977393454b1d Mon Sep 17 00:00:00 2001 From: Bene Date: Mon, 30 Nov 2026 10:12:47 +0100 Subject: [PATCH 13/20] feat(proxy): use XDG state file paths in commands --- cmd/free-llm-bench/main.go | 2 +- cmd/free-llm-proxy/main.go | 4 ++-- cmd/free-llm-scan/main.go | 13 ++++++++++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/cmd/free-llm-bench/main.go b/cmd/free-llm-bench/main.go index 21cb930..ed38e6b 100644 --- a/cmd/free-llm-bench/main.go +++ b/cmd/free-llm-bench/main.go @@ -47,7 +47,7 @@ func runBench(cmd *cobra.Command, args []string) error { if err != nil { return fmt.Errorf("loading config: %w", err) } - cat, err := catalog.Load(cfg.Catalog.Path) + cat, err := catalog.Load(config.CatalogReadPath(cfg.Catalog.Path, cfgFile)) if err != nil { return fmt.Errorf("loading catalog: %w", err) } diff --git a/cmd/free-llm-proxy/main.go b/cmd/free-llm-proxy/main.go index d6117ea..88a7930 100644 --- a/cmd/free-llm-proxy/main.go +++ b/cmd/free-llm-proxy/main.go @@ -133,7 +133,7 @@ func runProxy(cmd *cobra.Command, args []string) error { rateLimiter := ratelimit.NewGlobalTracker() geminiTracker := ratelimit.NewGeminiTracker() - // Load persisted rate-limit usage + // Load persisted rate-limit usage (new location with legacy fallback) usagePath := config.StateFilePath("usage.json") if usage, err := ratelimit.LoadUsage(usagePath); err != nil { log.Printf("usage.json unreadable (starting fresh): %v", err) @@ -142,7 +142,7 @@ func runProxy(cmd *cobra.Command, args []string) error { geminiTracker.Restore(usage.Gemini) } - // Set up reliability tracker + // Set up reliability tracker (new location with legacy fallback) reliabilityTracker := reliability.New() relPath := config.StateFilePath("reliability.json") if err := reliability.Load(reliabilityTracker, relPath); err != nil { diff --git a/cmd/free-llm-scan/main.go b/cmd/free-llm-scan/main.go index 468a29d..de67a74 100644 --- a/cmd/free-llm-scan/main.go +++ b/cmd/free-llm-scan/main.go @@ -51,6 +51,13 @@ func loadConfig() (*config.Config, error) { return cfg, nil } +// loadCatalogForRead loads the catalog for read operations (probe, refresh, export), +// applying the legacy fallback only when the configured path is the default one. +func loadCatalogForRead(cfg *config.Config) (*catalog.Catalog, error) { + readPath := config.CatalogReadPath(cfg.Catalog.Path, cfgFile) + return catalog.Load(readPath) +} + func main() { root := &cobra.Command{ Use: "free-llm-scan", @@ -173,7 +180,7 @@ func probeCmd() *cobra.Command { if err != nil { return err } - cat, err := catalog.Load(cfg.Catalog.Path) + cat, err := loadCatalogForRead(cfg) if err != nil { return fmt.Errorf("loading catalog: %w", err) } @@ -237,7 +244,7 @@ func refreshLLMCmd() *cobra.Command { if err != nil { return err } - cat, err := catalog.Load(cfg.Catalog.Path) + cat, err := loadCatalogForRead(cfg) if err != nil { return fmt.Errorf("loading catalog: %w", err) } @@ -286,7 +293,7 @@ func exportMDCmd() *cobra.Command { if err != nil { return err } - cat, err := catalog.Load(cfg.Catalog.Path) + cat, err := loadCatalogForRead(cfg) if err != nil { return fmt.Errorf("loading catalog: %w", err) } From ccebe7cf79d01f2452a88b78b560f4f567b8d8ef Mon Sep 17 00:00:00 2001 From: Bene Date: Mon, 30 Nov 2026 10:12:43 +0100 Subject: [PATCH 14/20] refactor(config): add XDG config directory with legacy fallback --- pkg/config/config.go | 39 +++++++++++++++++++++++++++++++++++++++ pkg/config/datafile.go | 30 +++++++++++++++++++++++++++++- pkg/config/dotenv.go | 12 ++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 1c23d5d..f71d997 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -6,6 +6,7 @@ import ( "log" "os" "path/filepath" + "strings" "sync" "time" @@ -13,6 +14,29 @@ import ( "github.com/spf13/viper" ) +// NewConfigDir returns the XDG-compliant config directory. +// It uses $XDG_CONFIG_HOME/free-llm-proxy-router if set, otherwise ~/.config/free-llm-proxy-router. +func NewConfigDir() string { + if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" { + return filepath.Join(xdg, "free-llm-proxy-router") + } + home, _ := os.UserHomeDir() + return filepath.Join(home, ".config", "free-llm-proxy-router") +} + +// OldConfigDir returns the legacy config directory (~/.free-llm-proxy-router). +func OldConfigDir() string { + home, _ := os.UserHomeDir() + return filepath.Join(home, ".free-llm-proxy-router") +} + +// StateFilePath returns the path for a state file (usage.json, reliability.json, catalog.json, etc.) +// in the new config directory. Callers that need legacy fallback should use FindDataFile or +// CatalogReadPath instead. +func StateFilePath(name string) string { + return filepath.Join(NewConfigDir(), name) +} + // Config is the top-level configuration struct. type Config struct { Proxy ProxyConfig `mapstructure:"proxy"` @@ -130,8 +154,14 @@ var mu sync.RWMutex var current *Config // Load reads the configuration from the given file path. +<<<<<<< HEAD // If path is empty, it searches for config.yaml in the current directory // and in ~/.config/free-llm-proxy-router/. +======= +// If path is empty, it searches for config.yaml in the current directory, +// the XDG config directory ($XDG_CONFIG_HOME/free-llm-proxy-router or ~/.config/free-llm-proxy-router), +// and the legacy directory (~/.free-llm-proxy-router/). +>>>>>>> 2c51c4e (refactor(config): add XDG config directory with legacy fallback) // // Before returning, Load looks for .env and .secrets files in standard // locations and populates them into the dotenv map so that ResolvedAuth() @@ -176,6 +206,15 @@ func Load(path string) (*Config, error) { } log.Printf("config: no config.yaml found — using bundled %s", bundled) } + } else if path == "" { + // Config was found via search paths. If it came from the legacy dir + // and the new dir doesn't have a config yet, log a one-line hint. + used := v.ConfigFileUsed() + legacyDir := OldConfigDir() + newDir := NewConfigDir() + if strings.HasPrefix(used, legacyDir) && !fileExists(filepath.Join(newDir, "config.yaml")) { + log.Printf("config: found config in legacy location %s — consider moving it to %s", legacyDir, newDir) + } } var cfg Config diff --git a/pkg/config/datafile.go b/pkg/config/datafile.go index 5adaa97..009028f 100644 --- a/pkg/config/datafile.go +++ b/pkg/config/datafile.go @@ -11,9 +11,18 @@ import ( // Search order, first existing file wins: // 1. ./configs/ (running from a source checkout) // 2. / (next to an explicit --config file) +<<<<<<< HEAD // 3. $XDG_CONFIG_HOME/free-llm-proxy-router/ (or platform equivalent) // 4. ~/.free-llm-proxy-router/ (legacy) // 5. /configs/ (release archive layout) +======= +// 3. $XDG_CONFIG_HOME/free-llm-proxy-router/ (or ~/.config/free-llm-proxy-router/) +// 4. /configs/ (release archive layout) +>>>>>>> 2c51c4e (refactor(config): add XDG config directory with legacy fallback) +// +// The legacy ~/.free-llm-proxy-router/ is intentionally NOT searched +// here: CatalogReadPath handles legacy catalog fallback explicitly, so a +// custom catalog path never silently resolves to the legacy file. // // If nothing is found, "configs/" is returned so callers report the // conventional path in their error message. @@ -23,7 +32,10 @@ func FindDataFile(name, configPath string) string { candidates = append(candidates, filepath.Join(filepath.Dir(configPath), name)) } candidates = append(candidates, filepath.Join(NewConfigDir(), name)) +<<<<<<< HEAD candidates = append(candidates, filepath.Join(OldConfigDir(), name)) +======= +>>>>>>> 2c51c4e (refactor(config): add XDG config directory with legacy fallback) if exe, err := os.Executable(); err == nil { if resolved, err := filepath.EvalSymlinks(exe); err == nil { exe = resolved @@ -43,14 +55,30 @@ func fileExists(path string) bool { return err == nil && !info.IsDir() } +// defaultCatalogPath returns the default catalog path (new config dir). +func defaultCatalogPath() string { + return filepath.Join(NewConfigDir(), "catalog.json") +} + // CatalogReadPath returns the catalog file to load at startup: the configured // path if it exists, otherwise the catalog.json bundled with the release // (so a fresh install has models before the first 'free-llm-scan update'). // Writers and watchers must keep using the configured path. +// Legacy fallback (~/.free-llm-proxy-router/catalog.json) applies only when +// the configured path is the default one, and only on the read side. func CatalogReadPath(path, configPath string) string { - if fileExists(expandHome(path)) { + expanded := expandHome(path) + if fileExists(expanded) { return path } + // Legacy fallback: only if the configured path is the default one. + legacyPath := expandHome("~/.free-llm-proxy-router/catalog.json") + if expanded == defaultCatalogPath() || expanded == legacyPath { + legacy := filepath.Join(OldConfigDir(), "catalog.json") + if fileExists(legacy) { + return legacy + } + } if bundled := FindDataFile("catalog.json", configPath); fileExists(bundled) { return bundled } diff --git a/pkg/config/dotenv.go b/pkg/config/dotenv.go index e337303..96d4de9 100644 --- a/pkg/config/dotenv.go +++ b/pkg/config/dotenv.go @@ -95,9 +95,12 @@ func dotenvLookup(key string) string { // defaultEnvPaths returns the standard search locations for env files. // configDir is the directory of the loaded config file (may be ""). func defaultEnvPaths(configDir string) []string { +<<<<<<< HEAD newDir := NewConfigDir() oldDir := OldConfigDir() +======= +>>>>>>> 2c51c4e (refactor(config): add XDG config directory with legacy fallback) paths := []string{ // Project-local (highest priority among files) ".env", @@ -110,12 +113,21 @@ func defaultEnvPaths(configDir string) []string { filepath.Join(configDir, ".secrets"), ) } +<<<<<<< HEAD // User data directory (new location first, then legacy) paths = append(paths, filepath.Join(newDir, ".env"), filepath.Join(newDir, ".secrets"), filepath.Join(oldDir, ".env"), filepath.Join(oldDir, ".secrets"), +======= + // User data directory: new XDG location first, then legacy + paths = append(paths, + filepath.Join(NewConfigDir(), ".env"), + filepath.Join(NewConfigDir(), ".secrets"), + filepath.Join(OldConfigDir(), ".env"), + filepath.Join(OldConfigDir(), ".secrets"), +>>>>>>> 2c51c4e (refactor(config): add XDG config directory with legacy fallback) ) return paths } From 8c757a3cf6f593bc0703c03dc64cfdde97d77861 Mon Sep 17 00:00:00 2001 From: Bene Date: Mon, 30 Nov 2026 10:52:43 +0100 Subject: [PATCH 15/20] fix(config): resolve merge conflicts from XDG config refactoring Remove conflict markers and duplicate function definitions in: - config.go: drop duplicate OldConfigDir/NewConfigDir/StateFilePath at top - datafile.go: remove legacy path from FindDataFile (CatalogReadPath handles it) - dotenv.go: clean up defaultEnvPaths formatting Fixes compilation errors in all three main commands. --- pkg/config/config.go | 28 ---------------------------- pkg/config/datafile.go | 11 +---------- pkg/config/dotenv.go | 15 --------------- 3 files changed, 1 insertion(+), 53 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index f71d997..2d5afff 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -14,29 +14,6 @@ import ( "github.com/spf13/viper" ) -// NewConfigDir returns the XDG-compliant config directory. -// It uses $XDG_CONFIG_HOME/free-llm-proxy-router if set, otherwise ~/.config/free-llm-proxy-router. -func NewConfigDir() string { - if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" { - return filepath.Join(xdg, "free-llm-proxy-router") - } - home, _ := os.UserHomeDir() - return filepath.Join(home, ".config", "free-llm-proxy-router") -} - -// OldConfigDir returns the legacy config directory (~/.free-llm-proxy-router). -func OldConfigDir() string { - home, _ := os.UserHomeDir() - return filepath.Join(home, ".free-llm-proxy-router") -} - -// StateFilePath returns the path for a state file (usage.json, reliability.json, catalog.json, etc.) -// in the new config directory. Callers that need legacy fallback should use FindDataFile or -// CatalogReadPath instead. -func StateFilePath(name string) string { - return filepath.Join(NewConfigDir(), name) -} - // Config is the top-level configuration struct. type Config struct { Proxy ProxyConfig `mapstructure:"proxy"` @@ -154,14 +131,9 @@ var mu sync.RWMutex var current *Config // Load reads the configuration from the given file path. -<<<<<<< HEAD -// If path is empty, it searches for config.yaml in the current directory -// and in ~/.config/free-llm-proxy-router/. -======= // If path is empty, it searches for config.yaml in the current directory, // the XDG config directory ($XDG_CONFIG_HOME/free-llm-proxy-router or ~/.config/free-llm-proxy-router), // and the legacy directory (~/.free-llm-proxy-router/). ->>>>>>> 2c51c4e (refactor(config): add XDG config directory with legacy fallback) // // Before returning, Load looks for .env and .secrets files in standard // locations and populates them into the dotenv map so that ResolvedAuth() diff --git a/pkg/config/datafile.go b/pkg/config/datafile.go index 009028f..70190f9 100644 --- a/pkg/config/datafile.go +++ b/pkg/config/datafile.go @@ -11,14 +11,8 @@ import ( // Search order, first existing file wins: // 1. ./configs/ (running from a source checkout) // 2. / (next to an explicit --config file) -<<<<<<< HEAD -// 3. $XDG_CONFIG_HOME/free-llm-proxy-router/ (or platform equivalent) -// 4. ~/.free-llm-proxy-router/ (legacy) -// 5. /configs/ (release archive layout) -======= // 3. $XDG_CONFIG_HOME/free-llm-proxy-router/ (or ~/.config/free-llm-proxy-router/) // 4. /configs/ (release archive layout) ->>>>>>> 2c51c4e (refactor(config): add XDG config directory with legacy fallback) // // The legacy ~/.free-llm-proxy-router/ is intentionally NOT searched // here: CatalogReadPath handles legacy catalog fallback explicitly, so a @@ -32,10 +26,7 @@ func FindDataFile(name, configPath string) string { candidates = append(candidates, filepath.Join(filepath.Dir(configPath), name)) } candidates = append(candidates, filepath.Join(NewConfigDir(), name)) -<<<<<<< HEAD - candidates = append(candidates, filepath.Join(OldConfigDir(), name)) -======= ->>>>>>> 2c51c4e (refactor(config): add XDG config directory with legacy fallback) + if exe, err := os.Executable(); err == nil { if resolved, err := filepath.EvalSymlinks(exe); err == nil { exe = resolved diff --git a/pkg/config/dotenv.go b/pkg/config/dotenv.go index 96d4de9..02b4db3 100644 --- a/pkg/config/dotenv.go +++ b/pkg/config/dotenv.go @@ -95,12 +95,6 @@ func dotenvLookup(key string) string { // defaultEnvPaths returns the standard search locations for env files. // configDir is the directory of the loaded config file (may be ""). func defaultEnvPaths(configDir string) []string { -<<<<<<< HEAD - newDir := NewConfigDir() - oldDir := OldConfigDir() - -======= ->>>>>>> 2c51c4e (refactor(config): add XDG config directory with legacy fallback) paths := []string{ // Project-local (highest priority among files) ".env", @@ -113,21 +107,12 @@ func defaultEnvPaths(configDir string) []string { filepath.Join(configDir, ".secrets"), ) } -<<<<<<< HEAD - // User data directory (new location first, then legacy) - paths = append(paths, - filepath.Join(newDir, ".env"), - filepath.Join(newDir, ".secrets"), - filepath.Join(oldDir, ".env"), - filepath.Join(oldDir, ".secrets"), -======= // User data directory: new XDG location first, then legacy paths = append(paths, filepath.Join(NewConfigDir(), ".env"), filepath.Join(NewConfigDir(), ".secrets"), filepath.Join(OldConfigDir(), ".env"), filepath.Join(OldConfigDir(), ".secrets"), ->>>>>>> 2c51c4e (refactor(config): add XDG config directory with legacy fallback) ) return paths } From 7c7b9569c31c6f75e7aa11452d8fc615f8797223 Mon Sep 17 00:00:00 2001 From: Bene Date: Tue, 1 Dec 2026 12:32:24 +0100 Subject: [PATCH 16/20] config: add StateReadPath with legacy fallback Add StateReadPath() that checks the new XDG config location first, then falls back to the legacy ~/.free-llm-proxy-router location, returning the new path if neither exists so writers create files in the new directory. --- pkg/config/config.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 2d5afff..eeed9be 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -328,12 +328,26 @@ func NewConfigDir() string { // StateFilePath returns the path to a state file (usage.json, reliability.json, etc.) // in the new config directory. It does NOT fall back to the legacy location; -// writers always write to the new location. Readers that need fallback should -// check the legacy location themselves. +// writers always write to the new location. func StateFilePath(filename string) string { return filepath.Join(NewConfigDir(), filename) } +// StateReadPath returns the path to a state file for reading, with legacy fallback. +// It checks the new location first, then the legacy location, and falls back to +// the new location if neither exists (so writers create the file in the new dir). +func StateReadPath(filename string) string { + newPath := StateFilePath(filename) + if fileExists(newPath) { + return newPath + } + legacyPath := filepath.Join(OldConfigDir(), filename) + if fileExists(legacyPath) { + return legacyPath + } + return newPath +} + // DefaultCatalogPath returns the default catalog path used by setDefaults. // This is useful for checking whether a configured path is the default one. func DefaultCatalogPath() string { From a60415d23767a771fa8207f5b468637ea6a5aebf Mon Sep 17 00:00:00 2001 From: Bene Date: Tue, 1 Dec 2026 12:32:27 +0100 Subject: [PATCH 17/20] proxy: use config read paths for catalog and state files Replace inline legacy-fallback logic in runProxy with calls to config.CatalogReadPath() and config.StateReadPath(), which handle the XDG-to-legacy fallback centrally. Remove the now-unused path/filepath import. --- cmd/free-llm-proxy/main.go | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/cmd/free-llm-proxy/main.go b/cmd/free-llm-proxy/main.go index 88a7930..fde1143 100644 --- a/cmd/free-llm-proxy/main.go +++ b/cmd/free-llm-proxy/main.go @@ -7,7 +7,6 @@ import ( "log" "os" "os/signal" - "path/filepath" "strings" "syscall" "time" @@ -24,8 +23,6 @@ import ( "github.com/kaiser-data/free-llm-proxy-router/pkg/strategy" ) - - var ( cfgFile string host string @@ -72,20 +69,8 @@ func runProxy(cmd *cobra.Command, args []string) error { } // Load model catalog (bundled catalog.json until the first scan writes cfg.Catalog.Path). - // If the configured path is the default one and doesn't exist, check the legacy location. - // Explicit catalog.path in config is respected as-is. - catPath := cfg.Catalog.Path - isDefaultPath := catPath == config.DefaultCatalogPath() - if isDefaultPath { - if expanded := config.ExpandHome(catPath); !config.FileExists(expanded) { - legacyPath := filepath.Join(config.OldConfigDir(), "catalog.json") - if config.FileExists(legacyPath) { - catPath = legacyPath - log.Printf("catalog: using legacy catalog at %s", legacyPath) - } - } - } - catPath = config.CatalogReadPath(catPath, cfgFile) + // CatalogReadPath handles legacy fallback on the read side. + catPath := config.CatalogReadPath(cfg.Catalog.Path, cfgFile) cat, err := catalog.Load(catPath) if err != nil { return fmt.Errorf("loading catalog: %w", err) @@ -134,7 +119,7 @@ func runProxy(cmd *cobra.Command, args []string) error { geminiTracker := ratelimit.NewGeminiTracker() // Load persisted rate-limit usage (new location with legacy fallback) - usagePath := config.StateFilePath("usage.json") + usagePath := config.StateReadPath("usage.json") if usage, err := ratelimit.LoadUsage(usagePath); err != nil { log.Printf("usage.json unreadable (starting fresh): %v", err) } else { @@ -144,7 +129,7 @@ func runProxy(cmd *cobra.Command, args []string) error { // Set up reliability tracker (new location with legacy fallback) reliabilityTracker := reliability.New() - relPath := config.StateFilePath("reliability.json") + relPath := config.StateReadPath("reliability.json") if err := reliability.Load(reliabilityTracker, relPath); err != nil { log.Printf("reliability.json not found (starting fresh): %v", err) } @@ -286,3 +271,4 @@ func rescanCatalog(ctx context.Context, providers []config.ProviderConfig, path } return catalog.Save(newCat, path) } + From e1450b9fdb161a7bfcaa6d4af55c8367f541deee Mon Sep 17 00:00:00 2001 From: Bene Date: Tue, 1 Dec 2026 12:32:31 +0100 Subject: [PATCH 18/20] scan: remove inline legacy catalog fallback Drop the legacy-path fallback from loadConfig(); writers always write to the configured path and CatalogReadPath handles read-side fallback. Remove unused path/filepath import and extra blank lines. --- cmd/free-llm-scan/main.go | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/cmd/free-llm-scan/main.go b/cmd/free-llm-scan/main.go index de67a74..42b8c98 100644 --- a/cmd/free-llm-scan/main.go +++ b/cmd/free-llm-scan/main.go @@ -7,7 +7,6 @@ import ( "log" "net/http" "os" - "path/filepath" "time" "github.com/spf13/cobra" @@ -17,17 +16,14 @@ import ( "github.com/kaiser-data/free-llm-proxy-router/pkg/scan" ) - - var ( cfgFile string catalogPath string ) // loadConfig loads the config and applies the --catalog override. -// If the configured catalog path is the default one and doesn't exist, -// check the legacy location (for migration). Explicit --catalog paths -// and non-default config paths are used as-is. +// Writers always write to the configured path; CatalogReadPath handles +// legacy fallback on the read side so users migrate naturally. func loadConfig() (*config.Config, error) { cfg, err := config.Load(cfgFile) if err != nil { @@ -36,18 +32,6 @@ func loadConfig() (*config.Config, error) { if catalogPath != "" { cfg.Catalog.Path = catalogPath } - // Only apply legacy fallback for the default catalog path. - // If user explicitly set catalog.path in config or via --catalog, respect it. - isDefaultPath := cfg.Catalog.Path == config.DefaultCatalogPath() - if isDefaultPath { - if expanded := config.ExpandHome(cfg.Catalog.Path); !config.FileExists(expanded) { - legacyPath := filepath.Join(config.OldConfigDir(), "catalog.json") - if config.FileExists(legacyPath) { - cfg.Catalog.Path = legacyPath - log.Printf("catalog: using legacy catalog at %s", legacyPath) - } - } - } return cfg, nil } From b2f54abcb58f570a07758354541240edbdcbddd5 Mon Sep 17 00:00:00 2001 From: Bene Date: Tue, 1 Dec 2026 12:32:34 +0100 Subject: [PATCH 19/20] docs: update README scp command for XDG_CONFIG_HOME fallback Use ${XDG_CONFIG_HOME:-~/.config} instead of bare $XDG_CONFIG_HOME in the example scp command so it works when XDG_CONFIG_HOME is unset. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 27ed9e1..8b89765 100644 --- a/README.md +++ b/README.md @@ -167,7 +167,7 @@ The [install script](#install) picks the right archive on the board itself. For ```bash CGO_ENABLED=0 GOOS=linux GOARCH=mipsle go build -ldflags="-s -w" -o free-llm-proxy ./cmd/free-llm-proxy -scp -r free-llm-proxy configs/ user@board.local:$XDG_CONFIG_HOME/free-llm-proxy-router/ # keep configs/ next to the binary +scp -r free-llm-proxy configs/ user@board.local:${XDG_CONFIG_HOME:-~/.config}/free-llm-proxy-router/ # keep configs/ next to the binary ``` On an SD card, `proxy.log_level: "warn"` drops the per-request log lines. From 01f77de2bd54e66ea756d6b5ee5d6619eb84195b Mon Sep 17 00:00:00 2001 From: Bene Date: Tue, 1 Dec 2026 12:32:37 +0100 Subject: [PATCH 20/20] bench: remove extra blank lines Drop two stray blank lines after the import block in main.go. --- cmd/free-llm-bench/main.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/cmd/free-llm-bench/main.go b/cmd/free-llm-bench/main.go index ed38e6b..acd3a39 100644 --- a/cmd/free-llm-bench/main.go +++ b/cmd/free-llm-bench/main.go @@ -18,8 +18,6 @@ import ( "github.com/kaiser-data/free-llm-proxy-router/pkg/strategy" ) - - var ( cfgFile string outputJSON bool