Created
March 31, 2022 22:56
-
-
Save SteveL-MSFT/4479d8d034549cbfe22731891d0fccfd to your computer and use it in GitHub Desktop.
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
param( | |
[Parameter(ValueFromPipeline)] | |
[string]$json | |
) | |
$obj = ConvertFrom-Json -InputObject $json -Depth 100 -AsHashtable | |
$location = "/" | |
function Test-Location ($path) { | |
$paths = $path.Split('/') | |
return $true | |
} | |
function Get-Json ($filter) { | |
$cur = $obj | |
foreach ($path in $location.Split('/', [StringSplitOptions]::RemoveEmptyEntries)) { | |
$cur = $cur.$path | |
} | |
return ($cur | Out-String) | |
} | |
while($true) { | |
$i = Read-Host -Prompt $location | |
$command = $i.Split(' ') | |
switch ($command[0]) { | |
'cd' { | |
$newLocation = $command[1] | |
if ($newLocation -eq '..') { | |
if ($location -eq '/') { | |
Write-Error "At root" | |
continue | |
} | |
$location = Split-Path $location -Parent | |
if ("" -eq $location) { | |
$location = '/' | |
} | |
} | |
elseif (Test-Location $newLocation) { | |
$location = Join-Path $location $newLocation | |
} | |
else { | |
Write-Error "Path does not exist" | |
} | |
} | |
'dir' { | |
$filter = $command[1] | |
Write-Host (Get-Json $filter) | |
} | |
'exit' { | |
return | |
} | |
default { | |
Write-Error "Command not found. Use 'cd', 'dir', or 'exit'" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment