Skip to content

Commit

Permalink
支持 XDG 目录标准
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Jul 25, 2024
1 parent 973e734 commit 6ec1dde
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 21 deletions.
12 changes: 0 additions & 12 deletions config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"encoding/json"
"fmt"
"os"
"runtime"

"github.com/aliyun/aliyun-cli/cli"
"github.com/aliyun/aliyun-cli/util"
Expand Down Expand Up @@ -206,14 +205,3 @@ func GetConfigPath() string {
}
return path
}

func GetHomePath() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
}
return os.Getenv("HOME")
}
9 changes: 0 additions & 9 deletions config/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"encoding/json"
"errors"
"os"
"runtime"
"testing"

"github.com/aliyun/aliyun-cli/cli"
Expand Down Expand Up @@ -146,14 +145,6 @@ func TestLoadProfile(t *testing.T) {
assert.EqualError(t, err, "init config failed error")
}

func TestHomePath(t *testing.T) {
if runtime.GOOS == "windows" {
assert.Equal(t, os.Getenv("USERPROFILE"), GetHomePath())
} else {
assert.Equal(t, os.Getenv("HOME"), GetHomePath())
}
}

func TestGetConfigPath(t *testing.T) {
orighookGetHomePath := hookGetHomePath
defer func() {
Expand Down
39 changes: 39 additions & 0 deletions config/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package config

import (
"os"
"runtime"
)

func GetXDGConfigHome() string {
if xgh := os.Getenv("XDG_CONFIG_HOME"); xgh != "" {
return xgh
} else {
return GetHomePath() + "./config"
}
}

func GetConfigDirPath() string {
// ~/.aliyun/ 存在则是老的配置路径
// 否则:使用 XDG 规范
home := GetHomePath()
path := home + "/.aliyun"
_, err := os.Stat(path)
// 目录存在
if err != nil {
return path
}

return GetXDGConfigHome() + "/aliyun"
}

func GetHomePath() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
}
return os.Getenv("HOME")
}
23 changes: 23 additions & 0 deletions config/path_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package config

import (
"os"
"runtime"
"testing"

"github.com/stretchr/testify/assert"
)

func TestHomePath(t *testing.T) {
if runtime.GOOS == "windows" {
assert.Equal(t, os.Getenv("USERPROFILE"), GetHomePath())
} else {
assert.Equal(t, os.Getenv("HOME"), GetHomePath())
}
}

func TestGetXDGConfigHome(t *testing.T) {
if runtime.GOOS != "windows" {
assert.Equal(t, os.Getenv("HOME")+"/.config", GetXDGConfigHome())
}
}

0 comments on commit 6ec1dde

Please sign in to comment.