Use go-homedir to read the home directory more reliably

This commit is contained in:
Nick Craig-Wood 2019-05-10 21:25:24 +01:00
parent 1fefa6adfd
commit edda6d91cd
2 changed files with 11 additions and 16 deletions

View File

@ -19,6 +19,7 @@ import (
"sync" "sync"
"time" "time"
homedir "github.com/mitchellh/go-homedir"
"github.com/ncw/rclone/fs" "github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fs/config" "github.com/ncw/rclone/fs/config"
"github.com/ncw/rclone/fs/config/configmap" "github.com/ncw/rclone/fs/config/configmap"
@ -322,7 +323,10 @@ func (f *Fs) putSftpConnection(pc **conn, err error) {
func shellExpand(s string) string { func shellExpand(s string) string {
if s != "" { if s != "" {
if s[0] == '~' { if s[0] == '~' {
s = "${HOME}" + s[1:] newS, err := homedir.Expand(s)
if err == nil {
s = newS
}
} }
s = os.ExpandEnv(s) s = os.ExpandEnv(s)
} }

View File

@ -14,7 +14,6 @@ import (
"log" "log"
mathrand "math/rand" mathrand "math/rand"
"os" "os"
"os/user"
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime" "runtime"
@ -25,6 +24,7 @@ import (
"unicode/utf8" "unicode/utf8"
"github.com/Unknwon/goconfig" "github.com/Unknwon/goconfig"
homedir "github.com/mitchellh/go-homedir"
"github.com/ncw/rclone/fs" "github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fs/accounting" "github.com/ncw/rclone/fs/accounting"
"github.com/ncw/rclone/fs/config/configmap" "github.com/ncw/rclone/fs/config/configmap"
@ -106,16 +106,7 @@ func getConfigData() *goconfig.ConfigFile {
// Return the path to the configuration file // Return the path to the configuration file
func makeConfigPath() string { func makeConfigPath() string {
// Find user's home directory // Find user's home directory
usr, err := user.Current() homeDir, err := homedir.Dir()
var homedir string
if err == nil {
homedir = usr.HomeDir
} else {
// Fall back to reading $HOME - work around user.Current() not
// working for cross compiled binaries on OSX.
// https://github.com/golang/go/issues/6376
homedir = os.Getenv("HOME")
}
// Find user's configuration directory. // Find user's configuration directory.
// Prefer XDG config path, with fallback to $HOME/.config. // Prefer XDG config path, with fallback to $HOME/.config.
@ -126,9 +117,9 @@ func makeConfigPath() string {
if xdgdir != "" { if xdgdir != "" {
// User's configuration directory for rclone is $XDG_CONFIG_HOME/rclone // User's configuration directory for rclone is $XDG_CONFIG_HOME/rclone
cfgdir = filepath.Join(xdgdir, "rclone") cfgdir = filepath.Join(xdgdir, "rclone")
} else if homedir != "" { } else if homeDir != "" {
// User's configuration directory for rclone is $HOME/.config/rclone // User's configuration directory for rclone is $HOME/.config/rclone
cfgdir = filepath.Join(homedir, ".config", "rclone") cfgdir = filepath.Join(homeDir, ".config", "rclone")
} }
// Use rclone.conf from user's configuration directory if already existing // Use rclone.conf from user's configuration directory if already existing
@ -143,8 +134,8 @@ func makeConfigPath() string {
// Use .rclone.conf from user's home directory if already existing // Use .rclone.conf from user's home directory if already existing
var homeconf string var homeconf string
if homedir != "" { if homeDir != "" {
homeconf = filepath.Join(homedir, hiddenConfigFileName) homeconf = filepath.Join(homeDir, hiddenConfigFileName)
_, err := os.Stat(homeconf) _, err := os.Stat(homeconf)
if err == nil { if err == nil {
return homeconf return homeconf