Catch errors with Resolve-Path

This commit is contained in:
2021-05-19 01:35:34 -04:00
parent d1a72f5882
commit f49f3eb002

14
z.ps1
View File

@@ -67,7 +67,21 @@ function Get-EpochTimeNow {
function Add-ToZDatabase { function Add-ToZDatabase {
$ZDataFile = if ($env:_Z_DATA) { $env:_Z_DATA } else { "$env:USERPROFILE\.z" }; $ZDataFile = if ($env:_Z_DATA) { $env:_Z_DATA } else { "$env:USERPROFILE\.z" };
$Add = $args[0]; $Add = $args[0];
if (-not (Test-Path -Path $Add -IsValid)) {
throw "Invalid path specified"
}
$Add = (Resolve-Path $Add.TrimEnd("\")); $Add = (Resolve-Path $Add.TrimEnd("\"));
if ($Add -match "^[A-Z]:$") {
# NOTE: Fixes weird behavior where `Resolve-Path C:` =>
# C:\Users\Username instead of C:\. Not sure why it does this.
#
# NOTE 2: Apparently the default behavior for 'Set-Location C:' is to cd
# to C:\Users\Username, or whatever the 'home' folder is for that drive.
# This is really stupid and unintuitive IMHO so I'm leaving this code
# here out of spite even though it's a NO-OP.
$Add += "\";
}
# Check so we don't match $HOME # Check so we don't match $HOME
if ($Add -eq $env:HOME) { return $null; } if ($Add -eq $env:HOME) { return $null; }