Public/Sync.ps1
|
# --------------------------------------------------------------------------- # Sync / Export / Import # --------------------------------------------------------------------------- function Export-TTState { <# .SYNOPSIS Export session state to a file for syncing across machines. .DESCRIPTION Exports all sessions, suspended sessions, archive, and configuration to a JSON file. Use with Import-TTState for cross-machine session sync. .PARAMETER Path Destination directory or file path. If a directory, creates a file named terminal-tracker-COMPUTERNAME.json. .PARAMETER Quiet Suppress verbose output. .EXAMPLE Export-TTState -Path 'C:\Sync\TerminalTracker' Exports state to C:\Sync\TerminalTracker\terminal-tracker-MYPC.json. .EXAMPLE Export-TTState -Path 'C:\backup\tt-state.json' Exports state to the specified file path. #> [CmdletBinding()] param( [Parameter(Mandatory)][string]$Path, [switch]$Quiet ) Initialize-TTDataStore if (Test-Path $Path -PathType Container) { $Path = Join-Path $Path "terminal-tracker-$($env:COMPUTERNAME).json" } $export = [PSCustomObject]@{ ComputerName = $env:COMPUTERNAME UserName = $env:USERNAME ExportTime = [datetime]::UtcNow.ToString('o') Sessions = @(Read-JsonFile $script:SessionsFile) Suspended = @(Read-JsonFile $script:SuspendedFile) Archive = @(Read-JsonFile $script:ArchiveFile) Config = (Get-TTConfig) } $export | ConvertTo-Json -Depth 10 | Set-Content -Path $Path -Encoding UTF8 if (-not $Quiet) { Write-Verbose "State exported to: $Path" } } function Import-TTState { <# .SYNOPSIS Import session state from a sync file. .DESCRIPTION Imports session data from a file created by Export-TTState. Can import all data or just suspended sessions for cross-machine resume workflows. .PARAMETER Path Path to the export JSON file. .PARAMETER SuspendedOnly Only import suspended sessions (for cross-machine resume). .PARAMETER MergeArchive Merge imported archive entries with existing archive. .EXAMPLE Import-TTState -Path 'C:\Sync\TerminalTracker\terminal-tracker-WORKPC.json' Imports suspended sessions from another machine's export. .EXAMPLE Import-TTState -Path 'backup.json' -MergeArchive Imports suspended sessions and merges archive entries. #> [CmdletBinding()] param( [Parameter(Mandatory)][string]$Path, [switch]$SuspendedOnly, [switch]$MergeArchive ) if (-not (Test-Path $Path)) { Write-Warning "File not found: $Path"; return } Initialize-TTDataStore $import = Get-Content -Path $Path -Raw -Encoding UTF8 | ConvertFrom-Json Write-Verbose "Importing from $($import.ComputerName) ($($import.ExportTime))..." if ($import.Suspended) { $existing = @(Read-JsonFile $script:SuspendedFile) $existingIds = $existing | ForEach-Object { $_.Id } $newSuspended = @($import.Suspended | Where-Object { $_.Id -notin $existingIds }) $merged = @($existing) + @($newSuspended) Write-JsonFile $script:SuspendedFile $merged Write-Verbose " Imported $($newSuspended.Count) suspended sessions." } if (-not $SuspendedOnly) { if ($MergeArchive -and $import.Archive) { $existing = @(Read-JsonFile $script:ArchiveFile) $merged = @($existing) + @($import.Archive) # Deduplicate by Id + ClosedTime $merged = $merged | Sort-Object { "$($_.Id)-$($_.ClosedTime)" } -Unique Write-JsonFile $script:ArchiveFile $merged Write-Verbose " Merged $($import.Archive.Count) archive entries." } } Write-Verbose "Import complete." } |