-
Notifications
You must be signed in to change notification settings - Fork 9
/
vpsm
executable file
·642 lines (539 loc) · 16.1 KB
/
vpsm
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
#!/bin/bash
progname=${0##*/}
numcolorok=2
numcolorfail=1
numcolorlogo=5
numcolorheader=3
numcolortext=4
numcolorgray=2
numcolorpkgcount=8
numcolordarkgray=11
numcolorbrackets=6
# enable or disable colors based on the argument given, i.e.:
# setcolors on # colors on
# setcolors off # colors off
# setcolors auto # colors on or off depending on environment
declare -A COLORS
setcolors () {
local opt=$1
# determine if colors should be enabled or not
if [[ $opt == auto ]]; then
# if stdout is a TTY and the TERM looks like it supports color enable colors
if [[ -t 1 && $TERM == *color* ]]; then
opt='on'
else
opt='off'
fi
fi
case "$opt" in
on)
local i
for i in {1..11}; do
if [[ -n ${COLORS[$i]} ]]; then
continue
fi
COLORS[$i]=$(tput setaf "$i")
done
colorbrackets=${COLORS[$numcolorbrackets]}
colordarkgray=${COLORS[$numcolordarkgray]}
colorfail=${COLORS[$numcolorfail]}
colorgray=${COLORS[$numcolorgray]}
colorheader=${COLORS[$numcolorheader]}
colorlogo=${COLORS[$numcolorlogo]}
colorok=${COLORS[$numcolorok]}
colorpkgcount=${COLORS[$numcolorpkgcount]}
colortext=${COLORS[$numcolortext]}
colorreset=$(tput sgr0)
;;
off)
colorbrackets=
colordarkgray=
colorfail=
colorgray=
colorheader=
colorlogo=
colorok=
colorpkgcount=
colortext=
colorreset=
unset COLORS
declare -A COLORS
;;
*)
rmsg 255 "unknown color option: '$opt'"
exit 255
;;
esac
}
# print the logo with brackets colorized
getlogo () {
printf '%s[%s%s%s]%s' \
"$colorbrackets" \
"$colorlogo" "$progname" \
"$colorbrackets" \
"$colorreset"
}
# prints a message (with vpsm-prefix)
msg () {
local logo=$(getlogo)
local newline=true
if [[ $1 == '-n' ]]; then
newline=false
shift
fi
printf '%s %s%s%s' "$logo" "$colortext" "$*" "$colorreset"
$newline && echo
}
# rmsg - same (but colorized based on return status passed via $1)
rmsg () {
local code=$1
shift
local logo=$(getlogo)
local statuscolor
if ((code == 0)); then
statuscolor=$colorok
else
statuscolor=$colorfail
fi
printf '%s %s%s%s\n' "$logo" "$statuscolor" "$*" "$colorreset"
}
# --------------------------------------------------------------------------------------------
setcolors on
logo=$(getlogo)
distdir=$(xdistdir 2>&1)
IFS=$'\n' read -rd '' -a xdistdir_msgs <<<"$distdir"
if [ "$?" == 111 ]; then
for m in "${xdistdir_msgs[@]}"
do
printf '%s %s%s%s\n' "$logo" "$colorfail" "$m" "$colorreset"
done
exit
elif ! [ -d $distdir ]; then
m="No such file or directory: $distdir"
printf '%s %s%s%s\n' "$logo" "$colorfail" "$m" "$colorreset"
read -p "$colorgray-> Clone void-packages from: $colorreset" void_packages_url
read -e -p "$colorgray-> Target directory: $colorreset" void_packages_dest
eval target_dir=$void_packages_dest
msg "Cloning from $void_packages_url -> $target_dir"
git clone "$void_packages_url" "$target_dir"
msg "Cloned!"
msg "Install xtools utility "
sudo xbps-install xtools
exit
else
export VOID_PKGS=$XBPS_DISTDIR
export VOID_SRCPKGS=$VOID_PKGS/srcpkgs
export VOID_HOSTDIR=$VOID_PKGS/hostdir
export VOID_BINPKGS=$VOID_HOSTDIR/binpkgs
cd $VOID_PKGS
fi
# --------------------------------------------------------------------------------------------
getversion () {
local fp=$(readlink -f "$0")
(cd "${fp%/*}" && \
git describe --all --debug --long --tags 2>/dev/null) || echo "UNKNOWN"
}
version=$(getversion)
verbose=false
banner () {
echo -n "$colorlogo"
echo ' __ ___ __ ____ __ ';
printf " \ V / '_ (_-< ' \ "
echo -n "$colorgray"
echo " $progname - void-packages sources management wrapper for XBPS-SRC"
echo -n "$colorlogo"
echo -n ' \_/| .__/__/_|_|_|'
echo -n "$colorgray"
echo ' GitHub: https://github.com/sinetoami/vpsm'
echo -n "$colorlogo"
echo ' |/ '
echo ' ´ '
echo -n "$colorreset"
}
version () {
banner
msg "$progname - Version: $version"
msg "Copyright (c) 2018 Sinésio Neto <sinetoami [at] gmail [dot] com> (GPLv3+)"
msg "XBPS version: $(xbps-query -v --version | sed 's/GIT: UNSET//')"
}
wrapcommand() {
local cmd ret
cmd=("$@")
echo "$colortext(${cmd[*]}):$colorreset"
"${cmd[@]}"
ret=$?
rmsg "$ret" "[${cmd[*]}], return code was: $ret"
exit "$ret"
}
usage () {
# echo
version
echo
echo -n "$colorheader"
echo "USAGE: "
echo -n "$colorgray"
echo "$progname [OPTIONS] [SUBCOMMANDS] [<ARGS>]"
echo
echo -n "$colorheader"
echo "OPTIONS: "
echo -n "$colorgray"
echo "--color=<yes|no|auto> - Enable/Disable colorized output (default: auto)"
echo "--help - (same as: help)"
echo "--help-pager - (same as: helppager)"
echo
echo -n "$colorheader"
echo "GIT SUBCOMMANDS: "
echo -n "$colorgray"
echo "add-changes (ac) <file> - Add work changes in your clone repository."
echo "add-remote-repo (arr) <remote-name> <url> - Add new remote repo in your clone repository."
echo "checkout-branch (co) <branch-name> - Switch branch."
echo "commit-changes (cc) <message> - Commits changes added."
echo "create-branch (cb) <branch-name> - Create a new branch."
echo "delete-branch (delb) <branch-name> - Delete a existing branch."
echo "list-branches (lb) - List all branches."
echo "log (lg) <pkgname> - Commit log for <pkgname> XBPS template"
echo "log-graph (lgg) - Commit graph log for all commits"
echo "pull (pp) <remote-repo> <branch> - Pull commit from <remote-repo> <remote-branch>."
echo "pull-request (pr) [message] - Create a pull-request [optional message]."
echo "push-commit (pc) <remote-repo> <branch> - Push commits to <romote-repo> <remote-branch>."
echo "update-repo (upr) - Pull commits from void-linux official repo."
echo
echo -n "$colorheader"
echo "XBPS-SRC SUBCOMMANDS: "
echo -n "$colorgray"
echo "binbootstrap (bb) - Install bootstrap packages from host repositories into <masterdir>."
echo "bootstrap-up (bu) - Updates bootstrap packages."
echo "build-environment - Configure environment to create binary packages."
echo "edit-template (et) <pkgname> - Edit <pkgname> template."
echo "install (i) <pkgname> - Build binary package for <pkgname> and all required dependencies and install."
echo "lint (li) <pkgname> - Scan XBPS <pkgname> template for common mistakes."
echo "list (l) - Lists installed packages in <masterdir>."
echo "mypkgs (mp) [email] - List all pkgs maintained by you or by [optional email]."
echo "new (n) <pkgname> - Create a new <pkgname> package."
echo "pkg <pkgname> - Only build binary package for <pkgname> and all required dependencies."
echo "purge-distfiles (pdistf) - Removes all obsolete distfiles in <hostdir>/sources."
echo "searchbin (sb) <pkgname> - Search in <hostdir>/binpkgs for package by <name> (use xbps-query). "
echo "searchsrc (ss) <pkgname> - Search in <srcpkgs> for package by <name>."
echo "show (sw) <pkgname> - Show information for the specified package."
echo "show-build-deps (bdeps) <pkgname> - Show required build dependencies for <pkgname>."
echo "show-deps (rdeps) <pkgname> - Show required run-time dependencies for <pkgname>."
echo "uninstall (un) <pkgname> - Uninstall and purge distfiles for <pkgname>."
echo "update-bulk (upb) - Rebuilds all packages in the system repositories that are outdated."
echo "update-check (upc) <pkgname> - Check upstream site of <pkgname> for new releases."
echo "update-sys (ups) - Rebuilds packages in system and updates them."
echo "xgsum <pkgname> - Generate SHA256 for <pkgname> template."
echo "xinstall (xi) <pkgname> - Like xbps-install -S <pkgname>, but take cwd repo and sudo/su into account."
echo
echo -n "$colorreset"
}
setcolors auto
case "$1" in
--color=true|--color=yes|--color=on)
setcolors on
shift
;;
--color=auto)
setcolors auto
shift
;;
--color=false|--color=off|--color=no)
setcolors off
shift
;;
--help)
shift
usage
exit 255
;;
--help-pager)
shift
"$0" --color=off --help | less
;;
--*)
msg "Unknown option: $1 (try: $progname --help)"
exit 1
;;
esac
current_branch=$(git rev-parse --abbrev-ref HEAD)
template_path() {
echo "$VOID_PKGS/srcpkgs/$1/template"
}
git_owner_name() {
url="$(git config --get remote.origin.url)"
re="^(https|git)(:\/\/|@)([^\/:]+)[\/:]([^\/:]+)\/(.+).git$"
if [[ $url =~ $re ]]; then
user=${BASH_REMATCH[4]}
fi
echo "$user"
}
void_linux_remote() {
git remote -v | rg void-linux/void-packages | awk '{print $1}' | head -1
}
longest_word () {
declare -a pkgs=$@
declare -i longest=0
for pkg in ${pkgs[@]}
do
if [ ${#pkg} -gt $longest ]; then
longest=${#pkg}
fi
done
echo $longest
}
whitespaces() {
i=0
space=" "
qnt_ws=$(($1 - $2))
while [[ "$i" != $qnt_ws ]]
do
space+=" "
((i+=1))
done
echo "$space"
}
show_desc () {
./xbps-src show "$1" | rg short_desc | awk '{$1=""; print $0}'
}
if_instaled () {
[[ "$1" != '' ]] && flag="[$colorgray*$colorreset]" || flag="[-]"
echo -n "$flag"
}
lsgrep_binpkgs () {
[ -d "$VOID_BINPKGS" ] && ls $VOID_HOSTDIR/* | rg $1 || echo ''
}
check_binpkgs () {
check=$(lsgrep_binpkgs "$1")
resul=$(if_instaled $check)
echo -n "$resul"
}
search_result () {
declare -a res=()
pkgs=$(ls $VOID_SRCPKGS | rg $1)
lword=$(longest_word ${pkgs[@]})
for pkg in ${pkgs[@]}
do
wsnum=$(whitespaces $lword ${#pkg})
desc=$(show_desc $pkg)
inststr=$(check_binpkgs $pkg)
res+=("$inststr $pkg$wsnum-$desc")
done
printf '%s\n' "${res[@]}"
}
xgensum_config() {
template=$(template_path $1)
xgensum -f $template | \
while read line
do
if [[ "$line" =~ checksum= ]]; then
sed -i "s/^checksum=.*$/$line/g" $template
fi
echo "$line"
done
}
addrepos() {
check_repos=$(find $VOID_BINPKGS/* -type d)
declare -a repos
for repo in ${check_repos[@]}
do
if ! [[ "$repo" =~ repodata ]];then
repos+="--repository=$repo "
fi
done
echo "${repos[@]}"
}
if [[ -z $1 ]]; then
usage
exit 0
fi
cmd=$1
if [[ $arg =~ --.* ]]; then
cmd=${arg:2}
fi
shift
case "$cmd" in
# --------------------------------------------------------------------------------------------
#GIT OPTIONS
add-changes|ac)
msg -n "Add changes $* "
wrapcommand git add "$@"
;;
add-remote-repo|arr)
msg -n "Add new remote repo $* "
wrapcommand git remote add "$@"
;;
checkout-branch|co)
msg -n "Switch branch -> $* "
wrapcommand git checkout "$@"
;;
commit-changes|cc)
msg -n "Commit changes on -> $current_branch "
wrapcommand xbump "$@"
;;
create-branch|cb)
msg -n "Create a new branch -> $* "
wrapcommand git branch "$@"
;;
delete-branch|delb)
msg -n "Delete branch -> $* "
wrapcommand git branch -d "$@"
;;
list-branches|lb)
msg -n "Listing all branchs"
wrapcommand git branch -lv
;;
log|lg)
msg -n "Commit log of $* template "
wrapcommand xlg "$@"
;;
log-graph|lgg)
git log --all --graph --date=short \
--pretty=format:'%C(auto)%h%C(auto)%d %s %C(dim white)(%aN, %ar, %cd)'
;;
pull|pp)
msg -n "Pull commits from: repo->$1, branch->$2 "
wrapcommand git pull --rebase "$@"
;;
pull-request|pr)
! [ -z "$1" ] && m="$*" || m="$(git log -1 --pretty=%B)"
ownername=$(git_owner_name)
lrepo=$(void_linux_remote)
msg -n "Create a pull-request "
wrapcommand hub pull-request -m "$m" -b "$lrepo:master" -h "$ownername:$current_branch"
;;
push-commit|pc)
msg -n "Push commit to: repo->$1, branch->$2 "
wrapcommand git push -u "$@"
;;
update-repo|upr)
lrepo=$(void_linux_remote)
msg "Pull commits from Void Linux's void-packages: remote-name -> $lrepo "
git checkout master
git pull --rebase $lrepo master
git push -u origin master
./xbps-src bootstrap-update
;;
# --------------------------------------------------------------------------------------------
# XBPS-SRC OPTIONS
binbootstrap|bb)
msg -n "Create the bootstrap environment "
echo XBPS_ALLOW_RESTRICTED=yes >> etc/conf
wrapcommand ./xbps-src binary-bootstrap
;;
bootstrap-up|bu)
msg -n "Updates bootstrap packages "
wrapcommand ./xbps-src bootstrap-update
;;
build-environment)
whoami=$(whoami)
msg "Setting up XBPS_CHROOT_CMD=uchroot "
echo XBPS_CHROOT_CMD=uchroot >> etc/conf
cd /usr/bin
msg "chown root:xbuilder xbps-uchroot "
sudo chown root:xbuilder xbps-uchroot
msg "chmod 4750 xbps-uchroot"
sudo chmod 4750 xbps-uchroot
msg "usermod -a -G xbuilder $whoami"
sudo usermod -a -G xbuilder $whoami
# cd $OLDPWD
# msg "add void-linux/void-packages track repository "
# git remote add void-linux git://github.com/void-linux/void-packages.git
# msg "git pull void-linux master "
# git pull --rebase void-linux master
;;
edit-template|et)
template=$(template_path "$@")
exec $EDITOR $template
;;
install|i)
if (($# < 1)); then
rmsg 255 "ERROR: install: argument missing, try --help."
exit 1
fi
msg "Build binary package -> $* "
./xbps-src pkg "$@"
msg -n "Installing package -> $* "
wrapcommand xi "$@"
;;
lint|li)
template=$(template_path "$@")
msg -n "Scan for common mistakes "
wrapcommand xlint "$template"
;;
list|l)
msg -n "Lists installed packages "
wrapcommand ./xbps-src list
;;
mypkgs|mp)
ownername=$(git_owner_name)
msg -n "Listing packages of $ownername "
wrapcommand xmypkgs
;;
new|n)
msg -n "Creating a new package -> $* "
wrapcommand xnew "$@"
;;
pkg)
msg -n "Build binary package -> $* "
wrapcommand ./xbps-src pkg "$@"
;;
purge-distfiles|purged)
msg -n "Removes all obsolete distfiles in $VOID_HOSTDIR/sources "
wrapcommand ./xbps-src purge-distfiles
;;
searchbin|sb)
repos=$(addrepos)
msg -n "Searching (binpkgs) -> $* "
wrapcommand xbps-query $repos -Rs "$@"
;;
searchsrc|ss)
msg -n "Searching (srcpkgs) -> $* "
wrapcommand search_result "$@"
;;
show|sw)
msg -n "Show information -> $* "
wrapcommand ./xbps-src show "$@"
;;
show-build-deps|bdeps)
msg -n "Show required build dependencies -> $* "
wrapcommand ./xbps-src show-build-deps "$@"
;;
show-deps|rdeps)
msg -n "Show required run-time dependencies -> $* "
wrapcommand ./xbps-src show-deps "$@"
;;
show-repo-updates|sru)
msg -n "Printing the list of outdated packages "
wrapcommand ./xbps-src show-repo-updates
;;
uninstall|un)
shopt -s globstar
msg "Remove package and purge distfiles -> $* "
sudo xbps-remove -v -R "$@"
rm -rf $VOID_BINPKGS/**/*$@*
shopt -u globstar
;;
update-bulk|upb)
msg -n "Rebuilds all packages outdated (system repositories) "
wrapcommand ./xbps-src update-bulk
;;
update-check|upc)
msg -n "Check upstream site new releases -> $* "
wrapcommand ./xbps-src update-check "$@"
;;
update-sys|ups)
msg -n "Rebuilds packages and updates them (in system) "
wrapcommand ./xbps-src update-sys
;;
xgsum)
msg -n "Generate/Update SHA256 sum -> $@/template "
wrapcommand xgensum_config "$@"
;;
xinstall|xi)
if (($# < 1)); then
rmsg 255 "ERROR: install: argument missing, try --help."
exit 1
fi
msg -n "Install $* package "
wrapcommand xi "$@"
;;
esac