67 lines
2.7 KiB
PHP
67 lines
2.7 KiB
PHP
<?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:"find", description:"Find defined services")]
|
|
class FindCommand extends Command
|
|
{
|
|
|
|
protected function configure()
|
|
{
|
|
$this->addArgument("service", InputArgument::OPTIONAL, "Search query");
|
|
$this->addOption("environment", "E", InputOption::VALUE_NONE, "Show the environment for the services");
|
|
$this->addOption("ports", "P", InputOption::VALUE_NONE, "Show the ports for the services");
|
|
$this->addOption("info", "I", InputOption::VALUE_NONE, "Show service information text if available");
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
{
|
|
$serviceRegistry = $this->getServiceRegistry();
|
|
|
|
$printEnvs = $input->getOption("environment");
|
|
$printPorts = $input->getOption("ports");
|
|
$showInfo = $input->getOption("info");
|
|
|
|
$services = $serviceRegistry->findAllServices();
|
|
$query = $input->getArgument("service");
|
|
|
|
|
|
$output->writeln($query?"Services matching <options=bold>{$query}</>:":"Available services:");
|
|
foreach ($services as $service) {
|
|
$tags = $service['tags']??[];
|
|
// Filter out, if we have a query
|
|
if ($query && !((in_array($query,$tags) || str_contains($service['description'],$query) || $service['name']==$query)))
|
|
continue;
|
|
|
|
if ($tags) {
|
|
$tags = '#'.join("</>,<fg=cyan>#", $tags)."</>";
|
|
} else {
|
|
$tags = "";
|
|
}
|
|
$output->writeln(sprintf(" <comment>%s</>: <info>%s</> <fg=cyan>%s</>", $service['name'], $service['description']??"?", $tags));
|
|
if ($showInfo && array_key_exists('info', $service)) {
|
|
$info = " ".strtr(wordwrap($service['info']), [ "\n" => "\n " ]);
|
|
$output->writeln("<fg=bright-green>".$info."</>");
|
|
}
|
|
if ($printEnvs) {
|
|
foreach ($service['environment']??[] as $env=>$value) {
|
|
$output->writeln(sprintf(" <fg=cyan>%s</>=<fg=magenta>%s</>", $env, $value));
|
|
|
|
}
|
|
}
|
|
if ($printPorts) {
|
|
foreach ($service['ports']??[] as $port) {
|
|
$output->writeln(sprintf(" Port: <fg=cyan>%s</> (<info>%s</>)", $port['port'], $port['info']??"Unknown"));
|
|
}
|
|
}
|
|
}
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
} |