diff options
author | Nathan Gass | 2013-03-21 11:20:33 +0000 |
---|---|---|
committer | Nathan Gass | 2013-03-21 11:20:33 +0000 |
commit | d76a84f251e8f79fcc959d5baed2b4c2c25c06d6 (patch) | |
tree | 3102b68171e1e824188bd8a7b6eb2abe32d84530 | |
parent | dfc0ed2c31ee75ded940147fe8c1963ce42c810e (diff) | |
download | itools-d76a84f251e8f79fcc959d5baed2b4c2c25c06d6.tar.gz itools-d76a84f251e8f79fcc959d5baed2b4c2c25c06d6.tar.bz2 itools-d76a84f251e8f79fcc959d5baed2b4c2c25c06d6.zip |
add new function it::system
-rw-r--r-- | it.class | 26 | ||||
-rwxr-xr-x | tests/exec.t | 17 |
2 files changed, 43 insertions, 0 deletions
@@ -504,6 +504,32 @@ static function exec(/* $cmd, $values1 = array(), ... */) } /** + * Construct shell command using it::shell_command, log it, execute it and return exit code. + * stdout/stderr is forwarded to stdout/stderror of calling script + * {keyword} quotes and inserts value from assoc array like ET() + * {0} .. {n} quotes and inserts positional arguments + * {-opts} takes an array and inserts options a la it_html attributes (value, true, false or null) + * @param $cmd Format string with {keywords} a la ET() + * @param $values (zero, one or more arrays can be passed) + * @return exit code of command. + */ +static function system(/* $cmd, $values1 = array(), ... */) +{ + $args = func_get_args(); + $cmd = call_user_func_array('it::shell_command', $args); + + $before = microtime(true); + if (!EDC('noexec')) + system($cmd, $result); + else + $result = 0; + + @it::log('exec', round((microtime(true) - $before)*1000) . "\t$cmd"); + + return $result; +} + +/** * Construct shell command * Keywords {keyword} are replace a la ET(), {-opts} takes an array and * inserts options a la it_html attributes (value, true, false or null) diff --git a/tests/exec.t b/tests/exec.t index 349f0ed..6418716 100755 --- a/tests/exec.t +++ b/tests/exec.t @@ -22,3 +22,20 @@ foreach (array("", "C", "de_CH", "de_CH.utf8") as $locale) { is(it::_exec_quotevalue(""), "''", "empty arg needs quotes"); is(it::_exec_quotevalue("*"), "'*'", "special chars need quotes"); is(it::_exec_quotevalue("Aabcdef0123456789"), "Aabcdef0123456789", "simple case. tel:debug_getdata needs unquoted vals"); + +is(it::system('exit 0'), 0, 'return exit code 0'); +is(it::system('exit 1'), 1, 'return exit code 1'); +is(it::system('exit -1'), 255, 'return exit code unsigned'); + +@unlink('/tmp/it_system_test'); +it::system('touch /tmp/it_system_test'); +ok(file_exists('/tmp/it_system_test'), 'shell command gets executed'); + +@unlink('/tmp/it_system_test'); +it::system('touch {path}', 'path' => '/tmp/it_system_test'); +ok(file_exists('/tmp/it_system_test'), 'shell command with argument'); + +@unlink('/tmp/it_system_test'); +it::system('touch {0}', '/tmp/it_system_test'); +ok(file_exists('/tmp/it_system_test'), 'shell command with positional argument'); +@unlink('/tmp/it_system_test'); |