From 4d85aa1ebcca4f00ef877e0c93d5305bc5eb97ba Mon Sep 17 00:00:00 2001 From: zawz Date: Tue, 26 May 2020 11:20:51 +0200 Subject: [PATCH] Cleanup + root fixes --- src/config.sh | 25 ++++++++++++------------- src/install.sh | 18 ++++++++---------- src/main.sh | 26 +++++++++----------------- src/print.sh | 8 ++++++-- src/remove.sh | 12 +++++------- src/util.sh | 7 +------ src/view.sh | 9 ++------- 7 files changed, 43 insertions(+), 62 deletions(-) diff --git a/src/config.sh b/src/config.sh index 16fb520..8ea00a3 100644 --- a/src/config.sh +++ b/src/config.sh @@ -12,25 +12,24 @@ virtual_config_path() # resolve relative config_path config_path="$(resolve_path "$config_path" "$(pwd)")" - config_file="$config_path/zpkg.conf" -if [ ! -d "$config_path" ] -then - sudo mkdir -p "$config_path" 2>/dev/null -fi -if [ ! -f "$config_file" ] -then - echo "Error: no config file '$config_file'" >&2 - exit 1 +# setup sudo prefix +unset sudo +if ! root_check ; then + which sudo >/dev/null || { echo "sudo not installed" && exit 11; } + sudo=sudo fi +[ ! -d "$config_path" ] && { $sudo mkdir -p "$config_path" 2>/dev/null || exit $?; } +[ ! -f "$config_file" ] && echo "Error: no config file '$config_file'" >&2 && exit 1 + . "$config_file" # resolve relative pkg_path PKG_PATH="$(resolve_path "$PKG_PATH" "$config_path")" -if [ ! -d "$PKG_PATH" ] -then - sudo mkdir -p "$PKG_PATH" 2>/dev/null -fi +root_check && [ -z "$opt_f" ] && [ "$ALLOW_ROOT" != "true" ] && echo "Cannot run as root" >&2 && exit 10 + +[ ! -d "$PKG_PATH" ] $sudo mkdir -p "$PKG_PATH" 2>/dev/null + diff --git a/src/install.sh b/src/install.sh index d349518..2cc7f38 100644 --- a/src/install.sh +++ b/src/install.sh @@ -6,15 +6,16 @@ unpack() tar -xf "$1" } +# $1 = package , $2 = prefix add_package_entry() { ( cd "$PKG_PATH" if grep -q -w "$1" installed 2>/dev/null then - sudo sed "s|$1 .*\$|$1 $(date +%s)|g" -i installed + $2 sed "s|$1 .*\$|$1 $(date +%s)|g" -i installed else - sudo sh -c "echo '$1 $(date +%s)' >> installed" + $2 sh -c "echo '$1 $(date +%s)' >> installed" fi ) } @@ -37,22 +38,19 @@ move_files() done } +# $1 = package , $2 = prefix install_package() { tmpdir="/tmp/zpkg_$(random_string 5)" mkdir -p "$tmpdir" ( cd "$tmpdir" - if ! fetch_package "$1" - then - echo "Package '$1' not found" >&2 - return 1 - fi - sudo cp "$1.tar.xz" "$PKG_PATH" + fetch_package "$1" || { echo "Package '$1' not found" >&2 && return 1; } + $2 cp "$1.tar.xz" "$PKG_PATH" unpack "$1.tar.xz" || return $? - move_files ROOT / sudo 2>/dev/null + move_files ROOT / $2 2>/dev/null move_files HOME "$HOME" 2>/dev/null - add_package_entry "$1" + add_package_entry "$1" $2 ) ret=$? rm -rd "$tmpdir" 2>/dev/null diff --git a/src/main.sh b/src/main.sh index 8afeb2a..a58647a 100644 --- a/src/main.sh +++ b/src/main.sh @@ -1,14 +1,9 @@ #!/bin/sh -if [ -z "$opt_f" ] ; then - root_check || exit 10 -fi - - case "$1" in list) awk '{print $1}' "$PKG_PATH/installed" 2>/dev/null | sort ;; list-all) awk '{print $1}' "$PKG_PATH/pkglist" 2>/dev/null | sort ;; -update-database) fetch_pkglist sudo ;; +update-database) fetch_pkglist $sudo ;; fetch) if [ -z "$2" ] then @@ -60,7 +55,7 @@ info) fi ;; install) - fetch_pkglist sudo || exit $? + fetch_pkglist $sudo || exit $? if [ -z "$2" ] then echo "No package specified" >&2 @@ -71,11 +66,8 @@ install) echo "Installing packages: $pkglist" for I in $pkglist do - if is_installed "$I" - then - remove_package "$I" - fi - install_package "$I" + is_installed $I && remove_package $I $sudo + install_package $I $sudo done fi ;; @@ -87,12 +79,12 @@ remove) shift 1 for I in $* do - remove_package "$I" + remove_package "$I" $sudo done fi ;; update) - fetch_pkglist sudo || exit 1 + fetch_pkglist $sudo || exit 1 r_pkg=$(removed_packages) o_pkg=$(outdated_packages) if [ -n "$r_pkg" ] @@ -100,7 +92,7 @@ update) echo "Removing packages: "$r_pkg for I in $r_pkg do - remove_package $I + remove_package $I $sudo done fi if [ -n "$o_pkg" ] @@ -108,8 +100,8 @@ update) echo "Updating packages: "$o_pkg for I in $o_pkg do - remove_package $I - install_package $I + remove_package $I $sudo + install_package $I $sudo done fi ;; diff --git a/src/print.sh b/src/print.sh index 5ea99a3..2710cd7 100644 --- a/src/print.sh +++ b/src/print.sh @@ -22,9 +22,13 @@ Admin operations: Options: -h Display this help - -c Custom config path + -c Custom config path. Default /etc/zpkg -f Force running when root -" + +Config (zpkg.conf): + SSH_ADDRESS SSH access for deploy + HTTP_ADDRESS HTTP address for downloading packages + ALLOW_ROOT Set to true to allow running as root without -f. Default: false" } error() { diff --git a/src/remove.sh b/src/remove.sh index fc038bb..bbacc43 100644 --- a/src/remove.sh +++ b/src/remove.sh @@ -6,13 +6,11 @@ delete_files() { while read -r in do - if [ -n "$in" ] - then - $1 rm -d "$in" 2>/dev/null - fi + [ -n "$in" ] && $1 rm -d "$in" 2>/dev/null done } +# $1 = package , $2 = prefix remove_package() { cd "$PKG_PATH" @@ -26,13 +24,13 @@ remove_package() ( # delete root files cd / - tar -tf "$archive" ROOT 2>/dev/null | sed 's|^ROOT/||g' | tac | delete_files sudo + tar -tf "$archive" ROOT 2>/dev/null | sed 's|^ROOT/||g' | tac | delete_files $2 ) ( # delete home files cd "$HOME" tar -tf "$archive" HOME 2>/dev/null | sed 's|^HOME/||g' | tac | delete_files ) - sudo rm "$archive" 2>/dev/null - sudo sed -i "/^$1 /d" installed + $2 rm "$archive" 2>/dev/null + $2 sed -i "/^$1 /d" installed } diff --git a/src/util.sh b/src/util.sh index 7e6f0c9..b661f47 100644 --- a/src/util.sh +++ b/src/util.sh @@ -26,10 +26,5 @@ getname() # return error if user is root root_check() { - if [ "$(id | cut -d'=' -f2 | cut -d'(' -f1)" -eq 0 ] - then - echo "Cannot run as root" >&2 - return 1 - fi - return 0 + [ "$(id | cut -d'=' -f2 | cut -d'(' -f1)" -eq 0 ] } diff --git a/src/view.sh b/src/view.sh index b462e1a..176cc12 100644 --- a/src/view.sh +++ b/src/view.sh @@ -68,9 +68,7 @@ removed_packages() do name=$(echo "$in" | awk '{print $1}') rem=$(grep -w "^$name" pkglist | awk '{print $2}') - if [ -z "$rem" ] ; then - echo $name - fi + [ -z "$rem" ] && echo $name done } @@ -82,9 +80,6 @@ outdated_packages() name=$(echo "$in" | awk '{print $1}') loc=$(echo "$in" | awk '{print $2}') rem=$(grep -w "^$name" pkglist | awk '{print $2}') - if [ -n "$rem" ] && [ "$loc" -lt "$rem" ] - then - echo $name - fi + [ -n "$rem" ] && [ "$loc" -lt "$rem" ] && echo $name done }