File tree 4 files changed +68
-21
lines changed
4 files changed +68
-21
lines changed Original file line number Diff line number Diff line change @@ -17,7 +17,6 @@ import (
17
17
"encoding/json"
18
18
"fmt"
19
19
"os"
20
- "runtime"
21
20
22
21
"github.com/aliyun/aliyun-cli/cli"
23
22
"github.com/aliyun/aliyun-cli/util"
@@ -206,14 +205,3 @@ func GetConfigPath() string {
206
205
}
207
206
return path
208
207
}
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
- }
Original file line number Diff line number Diff line change @@ -18,7 +18,6 @@ import (
18
18
"encoding/json"
19
19
"errors"
20
20
"os"
21
- "runtime"
22
21
"testing"
23
22
24
23
"github.com/aliyun/aliyun-cli/cli"
@@ -146,14 +145,6 @@ func TestLoadProfile(t *testing.T) {
146
145
assert .EqualError (t , err , "init config failed error" )
147
146
}
148
147
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
-
157
148
func TestGetConfigPath (t * testing.T ) {
158
149
orighookGetHomePath := hookGetHomePath
159
150
defer func () {
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments