Skip to content

Instantly share code, notes, and snippets.

@gfldex
Created June 6, 2020 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gfldex/62f605fc812b959ba324b9582fb582ee to your computer and use it in GitHub Desktop.
Save gfldex/62f605fc812b959ba324b9582fb582ee to your computer and use it in GitHub Desktop.
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