From 199d122990587bee5a66517ad7c1e95c63fa35e1 Mon Sep 17 00:00:00 2001 From: makeworld Date: Mon, 22 Jun 2020 11:56:55 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Prefer=20XDG=20vars=20if=20set=20-?= =?UTF-8?q?=20fixes=20#11?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + README.md | 2 +- config/config.go | 19 ++++++++++++++++--- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9bdccf..bd1a8df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Desktop entry file - Option to continue anyway when cert doesn't match TOFU database - Display all `text/*` documents, not just gemini and plain (#12) +- Prefer XDG environment variables if they're set, to specify config dir, etc (#11) ### Changed - Connection timeout is 15 seconds (was 5s) diff --git a/README.md b/README.md index 2eb7be2..5308a92 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ Features in *italics* are in the master branch, but not in the latest release. - [ ] History browser ## Configuration -The config file is written in the intuitive [TOML](https://github.com/toml-lang/toml) file format. See [default-config.toml](./default-config.toml) for details. By default this file is available at `~/.config/amfora/config.toml`. +The config file is written in the intuitive [TOML](https://github.com/toml-lang/toml) file format. See [default-config.toml](./default-config.toml) for details. By default this file is available at `~/.config/amfora/config.toml`, or `$XDG_CONFIG_HOME/amfora/config.toml`, if that variable is set. On Windows, the file is in `%APPDATA%\amfora\config.toml`, which usually expands to `C:\Users\\AppData\Roaming\amfora\config.toml`. diff --git a/config/config.go b/config/config.go index 9ed9abf..99d47a2 100644 --- a/config/config.go +++ b/config/config.go @@ -4,6 +4,7 @@ import ( "os" "path/filepath" "runtime" + "strings" "github.com/makeworld-the-better-one/amfora/cache" homedir "github.com/mitchellh/go-homedir" @@ -37,17 +38,29 @@ func Init() error { configDir = amforaAppData } else { // Unix / POSIX system - configDir = filepath.Join(home, ".config", "amfora") + xdg_config, ok := os.LookupEnv("XDG_CONFIG_HOME") + if ok && strings.TrimSpace(xdg_config) != "" { + configDir = filepath.Join(xdg_config, "amfora") + } else { + // Default to ~/.config/amfora + configDir = filepath.Join(home, ".config", "amfora") + } } configPath = filepath.Join(configDir, "config.toml") // Cache TOFU db directory and file paths - // Windows just stores it in APPDATA along with other stuff if runtime.GOOS == "windows" { + // Windows just stores it in APPDATA along with other stuff tofuDBDir = amforaAppData } else { // XDG cache dir on POSIX systems - tofuDBDir = filepath.Join(home, ".cache", "amfora") + xdg_cache, ok := os.LookupEnv("XDG_CACHE_HOME") + if ok && strings.TrimSpace(xdg_cache) != "" { + tofuDBDir = filepath.Join(xdg_cache, "amfora") + } else { + // Default to ~/.cache/amfora + tofuDBDir = filepath.Join(home, ".cache", "amfora") + } } tofuDBPath = filepath.Join(tofuDBDir, "tofu.toml")