aboutsummaryrefslogtreecommitdiffstats
path: root/internal/path_util
diff options
context:
space:
mode:
authorPaulo Gomes <pjbgf@linux.com>2023-07-11 08:53:03 +0100
committerGitHub <noreply@github.com>2023-07-11 08:53:03 +0100
commit56c4bf4ca9789505db7a6eefb910a7259c1fcb79 (patch)
tree3049fbf24af25a3e52bf7a391e959e837e032e3a /internal/path_util
parentdc17aae6503560777a665c9cfb0d2fcb3a9a9274 (diff)
parent5dad9b23030e344a4fd1458df0c50e6ada55a01a (diff)
downloadgo-git-56c4bf4ca9789505db7a6eefb910a7259c1fcb79.tar.gz
Merge pull request #809 from AriehSchneier/tilde-user-path
*: Handle paths starting with ~Username
Diffstat (limited to 'internal/path_util')
-rw-r--r--internal/path_util/path_util.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/internal/path_util/path_util.go b/internal/path_util/path_util.go
new file mode 100644
index 0000000..48e4a3d
--- /dev/null
+++ b/internal/path_util/path_util.go
@@ -0,0 +1,29 @@
+package path_util
+
+import (
+ "os"
+ "os/user"
+ "strings"
+)
+
+func ReplaceTildeWithHome(path string) (string, error) {
+ if strings.HasPrefix(path, "~") {
+ firstSlash := strings.Index(path, "/")
+ if firstSlash == 1 {
+ home, err := os.UserHomeDir()
+ if err != nil {
+ return path, err
+ }
+ return strings.Replace(path, "~", home, 1), nil
+ } else if firstSlash > 1 {
+ username := path[1:firstSlash]
+ userAccount, err := user.Lookup(username)
+ if err != nil {
+ return path, err
+ }
+ return strings.Replace(path, path[:firstSlash], userAccount.HomeDir, 1), nil
+ }
+ }
+
+ return path, nil
+}