-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
973e734
commit aa0ddb5
Showing
3 changed files
with
62 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
Check failure on line 11 in config/path_test.go GitHub Actions / build (ubuntu-latest)
|
||
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()) | ||
} | ||
} |