Skip to content

Commit

Permalink
Expand tilde to the users home directory
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
penguwin committed Aug 14, 2020
1 parent 7996704 commit 365a491
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"log"
"net/url"
"os"
"os/user"
"path/filepath"

gap "github.com/muesli/go-app-paths"
Expand Down Expand Up @@ -84,6 +85,23 @@ 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() on it 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)
}

c.url = url
return nil
}
Expand Down

0 comments on commit 365a491

Please sign in to comment.