2022-09-27 10:29:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace NoccyLabs\Serverctl\Commands;
|
|
|
|
|
|
|
|
use Symfony\Component\Console\Attribute\AsCommand;
|
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
|
|
|
#[AsCommand(name:"exec", description:"Execute a shell command or script in the service")]
|
|
|
|
class ExecCommand extends Command
|
|
|
|
{
|
|
|
|
|
|
|
|
protected function configure()
|
|
|
|
{
|
2022-09-27 10:47:30 +00:00
|
|
|
$this->addOption("instance", "i", InputOption::VALUE_REQUIRED, "Specify the instance name", "default");
|
2022-09-27 10:29:56 +00:00
|
|
|
$this->addOption("script", "s", InputOption::VALUE_NONE, "The command is a script (use 'list' for list)");
|
|
|
|
$this->addArgument("service", InputArgument::REQUIRED, "The service name");
|
|
|
|
$this->addArgument("execute", InputArgument::REQUIRED, "The command or script to execute");
|
|
|
|
$this->addArgument("arguments", InputArgument::IS_ARRAY, "Arguments");
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
|
|
|
$serviceRegistry = $this->getServiceRegistry();
|
|
|
|
$containerManager = $this->getContainerManager();
|
|
|
|
$instanceName = $input->getOption("instance");
|
|
|
|
|
|
|
|
$serviceName = $input->getArgument("service");
|
|
|
|
$command = $input->getArgument("execute");
|
|
|
|
|
|
|
|
$serviceInfo = $serviceRegistry->findServiceByName($serviceName);
|
|
|
|
|
|
|
|
if ($command == "list") {
|
|
|
|
$scripts = (array)($serviceInfo['scripts']??[]);
|
|
|
|
$output->writeln("Available scripts:");
|
|
|
|
foreach ($scripts as $script=>$meta) {
|
|
|
|
$output->writeln(sprintf(" <comment>%s</> - <info>%s</>", $script, $meta['info']??"?"));
|
|
|
|
}
|
|
|
|
return self::SUCCESS;
|
2022-09-27 23:11:14 +00:00
|
|
|
} elseif ($input->getOption("script")) {
|
|
|
|
$script = $serviceInfo['scripts'][$command]['execute']??null;
|
|
|
|
if (!$script) {
|
|
|
|
$output->writeln("<error>No such script</>");
|
|
|
|
return self::FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
$cmdl = [ is_array($script)?join("; ", $script):$script ];
|
|
|
|
$containerManager->execute($serviceInfo, $instanceName, $cmdl);
|
|
|
|
|
2022-09-27 10:29:56 +00:00
|
|
|
} else {
|
|
|
|
$cmdl = [ $input->getArgument("execute") ];
|
|
|
|
array_push($cmdl, ...$input->getArgument("arguments"));
|
|
|
|
$containerManager->execute($serviceInfo, $instanceName, $cmdl);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|