Manage noroot correctly

This commit is contained in:
Thomas Forgione 2019-05-03 10:38:58 +02:00
parent 8148b39bf4
commit 2f1888dc74
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
1 changed files with 44 additions and 28 deletions

View File

@ -67,6 +67,12 @@ _check_date() {
update-system() { update-system() {
if [ -f ~/.config/dotfiles/.data/noroot ]; then
# If this file exists, it means root is not available: skip the system update
echo -e "\033[33;1m=== You don't have root, skipping system update ===\033[0m"
fi
echo -e "\033[32;1m=== Starting system update, please enter your password ===\033[0m" echo -e "\033[32;1m=== Starting system update, please enter your password ===\033[0m"
sudo true sudo true
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
@ -131,47 +137,57 @@ update-rust() {
cargo install-update --help > /dev/null 2>&1 cargo install-update --help > /dev/null 2>&1
# Program to update cargo programs is not installed
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
# This program requires openssl, so check that it is installed
pkg-config --libs --cflags openssl > /dev/null 2>&1 pkg-config --libs --cflags openssl > /dev/null 2>&1
installed_openssl=$?
# We need to install openssl # We need to install openssl
if [ $? -ne 0 ]; then if [ $installed_openssl -ne 0 ]; then
# Ask for sudo right now # If the user has root power
sudoresult=$(sudo -nv 2>&1) if [ ! -f ~/.config/dotfiles/.data/noroot ]; then
echo -e "\033[32;1m=== libssl-dev is needed to update rust packages ===\033[0m"
sudo true
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
# For ubuntu
command -v apt > /dev/null 2>&1 command -v apt > /dev/null 2>&1
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
sudo apt install -y libssl-dev sudo apt install -y libssl-dev
installed_openssl=0
fi fi
# For fedora # For fedora
command -v dnf > /dev/null 2>&1 command -v dnf > /dev/null 2>&1
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
sudo dnf install openssl-devel sudo dnf install openssl-devel
fi installed_openssl=0
elif echo $sudoresult | grep -q '^sudo:'; then
echo -e "\033[32;1m=== libssl-dev is needed to update rust packages, please enter your password ===\033[0m"
sudo true
if [ $? -ne 0 ]; then
echo "Could not get sudo..."
return 1
fi
else
echo -e "\033[33;1m=== You are not a sudoer, cannot install cargo-update... ===\033[0m"
return 1
fi fi
fi fi
fi
fi
# Only try to install if openssl is installed
if [ $installed_openssl -eq 0 ]; then
echo -e "\033[32;1m=== Installing rust packages updater ===\033[0m" echo -e "\033[32;1m=== Installing rust packages updater ===\033[0m"
cargo install cargo-update cargo install cargo-update
fi fi
fi
# If the user has no root, and if he doesn't have openssl, we won't install cargo update.
# Before running cargo update, we check again that it is installed.
cargo install-update --help > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo -e "\033[32;1m=== Updating rust packages ===\033[0m" echo -e "\033[32;1m=== Updating rust packages ===\033[0m"
cargo install-update -ag cargo install-update -ag
fi
seconds=$((`date +%s` - $start_rust_update )) seconds=$((`date +%s` - $start_rust_update ))
formatted=`date -ud "@$seconds" +'%H hours %M minutes %S seconds'` formatted=`date -ud "@$seconds" +'%H hours %M minutes %S seconds'`