Files
SceneMarshal/scripts/remote-common.ps1
T

90 lines
3.5 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#Requires -Version 7.0
<#
.SYNOPSIS
media-rename / media-archive 共用的 SSH 与路径翻译工具(由调用方 dot-source 引入)。
#>
$script:Remote = 'ali@10.10.1.202'
$script:QuarkDir = '/hdd/sda1/Quark-Down'
$script:SshBaseOpts = @('-o', 'BatchMode=yes', '-o', 'ConnectTimeout=10')
function Assert-SshAgent {
if (-not $env:SSH_AUTH_SOCK) {
$pipe = '\\.\pipe\openssh-ssh-agent'
if (Test-Path -LiteralPath $pipe) { $env:SSH_AUTH_SOCK = $pipe }
}
if (-not $env:SSH_AUTH_SOCK) { throw 'SSH agent 未就绪:请启动 Bitwarden 并启用 SSH agent。' }
$out = & ssh-add -l 2>&1
if ($LASTEXITCODE -ne 0) {
$hint = if ($LASTEXITCODE -eq 1) { 'agent 里没有可用密钥' } else { '连不上 agent 管道' }
throw "SSH agent 不可用($hint):$out`n请先解锁 Bitwarden。"
}
}
function ConvertTo-RemotePath([string]$Path) {
$p = ($Path -replace '\\', '/').Trim()
if ($p -match '^//10\.10\.1\.202/n100-sda1/?(.*)$') {
$rest = $Matches[1]
return $(if ($rest) { "/hdd/sda1/$rest" } else { '/hdd/sda1' })
}
if ($p -match '^//10\.10\.1\.202/n100-ali/?(.*)$') {
$rest = $Matches[1]
return $(if ($rest) { "/home/ali/$rest" } else { '/home/ali' })
}
if ($p.StartsWith('//')) { throw "不认识的共享路径: $Path" }
if ($p -match '^/') { return $p }
if ($p -match '^[A-Za-z]:/') {
if ($p -match '^[Yy]:/(.*)$') { return '/hdd/sda1/' + $Matches[1] }
throw "只有 Y: 映射到 N100(Y:\ = \\10.10.1.202\n100-sda1 = /hdd/sda1)。当前路径: $Path"
}
return $null
}
function ConvertTo-RemoteArg([string]$Value) {
"'" + ($Value -replace "'", "'\''") + "'"
}
function Invoke-RemoteQuery([string]$Command) {
$out = & ssh @SshBaseOpts $script:Remote $Command 2>&1
if ($LASTEXITCODE -ne 0) { throw "远端查询失败: $Command`n$out" }
return $out
}
function Test-IsPathLike([string]$Value) {
return ($Value -match '^[A-Za-z]:' -or $Value.StartsWith('\\') -or $Value.StartsWith('/') -or
$Value.Contains('/') -or $Value.Contains('\'))
}
function Resolve-RemoteTarget([string]$Name) {
if ($Name -eq 'latest') {
# 取最近改动的目录;排除 _reports / Finish / 隐藏目录这类非下载目录
$raw = (Invoke-RemoteQuery "find $script:QuarkDir -maxdepth 1 -mindepth 1 -type d -not -name '.*' -not -name '_*' -not -name Finish -printf '%T@ %p\n' | sort -rn | head -1 | cut -d' ' -f2-") -join "`n"
$latest = $raw.Trim()
if (-not $latest) { throw "$script:QuarkDir 下没有任何可处理目录" }
return $latest
}
$candidate = "$script:QuarkDir/$Name"
$exists = (Invoke-RemoteQuery "test -e $(ConvertTo-RemoteArg $candidate) && echo yes || echo no") -join ''
if ($exists.Trim() -eq 'yes') { return $candidate }
$dirs = @(Invoke-RemoteQuery "ls -1 $script:QuarkDir")
$hits = @($dirs | Where-Object { $_ -like "*$Name*" })
if ($hits.Count -eq 1) { return "$script:QuarkDir/$($hits[0])" }
if ($hits.Count -gt 1) {
throw "『$Name』匹配到多个目录,请指定更完整的名称或直接用路径:`n " + ($hits -join "`n ")
}
throw "在 $script:QuarkDir 下找不到『$Name』。可用 latest,或给出完整路径。"
}
function Resolve-TargetToken([string]$Token) {
if (Test-IsPathLike $Token) {
$translated = ConvertTo-RemotePath $Token
if (-not $translated) { throw "无法识别的路径: $Token" }
return $translated
}
return Resolve-RemoteTarget $Token
}