Created
June 6, 2020 21:12
-
-
Save gfldex/62f605fc812b959ba324b9582fb582ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use v6; | |
class X::Proc::CommandNotFound is X::Proc::Unsuccessful { | |
method message { | |
my $symlink = $.proc.command[0].IO.l ?? ' (symlink)' !! ''; | |
"The command '{$.proc.command[0]}'$symlink was not found." | |
} | |
} | |
class X::Proc::CommandNotAFile is X::Proc::Unsuccessful { | |
method message { | |
my $symlink = $.proc.command[0].IO.l ?? ' (symlink)' !! ''; | |
"The command '{$.proc.command[0]}'$symlink is not a file." | |
} | |
} | |
class X::Proc::NoExec is X::Proc::Unsuccessful { | |
method message { | |
my $symlink = $.proc.command[0].IO.l ?? ' (symlink)' !! ''; | |
"The path '{$.proc.command[0]}'$symlink is not executable." | |
} | |
} | |
class X::Proc::NonZeroExit is X::Proc::Unsuccessful { | |
method message { | |
my $symlink = $.proc.command[0].IO.l ?? ' (symlink)' !! ''; | |
"The spawned command '{$.proc.command[0]}'$symlink returned with exitcode {$.proc.exitcode}." | |
} | |
} | |
sub use-proc-fatal { | |
&run.wrap(-> |c { | |
my Proc:D $ret := callsame; | |
if $ret.exitcode > 0 { | |
with $ret.command[0].IO { | |
X::Proc::CommandNotFound.new(:proc($ret)).throw unless .e; | |
X::Proc::CommandNotAFile.new(:proc($ret)).throw unless .f; | |
X::Proc::NoExec.new(:proc($ret)).throw unless .x; | |
} | |
X::Proc::Unsuccessful.new(:proc($ret)).throw if $ret.exitcode > 0 || $ret.signal > 0; | |
} | |
$ret | |
}); | |
&shell.wrap(-> |c { | |
my Proc:D $ret := callsame; | |
if $ret.exitcode > 0 { | |
with $ret.command[0].IO { | |
X::Proc::CommandNotFound.new(:proc($ret)).throw unless .e; | |
X::Proc::CommandNotAFile.new(:proc($ret)).throw unless .f; | |
X::Proc::NoExec.new(:proc($ret)).throw unless .x; | |
} | |
X::Proc::Unsuccessful.new(:proc($ret)).throw if $ret.exitcode > 0 || $ret.signal > 0; | |
} | |
$ret | |
}); | |
} | |
use-proc-fatal; | |
{ | |
say ‚### Proc.sink‘; | |
my $p = Proc.new; | |
$p.spawn(<does-not-exist>); | |
$p.sink; | |
CATCH { | |
default { say .^name, ‚: ‘, .message; } | |
} | |
} | |
{ | |
say ‚### run does-not-exist‘; | |
run 'does-not-exist'; | |
CATCH { | |
default { say .^name, ‚: ‘, .message; } | |
} | |
} | |
{ | |
say ‚### run /dev/null‘; | |
run '/dev/null'; | |
CATCH { | |
default { say .^name, ‚: ‘, .message; } | |
} | |
} | |
{ | |
say ‚### run /home‘; | |
run '/home'; | |
CATCH { | |
default { say .^name, ‚: ‘, .message; } | |
} | |
} | |
{ | |
say ‚### run $*PROGRAM‘; | |
run $*PROGRAM; | |
CATCH { | |
default { say .^name, ‚: ‘, .message; } | |
} | |
} | |
{ | |
say ‚### run /usr/sbin/usermod‘; | |
run '/usr/sbin/usermod', :err; | |
CATCH { | |
default { say .^name, ‚: ‘, .message; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment