Public/Remove-Budget.ps1
|
function Remove-Budget { <# .SYNOPSIS Removes a budget. .DESCRIPTION Deletes a budget directory and all its contents. Cannot remove the active budget without confirmation. .PARAMETER Name The name of the budget to remove. .PARAMETER WorkspacePath Optional custom workspace path. .EXAMPLE Remove-Budget -Name "old-project" Removes a budget (with confirmation) .EXAMPLE Remove-Budget -Name "old-project" -Confirm:$false Force removes a budget without confirmation .OUTPUTS None #> [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')] param( [Parameter(Mandatory, ValueFromPipelineByPropertyName)] [string]$Name, [Parameter()] [string]$WorkspacePath ) # Check if workspace is initialized if (-not (Test-WorkspaceInitialized -WorkspacePath $WorkspacePath)) { Write-Error "Budget workspace not initialized. Run Initialize-BudgetWorkspace first." return } # Get workspace path if (-not $WorkspacePath) { $WorkspacePath = Get-WorkspacePath } # Verify budget exists $budgetPath = Get-BudgetPath -BudgetName $Name -WorkspacePath $WorkspacePath if (-not (Test-Path $budgetPath)) { Write-Error "Budget '$Name' not found" return } # Check if removing active budget $preferences = Get-Preferences -WorkspacePath $WorkspacePath $isActive = ($preferences.activeBudget -eq $Name) $isDefault = ($preferences.defaultBudget -eq $Name) if ($isActive) { Write-Warning "This is the currently active budget" } if ($PSCmdlet.ShouldProcess($Name, "Remove budget and all its data")) { # Remove budget directory Remove-Item -Path $budgetPath -Recurse -Force Write-Verbose "Removed budget directory: $budgetPath" # Clear from preferences if it was active or default if ($isActive -or $isDefault) { if ($isActive) { $preferences.activeBudget = $null } if ($isDefault) { $preferences.defaultBudget = $null } Set-Preferences -Preferences $preferences -WorkspacePath $WorkspacePath | Out-Null Write-Verbose "Cleared budget from preferences" } Write-Host "Removed budget: $Name" -ForegroundColor Green if ($isActive) { Write-Host "No active budget set. Use Set-ActiveBudget to select one." -ForegroundColor Yellow } } } |