usr

Modulate your userpace into POSIX-compliant shell scripts!
git clone git://clone.theohenson.com/usr.git
Log | Files | Refs | README

commit 62adfefb7d5884abe427301e86e7a32e99376f66
parent 68c93a415925a2d14a4b39c612c3dbc6694b8392
Author: Theo Henson <[email protected]>
Date:   Sun, 15 Nov 2020 14:13:12 -0500

Implement module files/dirs

Diffstat:
Ahome/.test-dir/1 | 1+
Ahome/.test-dir/2 | 1+
Mmodules/test.mod.sh | 12++++++++----
Musr | 62++++++++++++++++++++++++++++++++++++++++++++------------------
4 files changed, 54 insertions(+), 22 deletions(-)

diff --git a/home/.test-dir/1 b/home/.test-dir/1 @@ -0,0 +1 @@ +one diff --git a/home/.test-dir/2 b/home/.test-dir/2 @@ -0,0 +1 @@ +two diff --git a/modules/test.mod.sh b/modules/test.mod.sh @@ -1,9 +1,5 @@ # test module -test_files() { - printf ".test\n" -} - test_add() { printf "linking \$HOME/.test\n" ln -sf "$USR_DIR/home/.test" "$HOME/.test" @@ -18,3 +14,11 @@ test_info() { printf "this module exists for testing purposes. it links a file (.test) into your home directory.\n" } + +test_dirs() { + printf ".test-dir\n" +} + +test_files() { + printf ".test\n.test-dir/1\n.test-dir/2" +} diff --git a/usr b/usr @@ -1,7 +1,5 @@ #!/bin/sh # licensed under the Unlicense -# TODO: -# - module files set -e @@ -10,10 +8,11 @@ die() { exit 1 } -[ "$(id -u)" = 0 ] && die "do not run as root" +[ "$(id -u)" = 0 ] && die "usr: do not run as root" : "${USR_DIR:=$HOME/src/git/usr}" +# create directories mkdir -p "$USR_DIR"/var mkdir -p "$USR_DIR"/home mkdir -p "$USR_DIR"/modules @@ -26,6 +25,7 @@ options: -r <module> remove module -i <module> module information -g <module> get module + -p <module> prep module -u update remote -l list local modules -li list installed modules @@ -37,13 +37,13 @@ module_add() { MOD_FILE="$USR_DIR/modules/$MOD".mod.sh MOD_INSTALL="$USR_DIR/var/$MOD".installed - [ -e "$MOD_INSTALL" ] && die "$MOD is already installed" + [ -e "$MOD_INSTALL" ] && die "usr: $MOD is already installed" [ -z "$MOD" ] && usage # shellcheck source=modules/test.mod.sh . "$MOD_FILE" - printf "adding %s...\n" "$MOD" + printf "usr: adding %s\n" "$MOD" "$MOD"_add touch "$MOD_INSTALL" @@ -54,13 +54,13 @@ module_remove() { MOD_FILE="$USR_DIR/modules/$MOD".mod.sh MOD_INSTALL="$USR_DIR/var/$MOD".installed - [ -e "$MOD_INSTALL" ] || die "$MOD is not installed" + [ -e "$MOD_INSTALL" ] || die "usr: $MOD is not installed" [ -z "$MOD" ] && usage # shellcheck source=modules/test.mod.sh . "$MOD_FILE" - printf "removing %s...\n" "$MOD" + printf "usr: removing %s\n" "$MOD" "$MOD"_remove rm "$MOD_INSTALL" @@ -75,7 +75,7 @@ module_info() { # shellcheck source=modules/test.mod.sh . "$MOD_FILE" - printf "%s:\n" "$MOD" + printf "usr: %s:\n" "$MOD" "$MOD"_info } @@ -86,22 +86,48 @@ get_module() { [ -z "$MOD" ] && usage VALID="" - while read -r RMOD; do + while read -r RMOD ; do if [ "$MOD" = "$RMOD" ] ; then VALID="yes" fi done <"$USR_DIR"/var/remote.list - [ "$VALID" = "yes" ] || die "remote does not have $MOD" + [ "$VALID" = "yes" ] || die "usr: remote does not have $MOD" URL=$(cat "$USR_DIR"/var/remote) URL="$URL"modules/"$MOD".mod.sh - printf "getting %s\n" "$URL" - printf "url = %s" "$URL" | curl -sLf -o "$USR_DIR/modules/$MOD".mod.sh -K - || die "error getting" + printf "usr: getting %s\n" "$URL" + printf "url = %s" "$URL" | curl -sLf -o "$USR_DIR/modules/$MOD".mod.sh -K - || die "usr: error getting" +} + +prep_module() { + MOD="$2" + MOD_FILE="$USR_DIR/modules/$MOD".mod.sh + + [ -z "$MOD" ] && usage + + # shellcheck source=modules/test.mod.sh + . "$MOD_FILE" + + for DIR in $("$MOD"_dirs) ; do + if ! [ -d "$USR_DIR/home/$DIR" ] ; then + printf "usr: mkdir %s/home/%s\n" "$USR_DIR" "$DIR" + mkdir -p "$USR_DIR/home/$DIR" + fi + done + + BASE_URL=$(cat "$USR_DIR"/var/remote) + for FILE in $("$MOD"_files) ; do + if ! [ -f "$USR_DIR/home/$FILE" ] ; then + URL="$BASE_URL"home/"$FILE" + printf "usr: getting %s\n" "$URL" + printf "url = %s" "$URL" | curl -sLf -o "$USR_DIR/home/$FILE" -K - || die "usr: error getting $USR_DIR/home/$FILE" & + fi + done } list_modules() { - for MOD in "$USR_DIR"/modules/*; do + for MOD in "$USR_DIR"/modules/* ; do if [ -f "$MOD" ] ; then if [ "${MOD#*.}" = "mod.sh" ] ; then NODOT=${MOD%%.*} @@ -112,7 +138,7 @@ list_modules() { } list_installed() { - for MOD in "$USR_DIR"/var/*; do + for MOD in "$USR_DIR"/var/* ; do if [ -f "$MOD" ] ; then if [ "${MOD#*.}" = "installed" ] ; then NODOT=${MOD%%.*} @@ -123,7 +149,7 @@ list_installed() { } list_remote() { - while read -r MOD; do + while read -r MOD ; do printf "%s\n" "$MOD" done <"$USR_DIR"/var/remote.list } @@ -131,8 +157,8 @@ list_remote() { update_remote() { URL=$(cat "$USR_DIR"/var/remote) URL="$URL"modules/mod.list - printf "getting %s\n" "$URL" - printf "url = %s" "$URL" | curl -sLf -o "$USR_DIR"/var/remote.list -K - || die "error updating" + printf "usr: getting %s\n" "$URL" + printf "url = %s" "$URL" | curl -sLf -o "$USR_DIR"/var/remote.list -K - || die "usr: error updating" } # create module list if it does not exist @@ -142,11 +168,11 @@ update_remote() { [ -e "$USR_DIR"/var/remote ] || printf "https://raw.githubusercontent.com/tteeoo/usr/main/" > "$USR_DIR"/var/remote case ${1#-} in - h) usage ;; a) module_add "[email protected]" ;; r) module_remove "[email protected]" ;; i) module_info "[email protected]" ;; g) get_module "[email protected]" ;; + p) prep_module "[email protected]" ;; l) list_modules ;; u) update_remote ;; li) list_installed ;;