Pipe improvements, misc cleanup

* Pipe improvements; better filter code, pipeline etc.
* Moved commands in PDO plugin to dedicated namespace
This commit is contained in:
2021-12-23 15:31:26 +01:00
parent 9050c74a08
commit f4257b39e4
11 changed files with 254 additions and 15 deletions

View File

@ -4,6 +4,8 @@ namespace Spark\Commands;
use Symfony\Component\Console\Attribute\AsCommand;
use Spark\Commands\Command;
use Spark\Pipe\Filters\ProgressFilter;
use Spark\Pipe\Pipeline;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
@ -17,9 +19,9 @@ class PipeCommand extends Command
protected function configure()
{
$this->addOption("list-filters", null, InputOption::VALUE_NONE, "List the defined filters");
$this->addOption("fdin", null, InputOption::VALUE_REQUIRED, "Input fd, for reading from", 0);
$this->addOption("fdout", null, InputOption::VALUE_REQUIRED, "Output fd, for writing to", 1);
$this->addOption("fderr", null, InputOption::VALUE_REQUIRED, "Error fd, for progress report and status", 2);
$this->addOption("input", "i", InputOption::VALUE_REQUIRED, "Input file or fd, for reading from", 0);
$this->addOption("output", "o", InputOption::VALUE_REQUIRED, "Output file or fd, for writing to", 1);
$this->addOption("error", "e", InputOption::VALUE_REQUIRED, "Error file or fd, for progress report and status", 2);
$this->addArgument("filter", InputArgument::OPTIONAL, "Pipe filter");
$this->addArgument("args", InputArgument::OPTIONAL|InputArgument::IS_ARRAY, "Arguments to the script");
$this->registerDefaultFilters();
@ -35,6 +37,7 @@ class PipeCommand extends Command
$hashed = password_hash($trimmed, PASSWORD_BCRYPT);
return str_replace($trimmed, $hashed, $in);
});
register_filter("progress", ProgressFilter::class);
}
protected function execute(InputInterface $input, OutputInterface $output)
@ -46,17 +49,44 @@ class PipeCommand extends Command
return Command::SUCCESS;
}
$fdin = "php://fd/".$input->getOption("fdin");
$fdout = "php://fd/".$input->getOption("fdout");
$fderr = "php://fd/".$input->getOption("fderr");
$fdin = $input->getOption("input");
$fdout = $input->getOption("output");
$fderr = $input->getOption("error");
$filterargs = $input->getArgument('args');
$args = [];
foreach ($filterargs as $arg) {
if (str_contains($arg, '=')) {
[$k,$v] = explode("=", $arg, 2);
$args[$k] = $v;
} else {
if (str_starts_with($arg, 'no-')) {
$args[substr($arg,3)] = false;
} else {
$args[$arg] = true;
}
}
}
$filtername = $input->getArgument("filter");
if ($filtername) {
$filter = get_filter($filtername);
$filter = get_filter($filtername, $args);
} else {
$filter = null;
}
$pipeline = new Pipeline();
$pipeline->setInputFile($fdin);
$pipeline->setOutputFile($fdout);
if ($filter) {
$pipeline->addFilter($filter);
}
$pipeline->run();
/*
$fin = fopen($fdin, "rb");
$fout = fopen($fdout, "wb");
while (!feof($fin)) {
@ -64,6 +94,7 @@ class PipeCommand extends Command
if (is_callable($filter)) $buf = $filter($buf);
fputs($fout, $buf);
}
*/
return Command::SUCCESS;
}