feat: interactive adding of packages

This commit is contained in:
xeruf 2022-07-25 15:58:44 +02:00
parent bb16a555e6
commit d6cc79380d
2 changed files with 41 additions and 20 deletions

View File

@ -2,7 +2,8 @@
## DEFINITIONS ## DEFINITIONS
name="$(basename "$0")" name="$(basename "$0")"
home="$(find "$INSTALEE_HOME" "${XDG_CONFIG_HOME:-$HOME/.config}/$name" "$HOME/.$name" "$PWD" -maxdepth 0 2>/dev/null | head -1)" homedir="$(find "$INSTALEE_HOME" "${XDG_CONFIG_HOME:-$HOME/.config}/$name" "$HOME/.$name" "$PWD" -maxdepth 0 2>/dev/null | head -1)"
packagedir="$homedir/packages"
highlight() { echo "$1"; } highlight() { echo "$1"; }
destress() { echo "$1"; } destress() { echo "$1"; }
@ -17,8 +18,8 @@ getcontent() {
# Get available package entries for given package # Get available package entries for given package
getentries() { getentries() {
cat "$home/handlers.available" | while read handler cat "$homedir/handlers.available" | while read handler
do find "$home/packages/$1/" -depth -type f -name "$handler*" | tac 2>/dev/null do find "$packagedir/$1/" -depth -type f -name "$handler*" | tac 2>/dev/null
done done
} }
@ -26,12 +27,12 @@ getentries() {
installpkg() { installpkg() {
test "$1" = "--quiet" && local quiet=true && shift test "$1" = "--quiet" && local quiet=true && shift
local pkgs=$(getentries "$1") local pkgs=$(getentries "$1")
local pkgdir="$home/packages/$1/" local pkgdir="$packagedir/$1/"
if test -z "$pkgs"; then if test -z "$pkgs"; then
local handlers=$(find "$pkgdir" -type f -printf "%f\n" 2>/dev/null) local handlers=$(find "$pkgdir" -type f -printf "%f\n" 2>/dev/null)
for handler in $handlers for handler in $handlers
do installpkg --quiet "$handler" && do installpkg --quiet "$handler" &&
echo "$handler" >> "$home/handlers.available" && echo "$handler" >> "$homedir/handlers.available" &&
installpkg "$1" && installpkg "$1" &&
return $? return $?
done done
@ -40,7 +41,7 @@ installpkg() {
return 2 return 2
fi fi
for pkg in $pkgs for pkg in $pkgs
do highlight "Installing $(echo ${pkg#$home/packages/} | sed 's|\(.*\)/|\1 via |')" do highlight "Installing $(echo ${pkg#$packagedir/} | sed 's|\(.*\)/|\1 via |')"
set -e set -e
local name="$(basename $pkg)" local name="$(basename $pkg)"
local base="${name%_*}" local base="${name%_*}"
@ -49,7 +50,7 @@ installpkg() {
done done
local ext="${name#$base}" local ext="${name#$base}"
local handler="$home/handlers/$base" local handler="$homedir/handlers/$base"
if test -d "$handler" if test -d "$handler"
then then
# This declaration needs to be ahead, otherwise it overrides failing exit codes # This declaration needs to be ahead, otherwise it overrides failing exit codes
@ -69,7 +70,7 @@ installpkg() {
} }
## EXECUTION ## EXECUTION
chmod +rx "$home/handlers" -R chmod +rx "$homedir/handlers" -R
case "$1" in case "$1" in
(-V|--version|"") (-V|--version|"")
@ -79,14 +80,31 @@ case "$1" in
exit 0 exit 0
;; ;;
(-h|--help) (-h|--help)
man $name 2>/dev/null || man "$home/$name.1" man $name 2>/dev/null || man "$homedir/$name.1"
exit $? exit $?
;; ;;
(-a|--add) (-a|--add)
dir="$home/packages/$2" shift
if test $# -gt 0
then packagename=$1; shift
else printf "Name of the package? "; read packagename
fi
dir="$packagedir/$packagename"
mkdir -p "$dir" mkdir -p "$dir"
test $# -gt 3 && echo "$4">"$dir/$3" || $EDITOR "$dir/$3" if test $# -gt 0
exit $? then handler=$1
test $# -gt 1 && echo "$2">"$dir/$handler" || $EDITOR "$dir/$handler"
exit $?
else
while printf "Handler? (Ctrl-C or Ctrl-D to exit, / to list all) " && read handler
do
case "$handler" in
(/*) find "$packagedir" "$homedir/handlers" -type f -not -name "*depends" -printf "%f\n" | sort | uniq | paste -s -d ' ';;
(*) $EDITOR "$dir/$handler";;
esac
done
exit 0
fi
;; ;;
(-v|--verbose) (-v|--verbose)
set -x set -x
@ -95,11 +113,11 @@ case "$1" in
esac esac
runinstalee() { runinstalee() {
groupfile="$home/groups/$1" groupfile="$homedir/groups/$1"
if test -f "$groupfile" if test -f "$groupfile"
then for pkg in $(getcontent "$groupfile") then for pkg in $(getcontent "$groupfile")
do runinstalee "$pkg" || do runinstalee "$pkg" ||
"$home/handlers/$(head -1 handlers.available)/install" "$pkg" "$homedir/handlers/$(head -1 handlers.available)/install" "$pkg"
done done
else installpkg "$1" else installpkg "$1"
fi fi

View File

@ -9,9 +9,11 @@ instalee - Unix-style universal package management wrapper
.SH SYNOPSIS .SH SYNOPSIS
.B instalee <packagename> .B instalee <packagename>...
- install packages
.br .br
.B instalee [-a|--add] <packagename> <identifier> [<content>] .B instalee <-a|--add> [packagename] [handler] [<content>]
- add new packages
.SH DESCRIPTION .SH DESCRIPTION
@ -48,17 +50,18 @@ If a \fIhandler\fP is available without entry in \fIhandlers\fP (commonly named
.SH OPTIONS .SH OPTIONS
.TP .TP
<packagename> <packagename>
The package to install. Corresponds to a directory in \fIpackages\fP. The package. Corresponds to a directory in \fIpackages\fP.
.TP .TP
-a|--add -a|--add
Add a package, instead of installing it. Add a package entry to the repo. Will prompt for missing arguments.
.TP .TP
<identifier> <identifier>
Name of the packager for which this entry is meant, plus potential pre- and suffixes. Name of the packager for which this entry is meant, plus potential pre- and suffixes.
.TP .TP
<content> <content>
Content of the file for the package to be added. If omitted, the file is opened with $EDITOR. Content of the file for the package to be added.
Note that the parent directory is definitely created by then, but the file isn't. If omitted, the file is opened with $EDITOR.
Note that the parent directory already exists by then, but the file does not.
.SH EXIT CODES .SH EXIT CODES