[CmdletBinding()] param( [ValidateSet('Add', 'List', 'Search', 'Open', 'Remove')] [string]$Action = 'List', [string]$Title, [string]$Url, [string]$Tags, [string]$Query, [int]$Id, [string]$Data = (Join-Path $env:APPDATA 'Productivity\bookmarks.json'), [switch]$Force ) $ErrorActionPreference = 'Stop' function Read-Bookmarks { param([string]$Path) if (-not (Test-Path -LiteralPath $Path)) { return @() } $raw = Get-Content -LiteralPath $Path -Raw -Encoding UTF8 if ([string]::IsNullOrWhiteSpace($raw)) { return @() } $items = $raw | ConvertFrom-Json if ($null -eq $items) { return @() } return @($items) } function Save-Bookmarks { param([string]$Path, [object[]]$Items) $directory = Split-Path -LiteralPath $Path if ($directory -and -not (Test-Path -LiteralPath $directory)) { New-Item -ItemType Directory -Path $directory -Force | Out-Null } if (Test-Path -LiteralPath $Path) { Copy-Item -LiteralPath $Path -Destination ($Path + '.bak') -Force } $json = ConvertTo-Json -InputObject @($Items) -Depth 5 Set-Content -LiteralPath $Path -Value $json -Encoding UTF8 } function Assert-WebUrl { param([string]$Value) $uri = $null if (-not [uri]::TryCreate($Value, [UriKind]::Absolute, [ref]$uri)) { throw "Некорректный адрес: $Value" } if ($uri.Scheme -notin @('http', 'https')) { throw 'Разрешены только адреса http и https.' } return $uri.AbsoluteUri } $items = Read-Bookmarks -Path $Data switch ($Action) { 'Add' { if ([string]::IsNullOrWhiteSpace($Title)) { throw 'Укажите название через -Title.' } if ([string]::IsNullOrWhiteSpace($Url)) { throw 'Укажите адрес через -Url.' } $cleanUrl = Assert-WebUrl -Value $Url $nextId = 1 if ($items.Count -gt 0) { $nextId = ([int]($items | Measure-Object -Property id -Maximum).Maximum) + 1 } $tagList = @() if (-not [string]::IsNullOrWhiteSpace($Tags)) { $tagList = @($Tags.Split(',') | ForEach-Object { $_.Trim() } | Where-Object { $_ }) } $entry = [pscustomobject]@{ id = $nextId title = $Title url = $cleanUrl tags = @($tagList) added = (Get-Date).ToString('s') } $items = @($items) + $entry Save-Bookmarks -Path $Data -Items $items Write-Host "Добавлено #${nextId}: $Title" } 'List' { if ($items.Count -eq 0) { Write-Host 'Закладок нет.'; break } $items | Sort-Object -Property id | ForEach-Object { Write-Host ("#{0} {1} - {2} [{3}]" -f $_.id, $_.title, $_.url, ($_.tags -join ', ')) } } 'Search' { if ([string]::IsNullOrWhiteSpace($Query)) { throw 'Укажите строку поиска через -Query.' } $found = @($items | Where-Object { $_.title -like "*$Query*" -or $_.url -like "*$Query*" -or ($_.tags -join ' ') -like "*$Query*" }) if ($found.Count -eq 0) { Write-Host 'Совпадений нет.'; break } $found | ForEach-Object { Write-Host ("#{0} {1} - {2}" -f $_.id, $_.title, $_.url) } } 'Open' { $target = $null if ($Id -gt 0) { $target = $items | Where-Object { [int]$_.id -eq $Id } | Select-Object -First 1 } elseif (-not [string]::IsNullOrWhiteSpace($Query)) { $target = $items | Where-Object { $_.title -like "*$Query*" } | Select-Object -First 1 } if ($null -eq $target) { throw 'Закладка не найдена. Укажите -Id или -Query.' } $safeUrl = Assert-WebUrl -Value $target.url Start-Process -FilePath $safeUrl Write-Host "Открыто: $($target.title)" } 'Remove' { if ($Id -le 0) { throw 'Укажите номер закладки через -Id.' } if (-not $Force) { throw 'Удаление меняет файл закладок. Добавьте -Force для подтверждения.' } $kept = @($items | Where-Object { [int]$_.id -ne $Id }) if ($kept.Count -eq $items.Count) { throw "Закладка #$Id не найдена." } Save-Bookmarks -Path $Data -Items $kept Write-Host "Удалено: #$Id" } }