From 99c01d4bb74964fd125b9c6cfc12aca511c626be Mon Sep 17 00:00:00 2001 From: "kebler.net" Date: Thu, 9 Feb 2023 08:34:18 -0800 Subject: [PATCH] add sudo module with Sudo function for running functions or alias as sudo add install-shell-repo library for easy install of other shell repos (host, network) --- functions/01-basic | 6 +---- modules/install-shell-repos.lib | 47 +++++++++++++++++++++++++++++++++ modules/utility/sudo.lib | 21 +++++++++++++++ 3 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 modules/install-shell-repos.lib create mode 100644 modules/utility/sudo.lib diff --git a/functions/01-basic b/functions/01-basic index ef82422..86dafd0 100644 --- a/functions/01-basic +++ b/functions/01-basic @@ -2,6 +2,7 @@ # 00 will get loaded first ## +module_load systemd user_reload() { save=$PWD @@ -89,10 +90,5 @@ osinfo () { cat /etc/upstream-release/lsb-release } -fsudo () # run a function as sudo -{ - [[ "$(type -t $1)" == "function" ]] && - ARGS="$@" && sudo bash -c "$(declare -f $1); $ARGS" -} alias ssudo="ssudo " \ No newline at end of file diff --git a/modules/install-shell-repos.lib b/modules/install-shell-repos.lib new file mode 100644 index 0000000..856ad74 --- /dev/null +++ b/modules/install-shell-repos.lib @@ -0,0 +1,47 @@ +#!/bin/bash +# assumes that bash is installed +#!/bin/bash + +# must! be run as sudo + + +install_shell_repo () { + # TODO have a cross distro package install + [[ ! $(which git) ]] && apt-get install --no-install-recommends git -y + [[ ! $EUID -eq 0 ]] && { echo ERROR script must be run as root; return; } + local user + [[ $1 == "-u" ]] && user=$2 && shift 2 + local dir=${2:-/shell/any} + local repo=${1:-host} + local rpath=$dir/$repo + mkdir -p $rpath + # TODO set git host via option or env var + git clone https://git.kebler.net/bash/shell-$repo.git $rpath + chown -R ${user:-1000}:${user:-1000} $rpath + chmod -R +r $rpath +} + +install_shell_host () { + module_load sudo + local user + [[ $1 == "-u" ]] && user="-u $2" && shift 2 + Sudo install_shell_repo $user host $1 +} + +install_shell_network () { + module_load sudo + local user + [[ $1 == "-u" ]] && user="-u $2" && shift 2 + Sudo install_shell_repo $user network $1 + } + +update_shell_host () { + cd $BASH_SHELL_ANY_HOST || { echo not directory $BASH_SHELL_ANY_HOST; return 2; } + git pull origin master + } + +update_shell_network () { + cd $BASH_SHELL_ANY_NETWORK || { echo not directory $BASH_SHELL_ANY_NETWORK; return 2; } + git pull origin master + } + diff --git a/modules/utility/sudo.lib b/modules/utility/sudo.lib new file mode 100644 index 0000000..2ef2805 --- /dev/null +++ b/modules/utility/sudo.lib @@ -0,0 +1,21 @@ +#!/bin/bash + +fsudo () # run a function as sudo +{ + [[ "$(type -t $1)" == "function" ]] && + ARGS="$@" && sudo bash -c "$(declare -f $1); $ARGS" +} + +function Sudo { + local firstArg=$1 + if [ $(type -t $firstArg) = function ] + then + shift && command sudo bash -c "$(declare -f $firstArg);$firstArg $*" + elif [ $(type -t $firstArg) = alias ] + then + alias sudo='\sudo ' + eval "sudo $@" + else + command sudo "$@" + fi +} \ No newline at end of file