-
Notifications
You must be signed in to change notification settings - Fork 703
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
*: Handle paths starting with ~username
Signed-off-by: Arieh Schneier <15041913+AriehSchneier@users.noreply.github.com>
- Loading branch information
1 parent
dc17aae
commit 5dad9b2
Showing
4 changed files
with
52 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters