Public/Remove-DevVmAlias.ps1
|
function Remove-DevVmAlias { <# .SYNOPSIS Removes a custom alias for devvm. .DESCRIPTION Removes a custom alias from the global .devvm configuration file. Default aliases (devvm, dvm) cannot be removed. .PARAMETER Name The name of the alias to remove. .EXAMPLE Remove-DevVmAlias -Name "vm" Removes the custom alias 'vm'. .NOTES The alias will be removed from the configuration file but will remain active in the current PowerShell session. Restart PowerShell for the change to take full effect. #> [CmdletBinding()] param( [Parameter(Mandatory = $true, Position = 0)] [string]$Name ) # Prevent removal of default aliases if ($Name -eq 'devvm' -or $Name -eq 'dvm') { Write-Host "Cannot remove default alias '$Name'." -ForegroundColor Red return } $homeDir = Get-HomeDirectory $globalConfigPath = Join-Path -Path $homeDir -ChildPath '.devvm' if (-not (Test-Path $globalConfigPath)) { Write-Host "No custom aliases configured." -ForegroundColor Yellow return } # Load existing config $config = @{ versions = @{} runtimes = @{} alias = @() } try { $rawContent = Get-Content -Path $globalConfigPath -Raw if ($rawContent -and $rawContent.Trim()) { $parsed = $rawContent | ConvertFrom-Json # Preserve existing versions if ($parsed.versions) { $config.versions = @{} foreach ($property in $parsed.versions.PSObject.Properties) { $config.versions[$property.Name] = $property.Value } } # Preserve existing runtimes if ($parsed.runtimes) { $config.runtimes = @{} foreach ($property in $parsed.runtimes.PSObject.Properties) { $config.runtimes[$property.Name] = $property.Value } } # Load existing aliases if ($parsed.alias) { $config.alias = @($parsed.alias) } } } catch { Write-Error "Failed to load configuration: $_" return } # Check if alias exists if ($config.alias -notcontains $Name) { Write-Host "Alias '$Name' not found in configuration." -ForegroundColor Yellow return } # Remove the alias $config.alias = @($config.alias | Where-Object { $_ -ne $Name }) # Save the configuration try { $json = $config | ConvertTo-Json -Depth 5 $json | Set-Content -Path $globalConfigPath -Encoding UTF8 -ErrorAction Stop Write-Host "Alias '$Name' removed successfully." -ForegroundColor Green Write-Host "Restart PowerShell for the change to take full effect." } catch { Write-Error "Failed to save configuration: $_" } } |