Skip to content

Commit db02e39

Browse files
committed
fix(#117): support for XDG_CONFIG_HOME environment variable
1 parent 0b0eb1e commit db02e39

File tree

8 files changed

+69
-294
lines changed

8 files changed

+69
-294
lines changed

.github/workflows/tests.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ jobs:
1818
with:
1919
go-version: ${{ matrix.go-version }}
2020
- uses: actions/checkout@v3
21-
- run: |
21+
- run: |
22+
export XDG_CONFIG_HOME=""
2223
go test -coverprofile=coverage.out -coverpkg=./... -covermode=count ./internal/...
2324
COVERAGE=$(go tool cover -func=coverage.out | grep total | awk '{print substr($3, 1, length($3)-1)}')
2425
echo "Total coverage: $COVERAGE"

internal/app/colors.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ func Color(c string) tcell.Color {
2424
}
2525

2626
func MustLoadColorScheme() map[string]interface{} {
27-
d := os2.MustGetUserHomeDir()
28-
p := fmt.Sprintf("%s/.fjira/colors.yml", d)
27+
d := os2.MustGetFjiraHomeDir()
28+
p := fmt.Sprintf("%s/colors.yml", d)
2929
b, err := os.ReadFile(p)
3030
if err != nil {
3131
schemeMap = parseYMLStr(defaultColorsYML())

internal/fjira/install.go

-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ var (
2929
)
3030

3131
func Install(workspace string) (*workspaces.WorkspaceSettings, error) {
32-
// it will be removed after a few version
33-
u := workspaces.NewDeprecatedUserHomeWorkspaces()
34-
_ = u.MigrateFromGlobWorkspacesToYaml()
35-
3632
err := validateWorkspaceName(workspace)
3733
if err != nil {
3834
return nil, err

internal/os/user_home_dir.go

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package os
22

33
import (
4+
"fmt"
45
"os"
56
"runtime"
67
)
@@ -18,13 +19,30 @@ func SetUserHomeDir(dir string) error {
1819
}
1920

2021
func MustGetUserHomeDir() string {
21-
xdx := os.Getenv("XDX_CONFIG_HOME")
22-
if xdx != "" {
23-
return xdx
24-
}
2522
dir, err := os.UserHomeDir()
2623
if err != nil {
2724
panic(err)
2825
}
2926
return dir
3027
}
28+
29+
func MustGetFjiraHomeDir() string {
30+
dir := MustGetUserHomeDir()
31+
userHomeDir := fmt.Sprintf("%s/.fjira", dir)
32+
xdg := os.Getenv("XDG_CONFIG_HOME")
33+
xdgDir := fmt.Sprintf("%s/fjira", xdg)
34+
if xdg != "" && dirExist(xdgDir) {
35+
return xdgDir
36+
}
37+
if xdg != "" && !dirExist(userHomeDir) {
38+
return xdgDir
39+
}
40+
return userHomeDir
41+
}
42+
43+
func dirExist(path string) bool {
44+
if stat, err := os.Stat(path); err == nil && stat.IsDir() {
45+
return true
46+
}
47+
return false
48+
}

internal/os/user_home_dir_test.go

+42-4
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,53 @@ func TestMustGetUserHomeDir_xdxPath(t *testing.T) {
99
tests := []struct {
1010
name string
1111
}{
12-
{"should return XDX path if available"},
12+
{"should return XDG path if available"},
1313
}
1414
for _, tt := range tests {
1515
t.Run(tt.name, func(t *testing.T) {
16-
_ = os.Setenv("XDX_CONFIG_HOME", "abc")
17-
if got := MustGetUserHomeDir(); got != "abc" {
16+
_ = os.Setenv("XDG_CONFIG_HOME", "abc")
17+
_ = SetUserHomeDir("something_for_just_for_test")
18+
if got := MustGetFjiraHomeDir(); got != "abc/fjira" {
1819
t.Errorf("MustGetUserHomeDir() = %v, want %v", got, "abc")
1920
}
20-
_ = os.Setenv("XDX_CONFIG_HOME", "")
21+
_ = os.Setenv("XDG_CONFIG_HOME", "")
22+
})
23+
}
24+
}
25+
26+
func TestMustGetUserHomeDir_userHomeWhenExist(t *testing.T) {
27+
tests := []struct {
28+
name string
29+
}{
30+
{"should return userHome when exist, and XDG doesn't"},
31+
}
32+
for _, tt := range tests {
33+
t.Run(tt.name, func(t *testing.T) {
34+
_ = os.Setenv("XDG_CONFIG_HOME", "abc")
35+
_ = SetUserHomeDir("./something_for_just_for_test")
36+
_ = os.MkdirAll("./something_for_just_for_test/.fjira", 0750)
37+
if got := MustGetFjiraHomeDir(); got != "./something_for_just_for_test/.fjira" {
38+
t.Errorf("MustGetUserHomeDir() = %v, want %v", got, "abc")
39+
}
40+
_ = os.Setenv("XDG_CONFIG_HOME", "")
41+
_ = os.RemoveAll("./something_for_just_for_test")
42+
})
43+
}
44+
}
45+
46+
func TestMustGetUserHomeDir_homePath(t *testing.T) {
47+
tests := []struct {
48+
name string
49+
}{
50+
{"should return HOME path if available"},
51+
}
52+
for _, tt := range tests {
53+
t.Run(tt.name, func(t *testing.T) {
54+
_ = SetUserHomeDir("user_home")
55+
if got := MustGetFjiraHomeDir(); got != "user_home/.fjira" {
56+
t.Errorf("MustGetUserHomeDir() = %v, want %v", got, "abc")
57+
}
58+
_ = os.Setenv("HOME", "")
2159
})
2260
}
2361
}

internal/workspaces/settings.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,7 @@ func (s *userHomeSettingsStorage) ReadAllWorkspaces() ([]string, error) {
149149
}
150150

151151
func (s *userHomeSettingsStorage) ConfigDir() (string, error) {
152-
userHomeDir := os2.MustGetUserHomeDir()
153-
configDir := fmt.Sprintf("%s/.fjira", userHomeDir)
152+
configDir := os2.MustGetFjiraHomeDir()
154153
if _, err := os.Stat(configDir); errors.Is(err, os.ErrNotExist) {
155154
err := os.Mkdir(configDir, os.ModePerm)
156155
if err != nil {

internal/workspaces/workspaces.go

-127
This file was deleted.

0 commit comments

Comments
 (0)