From 862729809e559510af69a10ca2a5a5cb76be9ad1 Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Sun, 25 Feb 2018 22:41:44 -0500 Subject: [PATCH] config+cmd/lncli: ensure tilde in path is expanded to home dir Since we're now able to create a base LND directory that no longer lives under the OS specific application data directory, we modify cleanAndExpandPath to replace the `~` in a path to the current user's home directory, rather than the user's application data directory. --- cmd/lncli/main.go | 11 ++++++++++- config.go | 11 ++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/cmd/lncli/main.go b/cmd/lncli/main.go index 6dbff198..85d0f7ad 100644 --- a/cmd/lncli/main.go +++ b/cmd/lncli/main.go @@ -8,6 +8,7 @@ import ( "fmt" "io/ioutil" "os" + "os/user" "path/filepath" "strings" @@ -236,7 +237,15 @@ func main() { func cleanAndExpandPath(path string) string { // Expand initial ~ to OS specific home directory. if strings.HasPrefix(path, "~") { - homeDir := filepath.Dir(defaultLndDir) + var homeDir string + + user, err := user.Current() + if err == nil { + homeDir = user.HomeDir + } else { + homeDir = os.Getenv("HOME") + } + path = strings.Replace(path, "~", homeDir, 1) } diff --git a/config.go b/config.go index 07dfa8ac..5030b552 100644 --- a/config.go +++ b/config.go @@ -9,6 +9,7 @@ import ( "io/ioutil" "net" "os" + "os/user" "path" "path/filepath" "regexp" @@ -609,7 +610,15 @@ func loadConfig() (*config, error) { func cleanAndExpandPath(path string) string { // Expand initial ~ to OS specific home directory. if strings.HasPrefix(path, "~") { - homeDir := filepath.Dir(defaultLndDir) + var homeDir string + + user, err := user.Current() + if err == nil { + homeDir = user.HomeDir + } else { + homeDir = os.Getenv("HOME") + } + path = strings.Replace(path, "~", homeDir, 1) }