]> gitweb.factorcode.org Git - factor.git/commitdiff
elevate rewrite
authorCat Stevens <catb0t@protonmail.ch>
Fri, 18 May 2018 16:08:24 +0000 (12:08 -0400)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 25 Jan 2022 18:28:04 +0000 (10:28 -0800)
basis/elevate/elevate-docs.factor
basis/elevate/elevate.factor

index ae9b664daa27c4733976146ee20bb959ee10c981..494d376eb990a986d2cf013dea0ccc0a63dd7754 100644 (file)
@@ -4,5 +4,9 @@ IN: elevate
 ABOUT: elevate
 
 ARTICLE: "elevate" "Cross-platform API for elevated permissions"
-    "Thanks to " { $url "https://github.com/barneygales/elevate" }
+    "Thanks to " { $url "https://github.com/barneygale/elevate" }
 ;
+
+HELP: elevated
+{ $values { "command" { $or array string } } }
+{ $description } ;
\ No newline at end of file
index 8640288cf5c08a37a9e1ec97d380368edca86e10..afedf350f80bcd457c40f65d14da92eb06400616 100644 (file)
@@ -2,29 +2,41 @@ USING: arrays command-line fry io.launcher kernel math namespaces
 sequences system unix.ffi ;
 IN: elevate
 
-: apple-script-elevate ( x x -- ) 2drop ;
+: apple-script-elevate ( command -- ) 2drop ;
 
-HOOK: elevate os ( win-console? posix-graphical? -- )
+GENERIC: glue-command ( prefix command -- glued )
 
-M: windows elevate 2drop ;
+M: array glue-command
+    swap prefix ;
 
-M: macosx elevate
-    [   ! graphical (through applescript)
-        t apple-script-elevate
+M: string glue-command
+    " " glue ;
+
+ERROR: elevated-failed path ;
+
+HOOK: elevated os ( command win-console? posix-graphical? -- process )
+
+M: windows elevated
+    2drop run-process ;
+
+M: macosx elevated
+    nip [ ! graphical (through applescript)
+        apple-script-elevate
     ] [
-        f linux os [ elevate ] with-variable
+        f f linux os [ elevated ] with-variable
     ] if ;
 
-M: linux elevate
-    getuid zero? [
-        2drop ! we are already root: do nothing
+M: linux elevated
+    nip getuid zero? [
+        drop ! we are already root: do nothing
     ] [
-        ! graphical on linuxes
-        nip [ { "gksudo" "kdesudo" } ] [ { } ] if
-        "sudo" suffix (command-line) '[ 1array _ append ] map
-        [
-            run-process drop
-        ] each
+        { "gksudo" "kdesudo" "sudo" } { "sudo" } ? ! graphical handled
+        swap '[ _ glue-command ] map
+        [ " " split [ first utf8 string>alien ] [ rest ] execvp ] map
+        [ -1 = ] all? elevated-failed
     ] if ;
 
-HOOK: lower os ( relaunch? -- )
+: elevate ( option? -- ) (command-line) elevated ;
+
+HOOK: lowered os ( relaunch? -- )
+