Move config file to XDG config dir

This commit is contained in:
Jan Bader
2026-07-20 20:41:24 +02:00
parent f603bbb8b8
commit 12aa0d4e21
3 changed files with 86 additions and 8 deletions
+10 -3
View File
@@ -18,11 +18,18 @@ A small command-line helper for managing Docspell import folders, archiving file
- A running Docspell instance. - A running Docspell instance.
- A password command, for example [`pass`](https://www.passwordstore.org/), that prints the Docspell password to stdout. - A password command, for example [`pass`](https://www.passwordstore.org/), that prints the Docspell password to stdout.
- FUSE support for the `mount` command, including `fusermount` or `fusermount3` for unmounting. - FUSE support for the `mount` command, including `fusermount` or `fusermount3` for unmounting.
- A configuration file at `~/docspell-cli.json`. - A configuration file at `${XDG_CONFIG_HOME:-~/.config}/docspell-cli/config.json`.
## Configuration ## Configuration
Create `~/docspell-cli.json`. See [docspell-cli-example.json](./docspell-cli-example.json) for a complete example. Create `${XDG_CONFIG_HOME:-~/.config}/docspell-cli/config.json`. See [docspell-cli-example.json](./docspell-cli-example.json) for a complete example.
```bash
mkdir -p "${XDG_CONFIG_HOME:-$HOME/.config}/docspell-cli"
cp docspell-cli-example.json "${XDG_CONFIG_HOME:-$HOME/.config}/docspell-cli/config.json"
```
For compatibility, the legacy `~/docspell-cli.json` file is still read if the XDG config file does not exist, but the tool will print a warning asking you to move it.
```json ```json
{ {
@@ -84,7 +91,7 @@ Uploaded files are not archived immediately. They are re-checked on the next run
## Read-only FUSE mount ## Read-only FUSE mount
The `mount` subcommand exposes Docspell search results as a read-only filesystem. It uses the same `~/docspell-cli.json` file and password command as the import/archive command. The `mount` subcommand exposes Docspell search results as a read-only filesystem. It uses the same XDG config file and password command as the import/archive command.
Set `mountPath` in the config to define the default mount point and `mountQuery` to define the default Docspell query. Set `mountPath` in the config to define the default mount point and `mountQuery` to define the default Docspell query.
+29 -5
View File
@@ -120,18 +120,19 @@ func newCLIParser() (*flaggy.Parser, cliCommands) {
} }
func loadConfigAndPassword() (config, string) { func loadConfigAndPassword() (config, string) {
homeDir, err := os.UserHomeDir() configFile, legacyTarget, err := configFilePath()
if err != nil { if err != nil {
fmt.Println("Error getting user home directory:", err) fmt.Println("Error finding config directory:", err)
os.Exit(1) os.Exit(1)
} }
configFile := filepath.Join(homeDir, "docspell-cli.json")
configData, err := os.ReadFile(configFile) configData, err := os.ReadFile(configFile)
if err != nil { if err != nil {
fmt.Println("Error reading config file:", err) fmt.Printf("Error reading config file %s: %v\n", configFile, err)
os.Exit(1) os.Exit(1)
} }
if legacyTarget != "" {
fmt.Fprintf(os.Stderr, "Warning: using legacy config file %s; move it to %s\n", configFile, legacyTarget)
}
var cfg config var cfg config
if err := json.Unmarshal(configData, &cfg); err != nil { if err := json.Unmarshal(configData, &cfg); err != nil {
@@ -149,6 +150,29 @@ func loadConfigAndPassword() (config, string) {
return cfg, strings.TrimSpace(string(password)) return cfg, strings.TrimSpace(string(password))
} }
func configFilePath() (path string, legacyTarget string, err error) {
configDir, err := os.UserConfigDir()
if err != nil {
return "", "", err
}
xdgPath := filepath.Join(configDir, "docspell-cli", "config.json")
if _, err := os.Stat(xdgPath); err == nil {
return xdgPath, "", nil
} else if !os.IsNotExist(err) {
return xdgPath, "", nil
}
homeDir, err := os.UserHomeDir()
if err != nil {
return xdgPath, "", nil
}
legacyPath := filepath.Join(homeDir, "docspell-cli.json")
if _, err := os.Stat(legacyPath); err == nil {
return legacyPath, xdgPath, nil
}
return xdgPath, "", nil
}
func runImportCommand(cfg config, password string, args []string) { func runImportCommand(cfg config, password string, args []string) {
options, err := parseImportOptions(args) options, err := parseImportOptions(args)
if err != nil { if err != nil {
+47
View File
@@ -0,0 +1,47 @@
package main
import (
"os"
"path/filepath"
"testing"
)
func TestConfigFilePathUsesXDGConfigDir(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
got, legacy, err := configFilePath()
if err != nil {
t.Fatal(err)
}
want := filepath.Join(os.Getenv("XDG_CONFIG_HOME"), "docspell-cli", "config.json")
if got != want {
t.Fatalf("expected %q, got %q", want, got)
}
if legacy != "" {
t.Fatalf("expected no legacy fallback, got %q", legacy)
}
}
func TestConfigFilePathFallsBackToLegacyHomeFile(t *testing.T) {
home := t.TempDir()
xdg := filepath.Join(t.TempDir(), "config")
t.Setenv("HOME", home)
t.Setenv("XDG_CONFIG_HOME", xdg)
legacyPath := filepath.Join(home, "docspell-cli.json")
if err := os.WriteFile(legacyPath, []byte("{}"), 0644); err != nil {
t.Fatal(err)
}
got, legacy, err := configFilePath()
if err != nil {
t.Fatal(err)
}
if got != legacyPath {
t.Fatalf("expected legacy path %q, got %q", legacyPath, got)
}
wantLegacy := filepath.Join(xdg, "docspell-cli", "config.json")
if legacy != wantLegacy {
t.Fatalf("expected XDG target %q, got %q", wantLegacy, legacy)
}
}