-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoPS.psm1
1462 lines (1148 loc) · 38.4 KB
/
GoPS.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using namespace System.Collections.Generic
<#
.oooooo. ooooooooo. .oooooo..o
d8P' `Y8b `888 `Y88. d8P' `Y8
888 .ooooo. 888 .d88' Y88bo.
888 d88' `88b 888ooo88P' `"Y8888o.
888 ooooo 888 888 888 `"Y88b
`88. .88' 888 888 888 oo .d8P
`Y8bood8P' `Y8bod8P' o888o 8""88888P'
#>
<#
.Description
Jump and easily manage a database file of jump paths.
#>
param (
# This is the default navigation file path to be used if a config file is missing. Default: $HOME/.navdb
$DefaultNavigationFile = "$HOME/.gops"
)
#region Setup ------------------------------------------------------------------
<#
-_-/ ,
(_ / ||
(_ --_ _-_ =||= \\ \\ -_-_
--_ ) || \\ || || || || \\
_/ )) ||/ || || || || ||
(_-_- \\,/ \\, \\/\\ ||-'
|/
'
#>
Set-Variable ErrorActionPreference Stop
Set-Variable ModuleRoot Gops -Option ReadOnly
$ResourceFile = @{
BindingVariable = 'Message'
BaseDirectory = $PSScriptRoot
FileName = $ModuleRoot + '.Resources.psd1'
}
$ConfigFile = @{
BindingVariable = 'Config'
BaseDirectory = $PSScriptRoot
FileName = $ModuleRoot + '.Config.psd1'
}
# Try to import the resource file
try {
Import-LocalizedData @ResourceFile
}
catch {
# Uh-oh. The module is likely broken if this file cannot be found.
Import-LocalizedData @ResourceFile -UICulture en-US
}
data ConfigProperties {
'DefaultNavigationFile'
'CommandAlias'
}
data CommandAliasProperties {
'Add-NavigationEntry'
'Export-NavigationEntry'
'Get-DefaultNavigationFile'
'Get-GoPSStack'
'Get-JumpHistory'
'Get-NavigationEntry'
'Invoke-Back'
'Invoke-GoPS'
'Invoke-Last'
'Invoke-Up'
'New-NavigationFile'
'Remove-NavigationEntry'
'Set-DefaultNavigationFile'
'Update-NavigationDatabase'
}
data DefaultConfig -SupportedCommand Get-Variable {
@{
DefaultNavigationFile = Get-Variable DefaultNavigationFile -ValueOnly
CommandAlias = @{}
}
}
# Try to import the config file
try {
Import-LocalizedData @ConfigFile
$xs = [HashSet[string]] [string[]] $Config.Keys
$ys = [HashSet[string]] $ConfigProperties
if (!$xs.IsSubsetOf($ys)) {
[void] $xs.ExceptWith($ys)
throw ($Message.TerminatingError.InvalidConfig -f ($xs -join ', '), ($ConfigProperties -join ', '))
}
if ($Config.ContainsKey('CommandAlias')) {
$xs = [HashSet[string]] [string[]] $Config.CommandAlias.Keys
$ys = [HashSet[string]] $CommandAliasProperties
if (!$xs.IsSubsetOf($ys)) {
[void] $xs.ExceptWith($ys)
throw ($Message.TerminatingError.InvalidCommandAlias -f
($xs -join ', '),
($CommandAliasProperties -join "`n"))
}
}
}
catch [System.Management.Automation.ItemNotFoundException] {
Write-Warning $Message.Warning.ConfigFileNotFound
$Config = $DefaultConfig
}
catch {
throw $_.Exception
}
#endregion
#region Classes ----------------------------------------------------------------
<#
,- _~. ,,
(' /| || _
(( || || < \, _-_, _-_, _-_ _-_,
(( || || /-|| ||_. ||_. || \\ ||_.
( / | || (( || ~ || ~ || ||/ ~ ||
-____- \\ \/\\ ,-_- ,-_- \\,/ ,-_-
#>
class Entry {
[string] $Token
[string] $Path
[bool] $IsValid
}
class Database {
[List[Entry]] $EntryList
[HashSet[string]] $TokenSet
}
# A nice, human-readable Entry list
class JumpStack {
[int] $Jump
[string] $Name
[string] $FullName
}
#endregion
#region helper -----------------------------------------------------------------
<#
_-_- ,,
/, ||
|| __ _-_ || -_-_ _-_ ,._-_
~||- - || \\ || || \\ || \\ ||
||===|| ||/ || || || ||/ ||
( \_, | \\,/ \\ ||-' \\,/ \\,
` |/
'
#>
function Invoke-Ternary {
<#
.Description
A nice internal ternary function.
Helps improve script readability by removing the need to use PowerShell array logic for trivial if-else
expressions.
Array logic ternary expressions can be confusing syntax for PowerShell beginners.
Array ternary logic example: ('false case', 'true case')[$conditional]
bool -> scriptblock -> scriptblock -> () #>
param (
# The conditional statement. Must evaluate to a boolean.
[bool] $Conditional
,
# A scriptblock to invoke when the conditional parameter evaluates to True.
[scriptblock] $OnTrue
,
# A scriptblock to invoke when the conditional parameter evaluates to False.
[scriptblock] $OnFalse
)
if ($Conditional) {
& $OnTrue
}
else {
& $OnFalse
}
}
function New-Entry {
<#
.Description
Creates a new Entry object.
string -> string -> Entry #>
[CmdletBinding()] # Makes variables easier to get from pipeline
param (
# Token Name.
[Parameter(ValueFromPipelineByPropertyName)]
[string] $Token
,
# Path Name.
[Parameter(ValueFromPipelineByPropertyName)]
[string] $Path
)
$x = $Path -as [System.IO.DirectoryInfo]
[Entry] @{
Token = $Token
Path = $Path
IsValid = $x.Exists
}
}
function New-Database {
<#
.Description
Creates a new Database object.
() -> Database #>
[Database] @{
EntryList = @()
TokenSet = @()
}
}
filter Add-Entry ($x) {
<#
.Description
Adds a piped Entry to a Database object if the Entry does not have a duplicate Token property.
If the Token property of the Entrty object already exists in the Database, the Entry is ignored.
A successful operation modifies the Database object.
A failed operation emits an error.
seq<Entry> -> Database -> () #>
if ($x.TokenSet.Add($_.Token)) {
[void] $x.EntryList.Add($_)
}
else {
Write-Error ($Message.Error.AddEntry -f $_.Token)
}
}
filter ConvertFrom-Database {
<#
.Description
Converts a Database object to an Entry array.
Database -> Entry[] #>
$_.EntryList.ToArray()
}
function New-JumpStack {
<#
.Description
Creates a new JumpStack object from a piped DirectoryInfo object.
seq<DirectoryInfo> -> seq<JumpStack> #>
begin {
$c = 1
}
process {
[JumpStack] @{
Jump = $c++
Name = $_.Name
FullName = $_.FullName
}
}
}
#endregion
#region Internal ---------------------------------------------------------------
<#
_-_, , ,,
// || _ ||
|| \\/\\ =||= _-_ ,._-_ \\/\\ < \, ||
~|| || || || || \\ || || || /-|| ||
|| || || || ||/ || || || (( || ||
_-_, \\ \\ \\, \\,/ \\, \\ \\ \/\\ \\
#>
# Todo: Make this a public cmdlet @endowdly
function Import-NavigationFile ($s) {
<#
.Description
Imports the data from a navigation file and returns the Database object.
If no file is at the given path, emits a friendly information string and returns an empty Database object.
string -> Database #>
if (!(Test-Path $s)) {
Write-Warning ($Message.Warning.NoNavFile -f $s)
return New-Database
}
$x = New-Database
[Entry[]] (Import-Csv $s) |
Add-Entry $x
$x
}
function Push-Path ($s) {
<#
.Description
If the given path is a valid directory:
- Pushes the current path onto module PathStack
- Sets the path to the valid directory
- Records the new path onto the provider PathStack
Does nothing otherwise.
string -> () #>
$s1 = Convert-Path $s
$x = [System.IO.DirectoryInfo] $s1
if ($x.Exists) {
$GoPS.PathStack.Push($GoPS.LastPath)
Push-Location $x.FullName -StackName GoPS
}
}
$setAlias = {
if ($_.Value -eq '') {
return
}
Set-Alias -Value $_.Key -Name $_.Value -Scope Script
}
# Module variables go here
$GoPS = @{
DefaultPath = $Config.DefaultNavigationFile
Database = New-Database
LastPath = $PWD.Path
PathStack = [Stack[System.IO.DirectoryInfo]] @()
}
#endregion
#region gatekeeping ------------------------------------------------------------
<#
__ ,
,-| ~ , ,,
('||/__, _ || || ' _
(( ||| | < \, =||= _-_ ||/\ _-_ _-_ -_-_ \\ \\/\\ / \\
(( |||==| /-|| || || \\ ||_< || \\ || \\ || \\ || || || || ||
( / | , (( || || ||/ || | ||/ ||/ || || || || || || ||
-____/ \/\\ \\, \\,/ \\,\ \\,/ \\,/ ||-' \\ \\ \\ \\_-|
|/ / \
' '----`
#>
function Assert-Path ($s) {
<#
.Description
Throw on Test-Path failure.
string -> () #>
if (!(Test-Path $s)) {
throw ($Message.TerminatingError.NavFileInvalid -f $s)
}
$true
}
function Assert-PositiveNumber ($d) {
<#
.Description
Throw if d is not a positive number. #>
if ($d -lt 0) {
throw ($Message.TerminatingError.NotAPositiveNumber -f $d)
}
$true
}
#endregion
#region Public -----------------------------------------------------------------
<#
-__ /\\ ,, ,,
|| \\ || || '
/||__|| \\ \\ ||/|, || \\ _-_
\||__|| || || || || || || ||
|| |, || || || |' || || ||
_-||-_/ \\/\\ \\/ \\ \\ \\,/
||
#>
# Todo: Change output to FileInfo @endowdly @low
function New-NavigationFile {
<#
.Synopsis
Creates a new navigation database file.
.Description
Creates a home Entry and exports to a CSV file.
This is considered a 'bare' navigation file.
Will not overwrite an existing file unless the Force switch is used.
The home Entry is simply the user's Home directory, derived from the Home automatic variable.
A navigation file is a CSV file that flat-packs Entry objects.
.Example
New-NavigationFile
Creates a new navigation file at the DefaultPath, which is normally '~/.gops', if it does not exist.
.Example
New-NavigationFile -Path $FilePath
Assuming FilePath is a valid location and does not exist, creates a new navigation file.
.Example
New-NavigationFile -Force
Overwrites the navigation file, if it exists, at the DefaultPath with a bare navigation file.
.Example
Join-Path $HOME .gops2 | New-NavigationFile -Force
Overwrites the navigation file, if it exists, at the input passed by `Join-Path`.
.Inputs
System.String
.Notes
string -> bool -> ()
#>
[CmdletBinding(SupportsShouldProcess)]
param(
# Specifies a path to database file. Default: Module DefaultPath
[Parameter(
ValueFromPipeline,
ValueFromPipelineByPropertyName)]
[string] $Path = $GoPS.DefaultPath
,
# Forces creation of a new navigation file.
[switch] $Force
)
$NoClobber = !$Force
$x = New-Database
if ($PSCmdlet.ShouldProcess($Path, $Message.ShouldProcess.NewNavigationFile)) {
New-Entry Home $HOME |
Add-Entry $x
$x |
ConvertFrom-Database |
Export-Csv -Path $Path -NoClobber:$NoClobber -NoTypeInformation
Write-Verbose ($Message.Verbose.NewNavigationFile -f $Path)
}
}
function Get-DefaultNavigationFile {
<#
.Synopsis
Returns the default path of the navigation file currently set.
.Description
Returns the default path of the navigation file currently set.
.Example
Get-DefaultNavigationFile
The only way to use it.
.Link
Set-DefaultNavigationFile
.Outputs
System.Management.Automation.PathInfo
.Notes
() -> PathInfo
#>
Resolve-Path $GoPS.DefaultPath
}
function Set-DefaultNavigationFile {
<#
.Synopsis
Sets the default navigation file path.
.Description
Sets the default navigation file path for GoPS.
.Example
Set-DefaultNavigationFile $FilePath
Sets the default navigation filelocation to FilePath, if it exists.
.Example
$FilePath | Set-DefaultNavigationFile
Sets the default navigation filelocation to FilePath, if it exists.
.Link
Get-DefaultNavigationFile
.Inputs
System.String
.Notes
string -> ()
#>
[CmdletBinding(
SupportsShouldProcess,
ConfirmImpact = 'Low')]
param (
# Specifies a path to database file.
[Parameter(
ValueFromPipeline,
ValueFromPipelineByPropertyName)]
[ValidateScript({ Assert-Path $_ })]
[string] $Path
)
process {
if ($PSCmdlet.ShouldProcess($Path, $Message.ShouldProcess.SetDefaultNavigationFile)) {
if ($Path -eq $GoPS.DefaultPath) {
return
}
$GoPS.DefaultPath = $Path
Write-Verbose ($Message.Verbose.SetDefaultNavigationFile -f $Path)
}
}
}
# Review: Consider an Append switch parameter to add to a file @endowdly @low
function Export-NavigationEntry {
<#
.Synopsis
Export an Entry object to a navigation file.
.Description
Export an Entry object to a navigation file.
If a path is not given, defaults to the default navigation file.
If no Entry objects are provided, exports the objects currently loaded in memory.
.Example
Export-NavigationEntry
Will export the Entry objects in memory to the default navigation file path, if it exists.
.Example
Export-NavigationEntry -Path $FilePath
Will export the Entry objects in memory to the path at FilePath, if it exists.
.Example
Get-NavigationEntry this that | Export-NavigationEntry
Will export the Entry objects with Token properties 'this' and 'that' to the default navifation file.
.Example
Get-NavigationEntry this that | Export-NavigationEntry -Path $FilePath
Will export the Entry objects with Token properties 'this' and 'that' to the path at FilePath, if it exists.
.Link
Add-NavigationEntry
.Link
Get-NavigationEntry
.Link
Remove-NavigationEntry
.Inputs
Entry[]
.Notes
Entry[] -> string? -> ()
#>
[CmdletBinding(
SupportsShouldProcess,
ConfirmImpact = 'Medium')]
[Alias('Export-NavigationDatabase')] # ! Deprecated
param(
# The Entry objects to export. Default: Entry objects in loaded Database
[Parameter(ValueFromPipeline)]
[Entry[]] $InputObject = $GoPS.Database.EntryList.ToArray()
,
# Specifies a path to database file. Default: Module DefaultPath
[Parameter(Position = 0)]
[ValidateScript({ Assert-Path $_ })]
[Alias('PSPath')]
[string] $Path = $GoPS.DefaultPath
,
[switch] $Append
)
begin {
if ($MyInvocation.InvocationName -eq 'Export-NavigationDatabase') {
Write-Warning $Message.Warning.ExportNavigationDatabase
}
}
process {
<# * Why ForEach is not used on each incoming array:
Inteded behavior is for the navigation file to be wholly replaced by the incoming Entry array.
If you use ForEach on the array, you may only export the last Entry object in the array.
I would rather only export the last array passed. #>
if ($PSCmdlet.ShouldProcess($Path, $Message.ShouldProcess.ExportNavigationEntry)) {
$InputObject |
Export-Csv -Path $Path -NoTypeInformation -Append:$Append
}
}
}
# Review: Consider an Append switch parameter @endowdly @low
function Update-NavigationDatabase {
<#
.Synopsis
Update the Database in memory with the contents of a navigation file.
.Description
Update the Database in memory with the contents of a navigation file.
.Example
Update-NavigationDatabase
Will change the internal Database object to the Entry array contained in the default file, if valid.
.Example
Update-NavigationDatabase -Path $FilePath
Will change the internal Database object to the Entry array contained in the file at FilePath, if valid.
.Example
Join-Path $Home .gops2 | Update-NavigationDatabase
Will change the internal Database object to the Entry array contained in the incoming path, if valid.
.Inputs
System.String
.Notes
string -> ()
The noun is correct. It is a little odd as the rest of its close functions deal with Entry objects.
However, this is the only function provided that allws the user to affect the internal Database object.
#>
[CmdletBinding(
SupportsShouldProcess,
ConfirmImpact = 'Low')]
param(
# Specifies a path to database file. Default: Module DefaultPath
[Parameter(
ValueFromPipeline,
ValueFromPipelineByPropertyName)]
[ValidateScript({ Assert-Path $_ })]
[Alias('PSPath')]
[string[]] $Path = $GoPS.DefaultPath
)
begin {
$ls = [List[Entry]] @()
# adds each item to a specified list
$f = {
$ls, $null = $args
[void] $ls.Add($_)
}
# string -> Entry[]
$g = {
Import-NavigationFile $_ | ConvertFrom-Database
}
$x = New-Database
}
process {
if ($PSCmdlet.ShouldProcess($Path, $Message.ShouldProcess.UpdateNavigationDatabase)) {
$Path.ForEach($g).ForEach($f, $ls)
}
}
end {
$ls | Add-Entry $x
$GoPS.Database = $x
}
}
function Add-NavigationEntry {
<#
.Synopsis
Adds an Entry object to the Database.
.Description
Adds an Entry object to the Database.
Think of an Entry object like a bookmark.
Each has a Token property and a JumpPath property.
The Token is the users chosen short name or bookmark name for each JumpPath.
The JumpPath property is a directory in the file-system they likely visit often.
The Database is an internal memory collection that validates and stores Entry objects.
It does not allow Entry objects with duplicate Tokens.
However, it will allow many Entry objects that point to the same JumpPath.
JumpPath can point to paths that do not yet exist.
Returns the Entry object added unless the Silent switch parameter is used.
.Example
Add-NavigationEntry -Token docs -Path ~/Documents
Adds an Entry with the Token property 'docs' pointing to the user's Documents directory.
.Example
Add-NavigationEntry -Token here
Adds an Entry with the Token property 'here' pointing to the current working directory.
.Example
Add-NavigationEntry -Token there -Path $there -Silent
Adds an Entry with the Token property 'there' pointing to the path at there.
Does return the Entry object created and added.
.Link
Get-NavigationEntry
.Link
Remove-NavigationEntry
.Link
Export-NavigationEntry
.Outputs
Entry
Returns the Entry object added unless the Silent switch parameter is used.
.Notes
string -> string -> bool? -> Entry?
#>
[CmdletBinding(
SupportsShouldProcess,
ConfirmImpact = 'Low')]
[OutputType([Entry])]
param(
# Token to use.
[Parameter(
Position = 0,
ValueFromPipelineByPropertyName)]
[Alias(
'Shortcut',
'Bookmark')]
[string] $Token
,
# Jump path. Default: current working directory
[Parameter(
Position = 1,
ValueFromPipelineByPropertyName)]
[ValidateNotNull()]
[string] $JumpPath = $PWD
,
# Do not emit the Entry object.
[switch] $Silent
)
process {
$isValidPath = Test-Path $JumpPath
if (!$isValidPath) {
Write-Warning ($Message.Warning.BadJumpPath -f $JumpPath)
}
<# Done: IO.DirectoryInfo objects will not validate incomplete, unqualified paths @endowdly
We don't want invalid paths to be ignored, so only change valid ones #>
$JumpPath = Invoke-Ternary $isValidPath { Convert-Path $JumpPath } { $JumpPath }
$msg = $Message.ShouldProcess.AddNavigationEntry -f $Token
if ($PSCmdlet.ShouldProcess($JumpPath, $msg)) {
New-Entry -Token $Token -Path $JumpPath -OutVariable entry |
Add-Entry $GoPS.Database
}
}
end {
if ($Silent.IsPresent) {
return
}
$entry
}
}
# Done: Tab-Completion on tokens with partial matching @endowdly
function Get-NavigationEntry {
<#
.Synopsis
Returns Entry objects filtered by token strings.
.Description
Returns Entry objects in the loaded Database or from specified files.
Accepts path names of navigation files as input or from the Path parameter.
Path strings can be wildcarded.
If Path is not used or no input is received, returns Entry objects from the loaded Database object.
Filters Entry objects by Token property.
Filtering strings can be entered by the Token parameter or by remaining arguments.
Token filtering strings can be wildcarded.
.Example
Get-NavigationEntry -Token 'this', 'that', 'theOther'
Get Entry objects in the currently loaded Database.
Returns Entry objects with Token properties 'this', 'that', or 'theOther' if they exist.
.Example
Get-NavigationEntry this that theOther
Get Entry objects in the currently loaded Database by remaining arguments.
Returns Entry objects with Token properties 'this', 'that', or 'theOther' if they exist.
.Example
Get-NavigationEntry git*
Get Entry objects in the currently loaded Database by wildcarded Token.
Returns all Entry objects with Token properties like 'git*' if they exist.
.Example
Get-NavigationEntry -Path ~/.gops2
Get Entry objects in specific files.
Returns all Entry objects in the given paths if they are valid navigation files.
.Example
'~/.gops3', '~/.gops2' | Get-NavigationEntry
Get Entry objects in specific files from the pipeline.
Returns all Entry objects in the given paths if they are valid navigation files.
.Example
'~/.gops*' | Get-NavigationEntry
Get Entry objects in specific files by wildcards from the pipeline
Returns all Entry objects in the given paths if they are valid navigation files.
.Example
Get-NavigationEntry -Path ~/.gops*
Get Entry objects in specific files by wildcards.
Returns all Entry objects in the given paths if they are valid navigation files.
.Example
Get-NavigationEntry -Path ~/.gops* -Token git*
Get Entry objects in specific files by wildcards filtered by Token.
Returns all Entry objects in the given paths with Token properties like git* if they are valid navigation files and the Entry objects with the specified Token properties exist.
Similar for pipelined paths.
.Link
Add-NavigationEntry
.Link
Remove-NavigationEntry
.Link
Export-NavigationEntry
.Inputs
System.String[]
.Outputs
Entry[], System.String[]
Returns an Entry array.
Returns a string array if the JumpPathOnly parameter is used.
.Notes
string[] -> string[] -> bool -> Entry[]?
string[] -> string[] -> bool -> string[]?
#>
[CmdletBinding()]
[OutputType(
[Entry[]],
[string[]])]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
param(
# Specifies a path to a database file. Default: Module DefaultPath
[Parameter(
ValueFromPipeline,
ValueFromPipelineByPropertyName)]
[ValidateScript({ Assert-Path $_ })]
[string[]] $Path = $GoPS.DefaultPath
,
# The tokens to fetch from the database. Default: '*'
[Parameter(
Position = 0,
ValueFromRemainingArguments)]
[ArgumentCompleter({
param ($cmdName, $paramName, $wordToComplete)
(Get-NavigationEntry).Token.Where{ $_ -like "${wordToComplete}*" } })]
[string[]] $Token = '*'
,
# Returns the jump path only.
[Alias(
'PathOnly',
'ValueOnly')]
[switch] $JumpPathOnly
)
begin {
$f = {
$currentToken = $_
$p = { $_.Token -like $currentToken }
$x.Where($p)
}
$g = {
process {
Convert-Path $_ |
ForEach-Object {
Import-NavigationFile $_ |
ConvertFrom-Database |
ForEach-Object { [void] $x.Add($_) } }
}
}
$x = [List[Entry]] @()
$y = [List[Entry]] @()
}
process {
if ($PSBoundParameters.ContainsKey('Path')) {
[void] $Path.ForEach($g)
}
else {
$GoPS.Database |
ConvertFrom-Database |
ForEach-Object { $x.Add($_) }
}
[void] $Token.ForEach($f).ForEach{ $y.Add($_) }
}
end {
Invoke-Ternary $JumpPathOnly.IsPresent { $y.ToArray().Path } { $y.ToArray() }
}
}
# Done: Tab-Completion on tokens with partial matching @endowdly
function Remove-NavigationEntry {
<#
.Synopsis
Removes an Entry from the navigation database.
.Description
Removes an Entry from the navigation database.
The function accepts Token arguments on the pipeline, from the parameter, and from remaining arguments.
If a Token property does not exist on any Entry objects, does nothing and continues.
Unlike Get-NavigationEntry, Remove- does not accept wildcard Tokens.
This is intentional in order to ensure that incorrect tokens are not removed by accident.
Remove- does accept Tokens returned from Get-NavigationEntry.
Returns the remaining Entry array in the database if the Silent parameter switch is not used.
.Example
Remove-NavigationEntry -Token 'this', 'that'
Removes Entry objects with Tokens this and that, if they exist.