-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyadm.ps1
194 lines (159 loc) · 3.8 KB
/
yadm.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
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
<#PSScriptInfo
.VERSION 1.3
.GUID efddd9c5-72e8-4eab-b5d7-abd9e968ca44
.AUTHOR jyf
.COMPANYNAME
.COPYRIGHT
.TAGS
.LICENSEURI https://github.com/jyfzh/PSYadm/blob/main/LICENSE
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
.PRIVATEDATA
#>
<#
.DESCRIPTION
Yet Another Dotfiles Manager Write In Powershell
#>
Param()
$GlobalArgs = $args
$gitDir = "$HOME/.local/share/yadm/repo.git"
$workTree = "$HOME"
$bootstrapFile = "$HOME/.config/yadm/bootstrap.ps1"
$encryptFile = "$HOME/.config/yadm/encrypt"
$archiveFile = "$HOME/.local/share/yadm/archive"
function help
{
$help = "
Usage: yadm <command> [options...]
Manage dotfiles maintained in a Git repository.
Git Commands:
Any Git command or alias can be used as a <command>. It will operate
on yadm's repository and files in the work tree (usually $HOME).
Commands:
yadm init [-f] - Initialize an empty repository
yadm clone <url> [-f] - Clone an existing repository
yadm list [-a] - List tracked files
yadm bootstrap - Execute $HOME/.config/yadm/bootstrap.ps1
Files:
$bootstrapFile - Script run via: yadm bootstrap
$gitDir - yadm's Git repository "
Write-Host $help
}
function bootstrap
{
if (Test-Path -Path $bootstrapFile)
{
. $bootstrapFile
} else
{
Write-Error "ERROR: Cannot execute bootstrap"
Write-Error "$bootstrapFile is not an executable program."
}
}
function init
{
if ($GlobalArgs[$args.Count-1] -eq '-f')
{
Remove-Item -Recurse -Force $gitDir
$GlobalArgs = $args[0..($args.Count-2)]
} elseif (Test-Path -Path $gitDir -PathType Container)
{
Write-Error "ERROR: Git repo already exists."
Write-Error "Use '-f' if you want to force it to be overwritten."
exit
}
$command = "git --git-dir=$gitDir $GlobalArgs --bare"
Invoke-Expression $command
git --git-dir=$gitDir --work-tree=$workTree config --local status.showUntrackedFiles no
}
function clone
{
if ($GlobalArgs[$args.Count-1] -eq '-f')
{
Remove-Item -Recurse -Force $gitDir
$GlobalArgs = $args[0..($args.Count-2)]
} elseif (Test-Path -Path $gitDir -PathType Container)
{
Write-Error "ERROR: Git repo already exists."
Write-Error "Use '-f' if you want to force it to be overwritten."
exit
}
$command = "git $GlobalArgs $gitDir --bare"
Invoke-Expression $command
git --git-dir=$gitDir --work-tree=$workTree reset --hard
git --git-dir=$gitDir --work-tree=$workTree config --local status.showUntrackedFiles no
}
function list
{
if ($GlobalArgs[$args.Count-1] -eq '-a')
{
$command = "git --git-dir=$gitDir ls-files"
Invoke-Expression $command
} else
{
$command = "git --git-dir=$gitDir --work-tree=$workTree ls-files"
Invoke-Expression $command
}
}
function encrypt
{
$tempfile = New-TemporaryFile
tar -cvzf $tempfile -T $encryptFile -C $HOME
gpg -c --output $archiveFile $tempfile
}
function decrypt
{
$tempfile = New-TemporaryFile
gpg -o $tempfile -d $archiveFile
tar -xvzf $tempfile -C $HOME
}
function default
{
$command = "git --git-dir=$gitDir --work-tree=$workTree $GlobalArgs"
Invoke-Expression $command
}
if ($GlobalArgs.Count -eq 0)
{
help
} else
{
switch ($GlobalArgs[0])
{
"help"
{
help
}
"bootstrap"
{
bootstrap
}
"init"
{
init
}
"clone"
{
clone
}
"list"
{
list
}
"encrypt"
{
encrypt
}
"decrypt"
{
decrypt
}
default
{
default
}
}
}