-
Notifications
You must be signed in to change notification settings - Fork 702
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 4a7b487
Showing
4 changed files
with
60 additions
and
26 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 "", 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