-
Notifications
You must be signed in to change notification settings - Fork 0
/
warp.ps1
115 lines (91 loc) · 2.44 KB
/
warp.ps1
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
Param(
[Parameter(Mandatory = $true, HelpMessage = "Action to take.")]
[ValidateSet("set", "remove", "to", "list")]
[string]
$Action,
[Parameter(HelpMessage = "Name of the warp to take action on.")]
[string]
$Name
)
$dataFilePath = "$PSScriptRoot/warps.csv"
# Ensures that the data file exists
if (-Not (Test-Path $dataFilePath)) {
Write-Output "Name,Location" > $dataFilePath
}
function WarpSet {
if ($Name -eq '') {
throw "Please enter the name of your warp."
}
# Ensures a warp doesn't already exist with the same name
$data = Import-Csv -Path $dataFilePath
$matchedWarp = $data | Where-Object { $_.Name -eq $Name }
if ($matchedWarp) {
throw "Warp '$($matchedWarp.Name)' already exists."
}
$line = @(
[PSCustomObject]@{Name = $Name; Location = (Get-Location).Path }
)
$line | Export-Csv -Path $dataFilePath -NoTypeInformation -Append
Write-Output "✨ Added warp '$Name' here."
}
function WarpRemove {
$data = Import-Csv -Path $dataFilePath
$newData = foreach ($line in $data) {
# If no name is provided, remove the local warp.
if ($Name -eq '') {
if (-not ($line.Location -eq (Get-Location).Path)) {
$line
}
}
# Otherwise, remove the warp by its name
if (-not ($line.Name -eq $Name)) {
$line
}
}
$newData | Export-Csv -Path $dataFilePath -NoTypeInformation
Write-Output "✅ Removed warp"
}
function WarpTo {
if ($Name -eq '') {
throw "Please enter the name of your warp."
}
$data = Import-Csv -Path $dataFilePath
$matchedWarp = $data | Where-Object { $_.Name -eq $Name }
if (-Not $matchedWarp) {
throw "Could not find the warp '$Name'"
}
Set-Location $matchedWarp.Location
Write-Output "✨ Warped to $Name."
}
function WarpList {
Write-Output "📖 Registered warps`n"
$n = 0
Import-Csv -Path $dataFilePath | ForEach-Object {
$name = $_.Name
$location = $_.Location
Write-Host "'$name' ➡️ $location"
$n = $n + 1
}
Write-Output "`nTotal: $n warps."
}
switch ($Action) {
"set" {
WarpSet;
break
}
"remove" {
WarpRemove;
break
}
"to" {
WarpTo;
break
}
"list" {
WarpList;
break
}
default {
throw "Invalid action '$Action'."
}
}