Skip to content

Commit

Permalink
fix(#117): support for XDG_CONFIG_HOME environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
mk-5 committed Sep 9, 2024
1 parent 0b0eb1e commit db02e39
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 294 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ jobs:
with:
go-version: ${{ matrix.go-version }}
- uses: actions/checkout@v3
- run: |
- run: |
export XDG_CONFIG_HOME=""
go test -coverprofile=coverage.out -coverpkg=./... -covermode=count ./internal/...
COVERAGE=$(go tool cover -func=coverage.out | grep total | awk '{print substr($3, 1, length($3)-1)}')
echo "Total coverage: $COVERAGE"
Expand Down
4 changes: 2 additions & 2 deletions internal/app/colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func Color(c string) tcell.Color {
}

func MustLoadColorScheme() map[string]interface{} {
d := os2.MustGetUserHomeDir()
p := fmt.Sprintf("%s/.fjira/colors.yml", d)
d := os2.MustGetFjiraHomeDir()
p := fmt.Sprintf("%s/colors.yml", d)
b, err := os.ReadFile(p)
if err != nil {
schemeMap = parseYMLStr(defaultColorsYML())
Expand Down
4 changes: 0 additions & 4 deletions internal/fjira/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ var (
)

func Install(workspace string) (*workspaces.WorkspaceSettings, error) {
// it will be removed after a few version
u := workspaces.NewDeprecatedUserHomeWorkspaces()
_ = u.MigrateFromGlobWorkspacesToYaml()

err := validateWorkspaceName(workspace)
if err != nil {
return nil, err
Expand Down
26 changes: 22 additions & 4 deletions internal/os/user_home_dir.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package os

import (
"fmt"
"os"
"runtime"
)
Expand All @@ -18,13 +19,30 @@ func SetUserHomeDir(dir string) error {
}

func MustGetUserHomeDir() string {
xdx := os.Getenv("XDX_CONFIG_HOME")
if xdx != "" {
return xdx
}
dir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
return dir
}

func MustGetFjiraHomeDir() string {
dir := MustGetUserHomeDir()
userHomeDir := fmt.Sprintf("%s/.fjira", dir)
xdg := os.Getenv("XDG_CONFIG_HOME")
xdgDir := fmt.Sprintf("%s/fjira", xdg)
if xdg != "" && dirExist(xdgDir) {
return xdgDir
}
if xdg != "" && !dirExist(userHomeDir) {
return xdgDir
}
return userHomeDir
}

func dirExist(path string) bool {
if stat, err := os.Stat(path); err == nil && stat.IsDir() {
return true
}
return false
}
46 changes: 42 additions & 4 deletions internal/os/user_home_dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,53 @@ func TestMustGetUserHomeDir_xdxPath(t *testing.T) {
tests := []struct {
name string
}{
{"should return XDX path if available"},
{"should return XDG path if available"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_ = os.Setenv("XDX_CONFIG_HOME", "abc")
if got := MustGetUserHomeDir(); got != "abc" {
_ = os.Setenv("XDG_CONFIG_HOME", "abc")
_ = SetUserHomeDir("something_for_just_for_test")
if got := MustGetFjiraHomeDir(); got != "abc/fjira" {
t.Errorf("MustGetUserHomeDir() = %v, want %v", got, "abc")
}
_ = os.Setenv("XDX_CONFIG_HOME", "")
_ = os.Setenv("XDG_CONFIG_HOME", "")
})
}
}

func TestMustGetUserHomeDir_userHomeWhenExist(t *testing.T) {
tests := []struct {
name string
}{
{"should return userHome when exist, and XDG doesn't"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_ = os.Setenv("XDG_CONFIG_HOME", "abc")
_ = SetUserHomeDir("./something_for_just_for_test")
_ = os.MkdirAll("./something_for_just_for_test/.fjira", 0750)
if got := MustGetFjiraHomeDir(); got != "./something_for_just_for_test/.fjira" {
t.Errorf("MustGetUserHomeDir() = %v, want %v", got, "abc")
}
_ = os.Setenv("XDG_CONFIG_HOME", "")
_ = os.RemoveAll("./something_for_just_for_test")
})
}
}

func TestMustGetUserHomeDir_homePath(t *testing.T) {
tests := []struct {
name string
}{
{"should return HOME path if available"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_ = SetUserHomeDir("user_home")
if got := MustGetFjiraHomeDir(); got != "user_home/.fjira" {
t.Errorf("MustGetUserHomeDir() = %v, want %v", got, "abc")
}
_ = os.Setenv("HOME", "")
})
}
}
3 changes: 1 addition & 2 deletions internal/workspaces/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ func (s *userHomeSettingsStorage) ReadAllWorkspaces() ([]string, error) {
}

func (s *userHomeSettingsStorage) ConfigDir() (string, error) {
userHomeDir := os2.MustGetUserHomeDir()
configDir := fmt.Sprintf("%s/.fjira", userHomeDir)
configDir := os2.MustGetFjiraHomeDir()
if _, err := os.Stat(configDir); errors.Is(err, os.ErrNotExist) {
err := os.Mkdir(configDir, os.ModePerm)
if err != nil {
Expand Down
127 changes: 0 additions & 127 deletions internal/workspaces/workspaces.go

This file was deleted.

Loading

0 comments on commit db02e39

Please sign in to comment.