Skip to content

Commit

Permalink
支持 XDG 目录标准
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Jul 25, 2024
1 parent 973e734 commit aa0ddb5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 12 deletions.
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")
}
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")
}
23 changes: 23 additions & 0 deletions config/path_test.go
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

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

TestHomePath redeclared in this block

Check failure on line 11 in config/path_test.go

View workflow job for this annotation

GitHub Actions / build (macos-12)

TestHomePath redeclared in this block
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())
}
}

0 comments on commit aa0ddb5

Please sign in to comment.