diff --git a/README.md b/README.md index 817d110..8cd5edc 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Note ✍️ +

Note ✍️

Note Demo diff --git a/config.go b/config.go index b5bdf28..0e8f774 100644 --- a/config.go +++ b/config.go @@ -46,18 +46,29 @@ type Dimensions struct { } func getConfigDir() (string, error) { - homeDir, err := os.UserHomeDir() - if err != nil { - return "", err + return filepath.Join(getConfigHome(), "note"), nil +} + +func getConfigHome() string { + if xdgConfig := os.Getenv("XDG_CONFIG_HOME"); xdgConfig != "" && filepath.IsAbs(xdgConfig) { + return xdgConfig } - return filepath.Join(homeDir, ".config", "note"), nil + homeDir, _ := os.UserHomeDir() + return filepath.Join(homeDir, ".config") } -func DefaultConfig() *Config { +func getDataHome() string { + if xdgData := os.Getenv("XDG_DATA_HOME"); xdgData != "" && filepath.IsAbs(xdgData) { + return xdgData + } homeDir, _ := os.UserHomeDir() + return filepath.Join(homeDir, ".local", "share") +} + +func DefaultConfig() *Config { return &Config{ - NotesDir: filepath.Join(homeDir, ".note"), - ArchiveDir: filepath.Join(homeDir, ".note", "archive"), + NotesDir: filepath.Join(getDataHome(), "note"), + ArchiveDir: filepath.Join(getDataHome(), "note", "archive"), DefaultEditor: "vim", Layout: Layout{ SidebarWidth: 30, diff --git a/config_test.go b/config_test.go new file mode 100644 index 0000000..f88ef60 --- /dev/null +++ b/config_test.go @@ -0,0 +1,56 @@ +// config_test.go +package main + +import ( + "os" + "path/filepath" + "testing" +) + +func TestXDGPaths(t *testing.T) { + // Save original env vars + originalConfigHome := os.Getenv("XDG_CONFIG_HOME") + originalDataHome := os.Getenv("XDG_DATA_HOME") + defer func() { + os.Setenv("XDG_CONFIG_HOME", originalConfigHome) + os.Setenv("XDG_DATA_HOME", originalDataHome) + }() + + homeDir, _ := os.UserHomeDir() + tests := []struct { + name string + xdgConfigHome string + xdgDataHome string + wantConfig string + wantData string + }{ + { + name: "XDG vars set", + xdgConfigHome: "/custom/config", + xdgDataHome: "/custom/data", + wantConfig: "/custom/config", + wantData: "/custom/data", + }, + { + name: "XDG vars empty", + xdgConfigHome: "", + xdgDataHome: "", + wantConfig: filepath.Join(homeDir, ".config"), + wantData: filepath.Join(homeDir, ".local", "share"), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + os.Setenv("XDG_CONFIG_HOME", tt.xdgConfigHome) + os.Setenv("XDG_DATA_HOME", tt.xdgDataHome) + + if got := getConfigHome(); got != tt.wantConfig { + t.Errorf("getConfigHome() = %v, want %v", got, tt.wantConfig) + } + if got := getDataHome(); got != tt.wantData { + t.Errorf("getDataHome() = %v, want %v", got, tt.wantData) + } + }) + } +}