Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持 XDG 目录标准 #1069

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"encoding/json"
"fmt"
"os"
"runtime"

"github.com/aliyun/aliyun-cli/cli"
"github.com/aliyun/aliyun-cli/util"
Expand Down Expand Up @@ -206,14 +205,3 @@ func GetConfigPath() string {
}
return path
}

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")
}
9 changes: 0 additions & 9 deletions config/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"encoding/json"
"errors"
"os"
"runtime"
"testing"

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

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 TestGetConfigPath(t *testing.T) {
orighookGetHomePath := hookGetHomePath
defer func() {
Expand Down
39 changes: 39 additions & 0 deletions config/path.go
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")
}
29 changes: 29 additions & 0 deletions config/path_test.go
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())
}
Loading