From 37c62bc094e19978ba278bb92ceaac21f0c4484e Mon Sep 17 00:00:00 2001 From: Nicolas Martin Date: Fri, 14 Aug 2020 09:15:14 +0200 Subject: [PATCH] Expand tilde to the users home directory The setURL() method will now correctly expand the tilde to the current users home directory for inputs like this: ``` crypto://password@~/path/to/file ``` Before this change the tilde symbol got interpreted as a normal directory name. --- config/config.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/config/config.go b/config/config.go index 75a4e4d9..b03c89d5 100644 --- a/config/config.go +++ b/config/config.go @@ -12,6 +12,7 @@ import ( "log" "net/url" "os" + "os/user" "path/filepath" gap "github.com/muesli/go-app-paths" @@ -84,6 +85,24 @@ func (c *Config) SetURL(u string) error { return err } + // Expand tilde to the users home directory + // This is needed in case the shell is unable to expand the path to the users + // home directory for inputs like these: + // crypto://password@~/path/to/config + // NOTE: - url.Parse() will interpret `~` as the Host Element of the URL in + // this case. + // - Using filepath.Abs() won't work as it will interpret `~` as the + // name of a regular folder. + if url.Host == "~" { + usr, err := user.Current() + if err != nil { + return err + } + + url.Path = filepath.Join(usr.HomeDir, url.Path) + url.Host = "" + } + c.url = url return nil }