-
Notifications
You must be signed in to change notification settings - Fork 201
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 7d4ba26
Showing
4 changed files
with
68 additions
and
21 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
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,29 @@ | ||
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" { | ||
return | ||
} | ||
|
||
assert.Equal(t, os.Getenv("HOME")+"/.config", GetXDGConfigHome()) | ||
os.Setenv("XDG_CONFIG_HOME", "/tmp/config") | ||
assert.Equal(t, "/tmp/config", GetXDGConfigHome()) | ||
os.Setenv("XDG_CONFIG_HOME", "") | ||
assert.Equal(t, os.Getenv("HOME")+"/.config", GetXDGConfigHome()) | ||
} |