Watcher plugin fixes

* com.noccy.watcher: Initial inotify support
* ScriptRunner now accepts an array of local vars for expansion
  when evaluating scripts
This commit is contained in:
2021-12-15 03:47:39 +01:00
parent 30dfd4889b
commit 1125ccb82d
5 changed files with 135 additions and 12 deletions

View File

@ -26,16 +26,16 @@ class ScriptRunner
$this->evaluate($script);
}
public function evaluate(string|array $script)
public function evaluate(string|array $script, array $locals=[])
{
if (is_array($script)) {
foreach ($script as $step) {
$this->evaluate($step);
$this->evaluate($step, $locals);
}
return;
}
$script = $this->expandString($script);
$script = $this->expandString($script, $locals);
// Determine what to do
if (str_starts_with($script, '@')) {
@ -81,10 +81,12 @@ class ScriptRunner
}
}
public function expandString(string $input)
public function expandString(string $input, array $locals=[])
{
return preg_replace_callback('/(\$\{(.+?)\})/', function ($match) {
return preg_replace_callback('/(\$\{(.+?)\})/', function ($match) use ($locals) {
if (array_key_exists($match[2], $locals)) {
return $locals[$match[2]];
}
return ($_ENV[$match[2]]??getenv($match[2]))??null;
}, $input);
}