Skip to content

Commit 09d31a2

Browse files
authored
feat(config): use xdg configuration (#13)
* docs(README): center title * feat(config): use xdg configuration
1 parent c862577 commit 09d31a2

File tree

3 files changed

+75
-8
lines changed

3 files changed

+75
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Note ✍️
1+
<h1 align="center">Note ✍️</h1>
22

33
<p align="center">
44
<img src="docs/demo.gif" alt="Note Demo" style="width: 100%;">

config.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,29 @@ type Dimensions struct {
4646
}
4747

4848
func getConfigDir() (string, error) {
49-
homeDir, err := os.UserHomeDir()
50-
if err != nil {
51-
return "", err
49+
return filepath.Join(getConfigHome(), "note"), nil
50+
}
51+
52+
func getConfigHome() string {
53+
if xdgConfig := os.Getenv("XDG_CONFIG_HOME"); xdgConfig != "" && filepath.IsAbs(xdgConfig) {
54+
return xdgConfig
5255
}
53-
return filepath.Join(homeDir, ".config", "note"), nil
56+
homeDir, _ := os.UserHomeDir()
57+
return filepath.Join(homeDir, ".config")
5458
}
5559

56-
func DefaultConfig() *Config {
60+
func getDataHome() string {
61+
if xdgData := os.Getenv("XDG_DATA_HOME"); xdgData != "" && filepath.IsAbs(xdgData) {
62+
return xdgData
63+
}
5764
homeDir, _ := os.UserHomeDir()
65+
return filepath.Join(homeDir, ".local", "share")
66+
}
67+
68+
func DefaultConfig() *Config {
5869
return &Config{
59-
NotesDir: filepath.Join(homeDir, ".note"),
60-
ArchiveDir: filepath.Join(homeDir, ".note", "archive"),
70+
NotesDir: filepath.Join(getDataHome(), "note"),
71+
ArchiveDir: filepath.Join(getDataHome(), "note", "archive"),
6172
DefaultEditor: "vim",
6273
Layout: Layout{
6374
SidebarWidth: 30,

config_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// config_test.go
2+
package main
3+
4+
import (
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
)
9+
10+
func TestXDGPaths(t *testing.T) {
11+
// Save original env vars
12+
originalConfigHome := os.Getenv("XDG_CONFIG_HOME")
13+
originalDataHome := os.Getenv("XDG_DATA_HOME")
14+
defer func() {
15+
os.Setenv("XDG_CONFIG_HOME", originalConfigHome)
16+
os.Setenv("XDG_DATA_HOME", originalDataHome)
17+
}()
18+
19+
homeDir, _ := os.UserHomeDir()
20+
tests := []struct {
21+
name string
22+
xdgConfigHome string
23+
xdgDataHome string
24+
wantConfig string
25+
wantData string
26+
}{
27+
{
28+
name: "XDG vars set",
29+
xdgConfigHome: "/custom/config",
30+
xdgDataHome: "/custom/data",
31+
wantConfig: "/custom/config",
32+
wantData: "/custom/data",
33+
},
34+
{
35+
name: "XDG vars empty",
36+
xdgConfigHome: "",
37+
xdgDataHome: "",
38+
wantConfig: filepath.Join(homeDir, ".config"),
39+
wantData: filepath.Join(homeDir, ".local", "share"),
40+
},
41+
}
42+
43+
for _, tt := range tests {
44+
t.Run(tt.name, func(t *testing.T) {
45+
os.Setenv("XDG_CONFIG_HOME", tt.xdgConfigHome)
46+
os.Setenv("XDG_DATA_HOME", tt.xdgDataHome)
47+
48+
if got := getConfigHome(); got != tt.wantConfig {
49+
t.Errorf("getConfigHome() = %v, want %v", got, tt.wantConfig)
50+
}
51+
if got := getDataHome(); got != tt.wantData {
52+
t.Errorf("getDataHome() = %v, want %v", got, tt.wantData)
53+
}
54+
})
55+
}
56+
}

0 commit comments

Comments
 (0)