Created
July 19, 2023 05:49
-
-
Save iam3yal/0d57b78f2dade478614b1347798c7c84 to your computer and use it in GitHub Desktop.
Jumps to a predefined directory through a given alias.
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
#Requires -Version 6.0 | |
[CmdletBinding(PositionalBinding)] | |
param ( | |
[Parameter(Position=0)] | |
[string] | |
$Operation, | |
[Parameter(Position=1)] | |
[String] | |
$Key, | |
[Parameter(Position=2)] | |
[String] | |
$Value | |
) | |
$File = ".GotoPath" | |
$FilePath = Join-Path -Path $HOME -ChildPath $File | |
if ($Value -eq "." -or [string]::IsNullOrEmpty($Value)) | |
{ | |
$Value = Get-Location | |
} | |
if ($Key -eq "." -or [string]::IsNullOrEmpty($Key)) | |
{ | |
$Key = (Get-Item -Path $Value).BaseName | |
} | |
$Key = ([String]$Key).ToUpper() | |
if (-Not (Test-Path $FilePath)) { | |
New-Item -Path $FilePath -ItemType "File" | |
} | |
$Paths = [Ordered]@{} | |
$FileContent = Get-Content -Path $FilePath | |
$Regex = "(?<KEY>\w+)\s*=(?<VALUE>.+)" | |
foreach($line in $FileContent) | |
{ | |
if ($line -match $Regex) | |
{ | |
$matchedKey = ([String]$Matches["KEY"]).ToUpper() | |
if (!$Paths.Contains(($matchedKey))) | |
{ | |
$matchedValue = ([String]$Matches["VALUE"]).Trim() | |
$Paths.Add($matchedKey, $matchedValue) | |
} | |
} | |
} | |
function GetCurrentPath { | |
return "$Key = $Value" | |
} | |
function GetStoredPath { | |
$OldPath = $Paths[$Key] | |
return "$Key = $OldPath" | |
} | |
if ($Operation -eq "add") | |
{ | |
Write-Host "$Key = $Value" | |
if (!$Paths.Contains($Key)) | |
{ | |
Add-Content -Path $FilePath -Value "$(GetCurrentPath)" | |
} | |
} | |
elseif ($Operation -eq "replace") | |
{ | |
if ($Paths.Contains($Key)) | |
{ | |
$FileContent -Replace $(GetStoredPath), $(GetCurrentPath) | Set-Content -Path $FilePath | |
} | |
} | |
elseif ($Operation -eq "remove") | |
{ | |
if ($Paths.Contains($Key)) | |
{ | |
$FileContent -Replace $(GetStoredPath), "" | Where-Object {$_ -ne ""} | Set-Content -Path $FilePath | |
} | |
} | |
elseif ($Operation -eq "list") | |
{ | |
Write-Host "\n" | |
foreach($path in $Paths.GetEnumerator()) | |
{ | |
Write-Host "$($path.Key) = $($path.Value)\n" | |
} | |
} | |
elseif (![string]::IsNullOrEmpty($Operation)) | |
{ | |
$Key = ([String]$Operation).ToUpper() | |
$Value = $Paths[$Key] | |
if ($Paths.Contains($Key)) | |
{ | |
$Value | Write-Output -NoEnumerate | |
} | |
exit 111 | |
} |
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
#!/bin/bash | |
GOTO_OUTPUT=$(pwsh -NoLogo -ExecutionPolicy Bypass -File "$HOME/Scripts/PowerShell/Goto.ps1" "$@") | |
if [ $? -eq 111 ] ; then | |
cd "$GOTO_OUTPUT" | |
exec $SHELL | |
else | |
echo -e $GOTO_OUTPUT | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment