More robust error handling when reading/writing the DB
This commit is contained in:
23
z.ps1
23
z.ps1
@@ -97,7 +97,12 @@ function Add-ToZDatabase {
|
|||||||
|
|
||||||
# Maintain the data file
|
# Maintain the data file
|
||||||
$TmpFile = New-TemporaryFile;
|
$TmpFile = New-TemporaryFile;
|
||||||
$Data = Import-Csv -Path $ZDataFile;
|
try {
|
||||||
|
$Data = Import-Csv -Path $ZDataFile;
|
||||||
|
} catch [FileNotFoundException] {
|
||||||
|
Set-Location -Path $Add;
|
||||||
|
throw "Couldn't read from $ZDataFile";
|
||||||
|
}
|
||||||
$Rank = 1;
|
$Rank = 1;
|
||||||
$Time = Get-EpochTimeNow;
|
$Time = Get-EpochTimeNow;
|
||||||
$Sum = 0;
|
$Sum = 0;
|
||||||
@@ -146,6 +151,9 @@ function Set-ZLocation {
|
|||||||
[parameter(Mandatory = $false)][switch]$RestrictToSubdirs,
|
[parameter(Mandatory = $false)][switch]$RestrictToSubdirs,
|
||||||
[String[]]$Patterns
|
[String[]]$Patterns
|
||||||
)
|
)
|
||||||
|
$ZDataFile = if ($env:_Z_DATA) { $env:_Z_DATA } else { "$env:USERPROFILE\.z" };
|
||||||
|
New-ZDataFile;
|
||||||
|
|
||||||
$HighRank = $null;
|
$HighRank = $null;
|
||||||
$IHighRank = $null;
|
$IHighRank = $null;
|
||||||
$BestMatch = $null;
|
$BestMatch = $null;
|
||||||
@@ -206,14 +214,19 @@ function Set-ZLocation {
|
|||||||
throw "Unable to find appropriate directory."
|
throw "Unable to find appropriate directory."
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Test-Path -PathType Container "$env:USERPROFILE\.z") {
|
function New-ZDataFile {
|
||||||
Write-Output "ERROR: z.ps1's datafile ($env:USERPROFILE\.z) is a directory!"
|
if (Test-Path -PathType Container "$env:USERPROFILE\.z") {
|
||||||
} else {
|
throw "ERROR: z.ps1's datafile ($env:USERPROFILE\.z) is a directory!"
|
||||||
if (-not (Test-Path -PathType Leaf "$env:USERPROFILE\.z")) {
|
} elseif (-not (Test-Path -PathType Leaf "$env:USERPROFILE\.z")) {
|
||||||
New-Item -Path "$env:USERPROFILE\.z"
|
New-Item -Path "$env:USERPROFILE\.z"
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function Set-ZAlias {
|
||||||
$AliasName = if ($env:_Z_CMD) { $env:_Z_CMD } else { "z" }
|
$AliasName = if ($env:_Z_CMD) { $env:_Z_CMD } else { "z" }
|
||||||
Set-Alias -Name $AliasName -Value Set-ZLocation -Option AllScope
|
Set-Alias -Name $AliasName -Value Set-ZLocation -Option AllScope
|
||||||
Set-Alias -Name "cd" -Value Add-ToZDatabase -Option AllScope
|
Set-Alias -Name "cd" -Value Add-ToZDatabase -Option AllScope
|
||||||
}
|
}
|
||||||
|
|
||||||
|
New-ZDataFile
|
||||||
|
Set-ZAlias
|
||||||
Reference in New Issue
Block a user