refactor: default cache to ~/.cache on all platforms

os.UserCacheDir returns ~/Library/Caches on macOS, which is intended
for bundled desktop apps and hides files from anyone looking under
~/.cache. These are CLI tools — use the XDG convention everywhere so
the cache lives somewhere predictable and cross-platform-consistent.
This commit is contained in:
AJ ONeal 2026-04-20 17:33:31 -06:00
parent 5e6688c2a9
commit c99cd3a2b8
No known key found for this signature in database
2 changed files with 9 additions and 7 deletions

View File

@ -54,7 +54,7 @@ func main() {
fs.StringVar(&cfg.Bind, "serve", "", "bind address for the HTTP API, e.g. :8080") fs.StringVar(&cfg.Bind, "serve", "", "bind address for the HTTP API, e.g. :8080")
fs.StringVar(&cfg.GeoIPConfPath, "geoip-conf", "", "path to GeoIP.conf (default: ./GeoIP.conf or ~/.config/maxmind/GeoIP.conf)") fs.StringVar(&cfg.GeoIPConfPath, "geoip-conf", "", "path to GeoIP.conf (default: ./GeoIP.conf or ~/.config/maxmind/GeoIP.conf)")
fs.StringVar(&cfg.RepoURL, "blocklist-repo", defaultBlocklistRepo, "git URL of the blocklist repo (must match bitwire-it layout)") fs.StringVar(&cfg.RepoURL, "blocklist-repo", defaultBlocklistRepo, "git URL of the blocklist repo (must match bitwire-it layout)")
fs.StringVar(&cfg.CacheDir, "cache-dir", "", "cache parent dir, holds bitwire-it/ and maxmind/ subdirs (default: OS user cache)") fs.StringVar(&cfg.CacheDir, "cache-dir", "", "cache parent dir, holds bitwire-it/ and maxmind/ subdirs (default: ~/.cache)")
fs.StringVar(&cfg.WhitelistPath, "whitelist", "", "path to a file of IPs and/or CIDRs (one per line) that override block decisions") fs.StringVar(&cfg.WhitelistPath, "whitelist", "", "path to a file of IPs and/or CIDRs (one per line) that override block decisions")
fs.Usage = func() { fs.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [flags] <ip> [ip...]\n", os.Args[0]) fmt.Fprintf(os.Stderr, "Usage: %s [flags] <ip> [ip...]\n", os.Args[0])
@ -88,11 +88,11 @@ func main() {
os.Exit(1) os.Exit(1)
} }
if cfg.CacheDir == "" { if cfg.CacheDir == "" {
d, err := os.UserCacheDir() home, err := os.UserHomeDir()
if err != nil { if err != nil {
log.Fatalf("cache-dir: %v", err) log.Fatalf("cache-dir: %v", err)
} }
cfg.CacheDir = d cfg.CacheDir = filepath.Join(home, ".cache")
} }
// GeoIP config discovery: explicit --geoip-conf wins; otherwise check the // GeoIP config discovery: explicit --geoip-conf wins; otherwise check the

View File

@ -33,12 +33,14 @@ func DefaultConfPaths() []string {
return paths return paths
} }
// DefaultCacheDir returns the OS cache directory for MaxMind databases, // DefaultCacheDir returns ~/.cache/maxmind. CLI tools use the XDG
// e.g. ~/.cache/maxmind on Linux or ~/Library/Caches/maxmind on macOS. // convention on all platforms — os.UserCacheDir's macOS default
// (~/Library/Caches) is meant for bundled desktop apps and hides the
// files from anyone looking under ~/.cache.
func DefaultCacheDir() (string, error) { func DefaultCacheDir() (string, error) {
base, err := os.UserCacheDir() home, err := os.UserHomeDir()
if err != nil { if err != nil {
return "", err return "", err
} }
return filepath.Join(base, "maxmind"), nil return filepath.Join(home, ".cache", "maxmind"), nil
} }