Skip to content

Commit aa0ddb5

Browse files
committed
支持 XDG 目录标准
1 parent 973e734 commit aa0ddb5

File tree

3 files changed

+62
-12
lines changed

3 files changed

+62
-12
lines changed

config/configuration.go

-12
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"encoding/json"
1818
"fmt"
1919
"os"
20-
"runtime"
2120

2221
"github.com/aliyun/aliyun-cli/cli"
2322
"github.com/aliyun/aliyun-cli/util"
@@ -206,14 +205,3 @@ func GetConfigPath() string {
206205
}
207206
return path
208207
}
209-
210-
func GetHomePath() string {
211-
if runtime.GOOS == "windows" {
212-
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
213-
if home == "" {
214-
home = os.Getenv("USERPROFILE")
215-
}
216-
return home
217-
}
218-
return os.Getenv("HOME")
219-
}

config/path.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package config
2+
3+
import (
4+
"os"
5+
"runtime"
6+
)
7+
8+
func GetXDGConfigHome() string {
9+
if xgh := os.Getenv("XDG_CONFIG_HOME"); xgh != "" {
10+
return xgh
11+
} else {
12+
return GetHomePath() + "./config"
13+
}
14+
}
15+
16+
func GetConfigDirPath() string {
17+
// ~/.aliyun/ 存在则是老的配置路径
18+
// 否则:使用 XDG 规范
19+
home := GetHomePath()
20+
path := home + "/.aliyun"
21+
_, err := os.Stat(path)
22+
// 目录存在
23+
if err != nil {
24+
return path
25+
}
26+
27+
return GetXDGConfigHome() + "/aliyun"
28+
}
29+
30+
func GetHomePath() string {
31+
if runtime.GOOS == "windows" {
32+
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
33+
if home == "" {
34+
home = os.Getenv("USERPROFILE")
35+
}
36+
return home
37+
}
38+
return os.Getenv("HOME")
39+
}

config/path_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package config
2+
3+
import (
4+
"os"
5+
"runtime"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestHomePath(t *testing.T) {
12+
if runtime.GOOS == "windows" {
13+
assert.Equal(t, os.Getenv("USERPROFILE"), GetHomePath())
14+
} else {
15+
assert.Equal(t, os.Getenv("HOME"), GetHomePath())
16+
}
17+
}
18+
19+
func TestGetXDGConfigHome(t *testing.T) {
20+
if runtime.GOOS != "windows" {
21+
assert.Equal(t, os.Getenv("HOME")+"/.config", GetXDGConfigHome())
22+
}
23+
}

0 commit comments

Comments
 (0)