-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgh-pkgs.sh
executable file
·265 lines (243 loc) · 7.25 KB
/
gh-pkgs.sh
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
#!/bin/sh
#Save path to root of the repo
installer_dir="$(dirname "$0")"
#Set directory to save package version
lib_dir="/var/lib/gh-pkgs"
[ ! -d "$lib_dir" ] && sudo mkdir -p "$lib_dir"
#Save boolean for script calls
install_command=false
update_command=false
usage_flags() {
echo ""
echo "Flags:"
echo " -x ignore hashes"
echo " -y refresh github api response"
}
usage() {
case "$command" in
"clean")
echo "Usage: gh-pkgs clean [<packages>]"
;;
"download")
echo "Usage: gh-pkgs download [<flags>] <packages>"
usage_flags
;;
"install")
echo "Usage: gh-pkgs install [<flags>] <packages>"
usage_flags
;;
"list")
echo "Usage: gh-pkgs list [<packages>]"
;;
"search")
echo "Usage: gh-pkgs search [<packages>]"
;;
"uninstall")
echo "Usage: gh-pkgs uninstall <packages>"
;;
"update")
echo "Usage: gh-pkgs update [<flags>] [<packages>]"
usage_flags
echo " -z print found update, no install"
;;
*)
echo "Usage: gh-pkgs <command> [<flags>] [<packages>]"
echo ""
echo "Commands:"
echo " clean clean cache directory"
echo " download download package without installing"
echo " install install package"
echo " list list all packages installed"
echo " search search across available packages"
echo " uninstall uninstall package"
echo " update update package"
usage_flags
echo " -z (update) print found update, no install"
;;
esac
exit
}
get_packages() {
#Get the all packages from packages folder
for script in "$installer_dir"/packages/*.sh; do
basename "$script" .sh
done
}
search_matching_packages() {
#Repeat for each argument
for argument in "$@"; do
#Get argument match from packages folder
[ -f "$installer_dir/packages/${argument}.sh" ] \
&& echo "$argument" \
|| echo "No package with name '$argument'" >&2
done
}
get_installed_packages() {
#Get the all packages in lib directory
find "$lib_dir" -maxdepth 1 -mindepth 1 -type f -printf '%f\n' | sed "s,-[^-]*$,,g" | sort -u
}
search_matching_installed_packages() {
#Repeat for each argument
for argument in "$@"; do
#Get argument match from lib directory
for file in "$lib_dir/$argument-"*; do
[ -f "$file" ] \
&& echo "$argument" && break \
|| echo "No package installed with name '$argument'" >&2
done
done
}
clean_cache() {
if [ $# = 0 ]; then
#Run clean cache script
. "$installer_dir/.clean-cache.sh"
else
#Execute based on package match
for package in $(search_matching_packages "$@"); do
#Check that script for package exists
if [ -f "$installer_dir/packages/${package}.sh" ]; then
(
. "$installer_dir/packages/${package}.sh"
echo "Clean cache for $package_name..."
. "$installer_dir/.clean-cache.sh"
)
fi
done
fi
}
download_packages() {
#Print usage if arguments are empty
if [ $# = 0 ]; then
usage
else
#Execute based on package match
for package in $(search_matching_packages "$@"); do
#Check that script for package exists
if [ -f "$installer_dir/packages/${package}.sh" ]; then
(
. "$installer_dir/packages/${package}.sh"
echo "Start $package_name download..."
. "$installer_dir/.download.sh"
)
fi
done
fi
}
install_packages() {
install_command=true
#Print usage if arguments are empty
if [ $# = 0 ]; then
usage
else
#Execute based on package match
for package in $(search_matching_packages "$@"); do
#Check that script for package exists
if [ -f "$installer_dir/packages/${package}.sh" ]; then
(
. "$installer_dir/packages/${package}.sh"
echo "Start $package_name installation..."
. "$installer_dir/.install.sh"
)
fi
done
fi
}
list_packages() {
if [ $# = 0 ]; then
#Print installed packages
get_installed_packages
else
#Print installed packages based on match
search_matching_installed_packages "$@"
fi
}
search_packages() {
if [ $# = 0 ]; then
#Print available packages
get_packages
else
#Print available packages based on match
search_matching_packages "$@"
fi
}
uninstall_packages() {
#Print usage if arguments are empty
if [ $# = 0 ]; then
usage
else
#Execute based on package match
for package in $(search_matching_installed_packages "$@"); do
#Check that script for package exists
if [ -f "$installer_dir/packages/${package}.sh" ]; then
(
. "$installer_dir/packages/${package}.sh"
echo "Start $package_name uninstallation..."
. "$installer_dir/.uninstall.sh"
)
fi
done
fi
}
update_packages() {
update_command=true
if [ $# = 0 ]; then
#Execute for all installed packages
for package in $(get_installed_packages); do
#Check that script for package exists
if [ -f "$installer_dir/packages/${package}.sh" ]; then
(
. "$installer_dir/packages/${package}.sh"
. "$installer_dir/.install.sh"
)
fi
done
else
#Execute based on installed package match
for package in $(search_matching_installed_packages "$@"); do
#Check that script for package exists
if [ -f "$installer_dir/packages/${package}.sh" ]; then
(
. "$installer_dir/packages/${package}.sh"
. "$installer_dir/.install.sh"
)
fi
done
fi
}
#Check that there are arguments
if [ $# != 0 ]; then
#Save first argument
command="$1"
#Skip first argument
shift 1
else
#Print usage if arguments are empty
usage
fi
#Check for flag match
ignore_hash_flag=false
refresh_flag=false
no_install_flag=false
while getopts ":xyz" opt; do
case $opt in
x) ignore_hash_flag=true ;;
y) refresh_flag=true ;;
z) no_install_flag=true ;;
*) usage ;;
esac
done
#Skip flags in script arguments
shift "$((OPTIND - 1))"
#Reset getopts automatic variable
OPTIND=1
#Check for command match
case "$command" in
"clean") clean_cache "$@" ;;
"download") download_packages "$@" ;;
"install") install_packages "$@" ;;
"list") list_packages "$@" ;;
"search") search_packages "$@" ;;
"uninstall") uninstall_packages "$@" ;;
"update") update_packages "$@" ;;
*) usage ;;
esac