optimize: expand '~' with user home

This commit is contained in:
mzz2017 committed 2021-10-04 23:19:48 +08:00
1 parent 60a226e0df
commit 65bed99656
2 files changed
+20

No files matched your search

+14
View File
@@ -6,6 +6,8 @@ import (
"fmt"
"net/url"
"os"
"os/user"
"path/filepath"
"reflect"
"runtime"
"strconv"
@@ -237,3 +239,15 @@ func ToBytes(val interface{}) (b []byte, err error) {
}
return BytesCopy(buf.Bytes()), nil
}
func HomeExpand(path string) (string, error) {
if !strings.HasPrefix(path, "~") {
return path, nil
}
usr, err := user.Current()
if err != nil {
return "", err
}
return filepath.Join(usr.HomeDir, path[1:]), nil
}
+6
View File
@@ -3,6 +3,7 @@ package conf
import (
"fmt"
"github.com/stevenroose/gonfig"
"github.com/v2rayA/v2rayA/common"
"github.com/v2rayA/v2rayA/pkg/util/log"
log2 "log"
"os"
@@ -62,6 +63,11 @@ func initFunc() {
filepath.Dir(params.Config),
strings.ReplaceAll(filepath.Base(params.Config), ".", "_"),
)
// expand '~' with user home
params.Config, err = common.HomeExpand(params.Config)
if err != nil {
log2.Fatal(err)
}
if strings.Contains(params.Config, "$HOME") {
if h, err := os.UserHomeDir(); err == nil {
params.Config = strings.ReplaceAll(params.Config, "$HOME", h)