Skip to content

Commit 7d4ba26

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

File tree

4 files changed

+68
-21
lines changed

4 files changed

+68
-21
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/configuration_test.go

-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"encoding/json"
1919
"errors"
2020
"os"
21-
"runtime"
2221
"testing"
2322

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

149-
func TestHomePath(t *testing.T) {
150-
if runtime.GOOS == "windows" {
151-
assert.Equal(t, os.Getenv("USERPROFILE"), GetHomePath())
152-
} else {
153-
assert.Equal(t, os.Getenv("HOME"), GetHomePath())
154-
}
155-
}
156-
157148
func TestGetConfigPath(t *testing.T) {
158149
orighookGetHomePath := hookGetHomePath
159150
defer func() {

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

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
return
22+
}
23+
24+
assert.Equal(t, os.Getenv("HOME")+"/.config", GetXDGConfigHome())
25+
os.Setenv("XDG_CONFIG_HOME", "/tmp/config")
26+
assert.Equal(t, "/tmp/config", GetXDGConfigHome())
27+
os.Setenv("XDG_CONFIG_HOME", "")
28+
assert.Equal(t, os.Getenv("HOME")+"/.config", GetXDGConfigHome())
29+
}

0 commit comments

Comments
 (0)