Adding shell_get and migrating to Exception

This commit is contained in:
Pavel Shevaev 2022-07-21 11:33:01 +03:00
parent 44d935a1bf
commit e34f22a06b
1 changed files with 9 additions and 1 deletions

View File

@ -377,7 +377,7 @@ function shell($cmd, &$out=null)
{ {
shell_try($cmd, $ret, $out); shell_try($cmd, $ret, $out);
if($ret != 0) if($ret != 0)
throw new TaskmanException("Shell execution error(exit code $ret)"); throw new Exception("Shell execution error(exit code $ret)");
} }
function shell_try($cmd, &$ret=null, &$out=null) function shell_try($cmd, &$ret=null, &$out=null)
@ -391,6 +391,14 @@ function shell_try($cmd, &$ret=null, &$out=null)
_execute_proc_cmd($cmd, $ret, $out); _execute_proc_cmd($cmd, $ret, $out);
} }
function shell_get($cmd, $as_string = true)
{
exec($cmd, $out, $code);
if($code !== 0)
throw new Exception("Error($code) executing shell cmd '$cmd'");
return $as_string ? implode("", $out) : $out;
}
function _execute_proc_cmd($cmd, &$ret, &$out) function _execute_proc_cmd($cmd, &$ret, &$out)
{ {
//TODO: do we really need to redirect error stream? //TODO: do we really need to redirect error stream?