Skip to content

Commit 8205080

Browse files
committed
add pass find. pass insert bugfix
1 parent f3b7caf commit 8205080

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

pass.psm1

+22-20
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,16 @@ function Invoke-PassFind {
4444
[CmdletBinding()]
4545
param (
4646
[Parameter(Mandatory, Position = 0)]
47-
[string]$PassName
47+
[string]$Pattern
4848
)
4949
$PassStorePath = Get-PasswordStore
50-
$LeafPath = Split-Path -Leaf $PassName
5150
$LikeOptions = @{
5251
Recurse = $true
5352
}
5453

5554
$Like = (Get-ChildItem -Path $PassStorePath @ExcludeGit |
5655
Get-ChildItem @LikeOptions |
57-
Where-Object { $_.FullName | Select-String -Pattern $LeafPath } |
56+
Where-Object { $_.FullName | Select-String -Pattern $Pattern } |
5857
ForEach-Object { Get-PassName $_ $PassStorePath })
5958
return $Like
6059
}
@@ -78,7 +77,7 @@ function Invoke-PassGenerate {
7877
)
7978
$PassPath = Get-RealPath $PassName -MakeParentDirectoy $true
8079
if ((Test-Path $PassPath) -and (-not $Force)) {
81-
$Confirmation = Read-Host "An entry already exists for $RelativePath. Overwrite it? [y/N] "
80+
$Confirmation = Read-Host "An entry already exists for $PassName. Overwrite it? [y/N] "
8281
if ($Confirmation -ne 'y') {
8382
throw 'User interrupted.'
8483
}
@@ -408,6 +407,7 @@ function Out-TreeInternal {
408407
Param(
409408
[System.IO.FileSystemInfo] $Info,
410409
[int] $Depth,
410+
[string] $Format,
411411
[switch] $Last
412412
)
413413
if (-not $Info) {
@@ -417,36 +417,37 @@ function Out-TreeInternal {
417417
Write-Debug $Info
418418

419419
if ($Depth -eq 0) {
420-
$Format = '{0}'
420+
$DisplayFormat = '{0}'
421421
}
422422
else {
423423
$CurrentLeafFormat = if ($Last) { $LastLeafFormat } else { $LeafFormat }
424-
$Format = $TrunkFormat * ($Depth - 1) + $CurrentLeafFormat + '{0}'
424+
$DisplayFormat = $Format + $CurrentLeafFormat + '{0}'
425+
$CurrentTrunkFormat = if ($Last) { " " } else { "| " }
426+
$Format = $Format + $CurrentTrunkFormat
427+
425428
}
426429

427430
$KeyName = if ($Info.Name -match '(?<path>.*).gpg') { $Matches['path'] } else { $Info.Name }
428431
if (-not ($KeyName -like '.*' )) {
429432
Write-Debug $KeyName
430-
Write-Output ($Format -f $KeyName)
433+
Write-Output ($DisplayFormat -f $KeyName)
431434
}
432435

433436
If (Test-Path -Path $Info.FullName -PathType Container) {
434-
$Children = (Get-ChildItem @ExcludeGit $Info.FullName)
435-
if ($Children -is [object[]]) {
436-
switch ($Children.Length) {
437-
0 { return; }
438-
1 { Out-TreeInternal -Info $Children[0] -Depth ($Depth + 1) -Last }
439-
Default {
440-
foreach ($child in $Children[0..($Children.Length - 2)]) {
441-
Out-TreeInternal -Info $child -Depth ($Depth + 1)
442-
}
443-
Out-TreeInternal -Info $Children[$Children.Length - 1] -Depth ($Depth + 1) -Last
437+
$Children = @(Get-ChildItem @ExcludeGit $Info.FullName)
438+
switch ($Children.Length) {
439+
0 { break; }
440+
1 {
441+
Out-TreeInternal -Info $Children[0] -Depth ($Depth + 1) -Format $Format -Last;
442+
break;
443+
}
444+
Default {
445+
foreach ($child in $Children[0..($Children.Length - 2)]) {
446+
Out-TreeInternal -Info $child -Depth ($Depth + 1) -Format $Format
444447
}
448+
Out-TreeInternal -Info $Children[$Children.Length - 1] -Depth ($Depth + 1) -Format $Format -Last
445449
}
446450
}
447-
else {
448-
Out-TreeInternal -Info $Children -Depth ($Depth + 1) -Last
449-
}
450451
}
451452
}
452453

@@ -665,6 +666,7 @@ $ExportSubcommandSplat = @{
665666
'Invoke-PassGenerate'
666667
'Invoke-PassInsert'
667668
'Invoke-PassList'
669+
'Invoke-PassFind'
668670
'Invoke-PassGit'
669671
)
670672
Alias = @(

tests/Pass.Test.ps1

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ function Resolve-Error ($ErrorRecord = $Error[0]) {
1212

1313
Import-Module $PSScriptRoot/../Pass.psm1 -PassThru
1414
$env:PASSWORD_STORE_DIR = "$PSScriptRoot/password-store-test/"
15-
Remove-Item -Recurse $env:PASSWORD_STORE_DIR -Force
1615

16+
if (Test-Path $env:PASSWORD_STORE_DIR) {
17+
Remove-Item -Recurse $env:PASSWORD_STORE_DIR -Force
18+
}
1719
try {
1820
Invoke-PassInit -GpgId "[email protected]"
1921
Invoke-PassGit init
@@ -23,11 +25,13 @@ try {
2325
Invoke-PassInsert "example.com/[email protected]" "1234"
2426
Test-Path "$env:PASSWORD_STORE_DIR/example.com/[email protected]"
2527
Invoke-PassShow "example.com/[email protected]"
28+
Invoke-PassInsert "example.com/[email protected]" "1234"
2629

2730
Invoke-PassGenerate "example2.com/[email protected]" 16
2831
Test-Path "$env:PASSWORD_STORE_DIR/example2.com/[email protected]"
32+
Invoke-PassGenerate "example2.com/[email protected]" 16
2933
Invoke-PassShow "example2.com/[email protected]"
30-
34+
Invoke-PassFind "example"
3135
}
3236
catch {
3337
Resolve-Error $Error

0 commit comments

Comments
 (0)