]> gitweb.fluxo.info Git - trashman.git/commitdiff
Feat: trashman: try to invoke with sudo if user is not root
authorSilvio Rhatto <rhatto@riseup.net>
Sun, 6 Dec 2020 01:03:53 +0000 (22:03 -0300)
committerSilvio Rhatto <rhatto@riseup.net>
Sun, 6 Dec 2020 01:03:53 +0000 (22:03 -0300)
README.md
share/trashman/trashman/functions
trashman

index cf9abc738a2649a025ddda7917eee54e688e1e1e..e0fb43acccdb17866c0c2259892bc1a7567ce9d5 100644 (file)
--- a/README.md
+++ b/README.md
@@ -26,7 +26,7 @@ by this one:
 
     trashman install <package>
 
-Instead of being just a shorthand, `trashman` packages try as best as possible
+Instead of being just a shorthand, `trashman` packages can try as best as possible
 not to fetch keys and scripts from remote locations without some basic checks. So if
 you have to add files into `/etc/apt/trusted.gpg.d`, `trashman` will provide those
 keys or fingerprints instead of donwloading it from somewhere, in which case it could
index 61102e9533dc84029419965dc0428d1810176b49..48700cd72f9821ecad7903ce55733723d390059b 100644 (file)
@@ -203,6 +203,7 @@ trashman_check_exit_status() {
 # Check for superuser privileges
 trashman_check_root() {
   local action="$1"
+  local package="$2"
 
   if [ -z "$action" ]; then
     return
@@ -213,8 +214,15 @@ trashman_check_root() {
   fi
 
   if [ "`whoami`" != "root" ]; then
-    trashman_echo "Action $action requires root privileges"
-    exit 1
+    if which sudo &> /dev/null; then
+      # Try to invoke it with sudo
+      trashman_echo "You're running as a regular user, trying to re-invoke the command using sudo..."
+      sudo $PROGRAM $action $package
+      exit $?
+    else
+      trashman_echo "Action $action requires root privileges"
+      exit 1
+    fi
   fi
 }
 
index 8ffa0b46181ce470d42e786f404f4bf116fed400..32fa6e2c97af6737331c7b4fad0ea65478c7db2a 100755 (executable)
--- a/trashman
+++ b/trashman
@@ -91,7 +91,7 @@ else
 
         # Checking for privileges
         if [ "$BASENAME" = "trashman" ]; then
-          trashman_check_root $ACTION
+          trashman_check_root $ACTION $package
         else
           trashman_check_sudo
         fi