From 2f1888dc74d32bb033b898bf10fa7aab76fad0c5 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Fri, 3 May 2019 10:38:58 +0200 Subject: [PATCH] Manage noroot correctly --- bin/update | 72 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 28 deletions(-) diff --git a/bin/update b/bin/update index 9139c1b..f1982a4 100755 --- a/bin/update +++ b/bin/update @@ -67,6 +67,12 @@ _check_date() { 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" sudo true if [ $? -ne 0 ]; then @@ -131,47 +137,57 @@ update-rust() { cargo install-update --help > /dev/null 2>&1 + # Program to update cargo programs is not installed if [ $? -ne 0 ]; then + # This program requires openssl, so check that it is installed pkg-config --libs --cflags openssl > /dev/null 2>&1 + installed_openssl=$? # We need to install openssl - if [ $? -ne 0 ]; then + if [ $installed_openssl -ne 0 ]; then - # Ask for sudo right now - sudoresult=$(sudo -nv 2>&1) + # If the user has root power + if [ ! -f ~/.config/dotfiles/.data/noroot ]; then - if [ $? -eq 0 ]; then - command -v apt > /dev/null 2>&1 - if [ $? -eq 0 ]; then - sudo apt install -y libssl-dev - fi - - # For fedora - command -v dnf > /dev/null 2>&1 - if [ $? -eq 0 ]; then - sudo dnf install openssl-devel - fi - 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" + echo -e "\033[32;1m=== libssl-dev is needed to update rust packages ===\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 + if [ $? -eq 0 ]; then + + # For ubuntu + command -v apt > /dev/null 2>&1 + if [ $? -eq 0 ]; then + sudo apt install -y libssl-dev + installed_openssl=0 + fi + + # For fedora + command -v dnf > /dev/null 2>&1 + if [ $? -eq 0 ]; then + sudo dnf install openssl-devel + installed_openssl=0 + fi + + fi + + fi fi - echo -e "\033[32;1m=== Installing rust packages updater ===\033[0m" - cargo install cargo-update + # 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" + cargo install cargo-update + fi fi - echo -e "\033[32;1m=== Updating rust packages ===\033[0m" - cargo install-update -ag + # 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" + cargo install-update -ag + fi seconds=$((`date +%s` - $start_rust_update )) formatted=`date -ud "@$seconds" +'%H hours %M minutes %S seconds'`