mirror of
https://github.com/v2rayA/v2rayA.git
synced 2026-11-20 23:02:25 +03:00
optimize: expand '~' with user home
This commit is contained in:
1 parent
60a226e0df
commit
65bed99656
2 files changed
+20
No files matched your search
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user