Skip to content

Commit

Permalink
feat: run script itself with sudo
Browse files Browse the repository at this point in the history
  • Loading branch information
DakEnviy committed Nov 18, 2021
1 parent 7ef6725 commit 2675b0d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 54 deletions.
17 changes: 7 additions & 10 deletions scripts/eclt
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,21 @@
AUTO_SUDO=0
EMERGE_LOG_FILE="/var/log/emerge.log"

asudo() {
if [[ ! -r "$EMERGE_LOG_FILE" ]]; then
if [[ "$AUTO_SUDO" -eq 1 ]]; then
sudo $@
sudo "$0" "$@"
exit $?
else
$@
echo "Could not open logfile '$EMERGE_LOG_FILE': Permission denied"
echo "Add your user to portage group or run this command with sudo"
exit
fi
}

if [[ ! -r "$EMERGE_LOG_FILE" && "$AUTO_SUDO" -eq 0 ]]; then
echo "Could not open logfile '$EMERGE_LOG_FILE': Permission denied"
echo "Add your user to portage group or run this command with sudo"
exit
fi

emerge -pvquND --with-bdeps=y @world\
| grep -P '^\['\
| sed -E 's/^\[[^]]+\] ([^ ]+) .+$/\1/;s/-[0-9\.]+(-r[0-9]+)?$//'\
| asudo xargs qlop -aM\
| xargs qlop -aM\
| sort -t " " -k 2 -rn\
| awk '{ total += $2; printf("%s ", substr($1, 1, length($1)-1)); system("date -u +%H:%M:%S -d @"$2); } END { printf "total "; system("date -u +%H:%M:%S -d @"total); }'\
| cat -n\
Expand Down
17 changes: 7 additions & 10 deletions scripts/esync
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@

AUTO_SUDO=0

asudo() {
if [[ "$EUID" -ne 0 ]]; then
if [[ "$AUTO_SUDO" -eq 1 ]]; then
sudo $@
sudo "$0" "$@"
exit $?
else
$@
echo "No root alert!"
echo "Run this command with sudo"
exit
fi
}

if [[ "$EUID" -ne 0 && "$AUTO_SUDO" -eq 0 ]]; then
echo "No root alert!"
echo "Run this command with sudo"
exit
fi

if command -v layman &>/dev/null; then
Expand All @@ -22,6 +19,6 @@ if command -v layman &>/dev/null; then
fi

echo "Syncing repositories..."
asudo emerge --sync -q
emerge --sync -q

echo "Done."
21 changes: 9 additions & 12 deletions scripts/eupd
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,25 @@ AUTO_SUDO=0
COMMON_EMERGE_FLAGS=("--update" "--newuse" "--deep" "--with-bdeps=y" "--keep-going=y" "--autounmask" "--autounmask-write")
DEFAULT_EMERGE_FLAGS=("--ask" "--verbose" "--quiet")

asudo() {
if [[ "$EUID" -ne 0 ]]; then
if [[ "$AUTO_SUDO" -eq 1 ]]; then
sudo $@
sudo "$0" "$@"
exit $?
else
$@
echo "No root alert!"
echo "Run this command with sudo"
exit
fi
}

if [[ "$EUID" -ne 0 && "$AUTO_SUDO" -eq 0 ]]; then
echo "No root alert!"
echo "Run this command with sudo"
exit
fi

esync() {
if command -v layman &>/dev/null; then
echo "Syncing layman overlays..."
asudo layman -S -q
layman -S -q
fi

echo "Syncing repositories..."
asudo emerge --sync -q
emerge --sync -q
}

show_help() {
Expand Down Expand Up @@ -107,7 +104,7 @@ if [[ "$sync" -eq 1 ]]; then
fi

echo "Running emerge..."
asudo emerge "${emerge_flags[@]}" "${COMMON_EMERGE_FLAGS[@]}" "$@" "@world"
emerge "${emerge_flags[@]}" "${COMMON_EMERGE_FLAGS[@]}" "$@" "@world"

if [[ "$depclean" -eq 1 ]]; then
echo "Running depclean..."
Expand Down
21 changes: 9 additions & 12 deletions scripts/qtop
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,25 @@
AUTO_SUDO=0
EMERGE_LOG_FILE="/var/log/emerge.log"

asudo() {
if [[ ! -r "$EMERGE_LOG_FILE" ]]; then
if [[ "$AUTO_SUDO" -eq 1 ]]; then
sudo $@
sudo "$0" "$@"
exit $?
else
$@
echo "Could not open logfile '$EMERGE_LOG_FILE': Permission denied"
echo "Add your user to portage group or run this command with sudo"
exit
fi
}

if [[ ! -r "$EMERGE_LOG_FILE" && "$AUTO_SUDO" -eq 0 ]]; then
echo "Could not open logfile '$EMERGE_LOG_FILE': Permission denied"
echo "Add your user to portage group or run this command with sudo"
exit
fi

with_total=1

if [[ -z "$1" ]]; then
list=$( qlist -CI | asudo xargs qlop -aM | sort -t " " -k 2 -rn )
list=$( qlist -CI | xargs qlop -aM | sort -t " " -k 2 -rn )
elif [[ "$1" =~ ^[0-9]+$ ]]; then
list=$( qlist -CI | asudo xargs qlop -aM | sort -t " " -k 2 -rn | head -n "$1" )
list=$( qlist -CI | xargs qlop -aM | sort -t " " -k 2 -rn | head -n "$1" )
else
list=$( qlist -CI | asudo xargs qlop -aM | sort -t " " -k 2 -rn | grep -i "$1" )
list=$( qlist -CI | xargs qlop -aM | sort -t " " -k 2 -rn | grep -i "$1" )
with_total=0
fi

Expand Down
17 changes: 7 additions & 10 deletions scripts/rcms
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@

AUTO_SUDO=0

asudo() {
if [[ "$EUID" -ne 0 ]]; then
if [[ "$AUTO_SUDO" -eq 1 ]]; then
sudo $@
sudo "$0" "$@"
exit $?
else
$@
echo "No root alert!"
echo "Run this command with sudo"
exit
fi
}

if [[ "$EUID" -ne 0 && "$AUTO_SUDO" -eq 0 ]]; then
echo "No root alert!"
echo "Run this command with sudo"
exit
fi

for service in "${@:2}"; do
asudo rc-service "$service" "$1"
rc-service "$service" "$1"
done

0 comments on commit 2675b0d

Please sign in to comment.