Skip to content

Commit 15e2082

Browse files
committed
Fixed LiteralPath, TLS timeout, PSFzf import, and null guards
1 parent 916fadf commit 15e2082

1 file changed

Lines changed: 19 additions & 25 deletions

File tree

Microsoft.PowerShell_profile.ps1

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -731,8 +731,8 @@ Set-Alias -Name ep -Value Edit-Profile
731731
# Create file or update its timestamp
732732
function touch($file) {
733733
if (-not $file) { Write-Error "Usage: touch <file>"; return }
734-
if (Test-Path $file) {
735-
(Get-Item $file).LastWriteTime = Get-Date
734+
if (Test-Path -LiteralPath $file) {
735+
(Get-Item -LiteralPath $file).LastWriteTime = Get-Date
736736
}
737737
else {
738738
New-Item -ItemType File -Path $file | Out-Null
@@ -915,16 +915,16 @@ function df {
915915

916916
# Find and replace text in a file
917917
function sed($file, $find, $replace) {
918-
if (-not $file -or -not (Test-Path $file)) {
918+
if (-not $file -or -not (Test-Path -LiteralPath $file)) {
919919
Write-Warning "File not found: $file"
920920
return
921921
}
922922
if ($null -eq $find -or $find -eq '') { Write-Error "Usage: sed <file> <find> <replace>"; return }
923923
if ($null -eq $replace) { $replace = '' }
924-
$content = Get-Content $file -Raw
925-
if (-not $content) { Write-Warning "File is empty: $file"; return }
924+
$content = Get-Content -LiteralPath $file -Raw
925+
if ($null -eq $content -or $content.Length -eq 0) { Write-Warning "File is empty: $file"; return }
926926
$utf8NoBom = [System.Text.UTF8Encoding]::new($false)
927-
[System.IO.File]::WriteAllText((Resolve-Path $file).Path, $content.replace("$find", $replace), $utf8NoBom)
927+
[System.IO.File]::WriteAllText((Resolve-Path -LiteralPath $file).Path, $content.replace("$find", $replace), $utf8NoBom)
928928
}
929929

930930
# Show the full path of a command
@@ -1041,6 +1041,7 @@ function file {
10411041
# Set an environment variable in the current session
10421042
function export($name, $value) {
10431043
if (-not $name) { Write-Error "Usage: export <name> <value>"; return }
1044+
if ($null -eq $value) { Write-Error "Usage: export <name> <value>"; return }
10441045
set-item -force -path "env:$name" -value $value;
10451046
}
10461047

@@ -1278,16 +1279,16 @@ function hash {
12781279
[Parameter(Mandatory)][string]$File,
12791280
[ValidateSet('MD5', 'SHA1', 'SHA256', 'SHA384', 'SHA512')][string]$Algorithm = 'SHA256'
12801281
)
1281-
if (-not (Test-Path $File)) { Write-Error "File not found: $File"; return }
1282-
(Get-FileHash -Path $File -Algorithm $Algorithm).Hash
1282+
if (-not (Test-Path -LiteralPath $File)) { Write-Error "File not found: $File"; return }
1283+
(Get-FileHash -LiteralPath $File -Algorithm $Algorithm).Hash
12831284
}
12841285

12851286
function checksum {
12861287
param(
12871288
[Parameter(Mandatory)][string]$File,
12881289
[Parameter(Mandatory)][string]$Expected
12891290
)
1290-
if (-not (Test-Path $File)) { Write-Error "File not found: $File"; return }
1291+
if (-not (Test-Path -LiteralPath $File)) { Write-Error "File not found: $File"; return }
12911292
$algo = switch ($Expected.Length) {
12921293
32 { 'MD5' }
12931294
40 { 'SHA1' }
@@ -1356,7 +1357,7 @@ function vtscan {
13561357
Write-Host 'Or run: vt init' -ForegroundColor Yellow
13571358
return
13581359
}
1359-
$resolved = Resolve-Path $FilePath -ErrorAction SilentlyContinue
1360+
$resolved = Resolve-Path -LiteralPath $FilePath -ErrorAction SilentlyContinue
13601361
if (-not $resolved) { Write-Error "File not found: $FilePath"; return }
13611362
$file = Get-Item $resolved
13621363
$sizeMB = [math]::Round($file.Length / 1MB, 2)
@@ -2118,7 +2119,10 @@ function tlscert {
21182119
throw "Connection to ${Domain}:${Port} timed out after 5 seconds"
21192120
}
21202121
$tcp.EndConnect($async)
2121-
$ssl = New-Object System.Net.Security.SslStream($tcp.GetStream(), $false, {$true})
2122+
$stream = $tcp.GetStream()
2123+
$stream.ReadTimeout = 10000
2124+
$stream.WriteTimeout = 10000
2125+
$ssl = New-Object System.Net.Security.SslStream($stream, $false, {$true})
21222126
$ssl.AuthenticateAsClient($Domain)
21232127
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($ssl.RemoteCertificate)
21242128
$daysLeft = [math]::Floor(($cert.NotAfter - (Get-Date)).TotalDays)
@@ -2342,28 +2346,18 @@ if ($isInteractive -and (Get-Module PSReadLine)) {
23422346
Set-PSReadLineKeyHandler -Chord 'Alt+v' -BriefDescription SmartPaste -Description 'Paste clipboard as one block into prompt' -ScriptBlock $smartPasteHandler
23432347

23442348
# fzf integration via PSFzf (fuzzy history search on Ctrl+R, file finder on Ctrl+T)
2345-
# Lazy-loaded: PSFzf is imported on first Ctrl+R/Ctrl+T press, not at profile load,
2346-
# to prevent fzf from launching during terminal startup (VS Code shell integration, etc.)
23472349
if (Get-Command fzf -ErrorAction SilentlyContinue) {
23482350
if (-not $env:FZF_DEFAULT_COMMAND -and (Get-Command rg -ErrorAction SilentlyContinue)) {
23492351
$env:FZF_DEFAULT_COMMAND = 'rg --files --hidden --glob "!.git"'
23502352
}
23512353
if (-not $env:FZF_DEFAULT_OPTS) {
23522354
$env:FZF_DEFAULT_OPTS = '--height=40% --layout=reverse'
23532355
}
2354-
Set-PSReadLineKeyHandler -Chord 'Ctrl+r' -BriefDescription FzfHistory -Description 'Fuzzy history search (fzf)' -ScriptBlock {
2355-
if (-not (Get-Module PSFzf)) {
2356-
try { Import-Module PSFzf -ErrorAction Stop }
2357-
catch { [Microsoft.PowerShell.PSConsoleReadLine]::Ding(); return }
2356+
if (Get-Module -ListAvailable -Name PSFzf) {
2357+
Import-Module PSFzf -ErrorAction SilentlyContinue
2358+
if (Get-Module PSFzf) {
2359+
Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+t' -PSReadlineChordReverseHistory 'Ctrl+r'
23582360
}
2359-
Invoke-FzfPsReadlineHandlerHistory
2360-
}
2361-
Set-PSReadLineKeyHandler -Chord 'Ctrl+t' -BriefDescription FzfFileFind -Description 'Fuzzy file finder (fzf)' -ScriptBlock {
2362-
if (-not (Get-Module PSFzf)) {
2363-
try { Import-Module PSFzf -ErrorAction Stop }
2364-
catch { [Microsoft.PowerShell.PSConsoleReadLine]::Ding(); return }
2365-
}
2366-
Invoke-FzfPsReadlineHandlerProvider
23672361
}
23682362
}
23692363

0 commit comments

Comments
 (0)