Add find, query and set slot commands
This commit is contained in:
@ -2,13 +2,24 @@
|
||||
|
||||
namespace SlotDb\Cli;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use SlotDb\Client\SlotDbClient;
|
||||
|
||||
class Application extends \Symfony\Component\Console\Application
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct("slotcli", "0.0.0");
|
||||
|
||||
$psr = new Client();
|
||||
$client = new SlotDbClient($psr);
|
||||
|
||||
$this->add(new Command\ImportCommand());
|
||||
$this->add(new Command\ExportCommand());
|
||||
|
||||
$this->add(new Command\Slot\SlotQueryCommand($client));
|
||||
$this->add(new Command\Slot\SlotFindCommand($client));
|
||||
$this->add(new Command\Slot\SlotSetCommand($client));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,9 +2,14 @@
|
||||
|
||||
namespace SlotDb\Cli\Command;
|
||||
|
||||
use SlotDb\Cli\Application;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
|
||||
abstract class BaseCommand extends Command
|
||||
{
|
||||
|
||||
public function getApplication(): Application
|
||||
{
|
||||
return parent::getApplication(); // @phpstan-ignore return.type
|
||||
}
|
||||
}
|
||||
|
67
src/Command/Slot/SlotFindCommand.php
Normal file
67
src/Command/Slot/SlotFindCommand.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace SlotDb\Cli\Command\Slot;
|
||||
|
||||
use SlotDb\Client\SlotDbClient;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Helper\Table;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
#[AsCommand(name:"slot:find", aliases:[ "find" ], description:"Find all slots, or slots matching conditions")]
|
||||
class SlotFindCommand extends Command
|
||||
{
|
||||
public function __construct(
|
||||
private readonly SlotDbClient $client
|
||||
)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->addArgument("where", InputArgument::IS_ARRAY, "Conditions that are ANDed")
|
||||
;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
|
||||
$wheres = $input->getArgument("where");
|
||||
|
||||
$whereConds = [];
|
||||
foreach ($wheres as $where) {
|
||||
if (preg_match('<^(.+?)(=|\<=|\>=|\<|\>)(.+?)$>', $where, $match)) {
|
||||
$c = $match[2];
|
||||
if ($c == '=') $c = '';
|
||||
$whereConds[$match[1]] = sprintf("%s%s", $c, $match[3]);
|
||||
}
|
||||
}
|
||||
|
||||
$slots = $this->client->findSlots($whereConds);
|
||||
|
||||
$keys = [];
|
||||
foreach ($slots as $slot) {
|
||||
$keys = array_merge($keys, array_keys($slot));
|
||||
}
|
||||
$keys = array_unique($keys);
|
||||
|
||||
$table = new Table($output);
|
||||
$table->setHeaders($keys);
|
||||
$table->setStyle('box');
|
||||
foreach ($slots as $slot) {
|
||||
$row = [];
|
||||
foreach ($keys as $key) {
|
||||
$row[] = isset($slot[$key]) ? json_encode($slot[$key]) : null;
|
||||
}
|
||||
$table->addRow($row);
|
||||
}
|
||||
|
||||
$table->render();
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
49
src/Command/Slot/SlotQueryCommand.php
Normal file
49
src/Command/Slot/SlotQueryCommand.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace SlotDb\Cli\Command\Slot;
|
||||
|
||||
use SlotDb\Client\SlotDbClient;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
#[AsCommand(name:"slot:query", aliases:[ "query" ], description:"Query a slot and its properties")]
|
||||
class SlotQueryCommand extends Command
|
||||
{
|
||||
public function __construct(
|
||||
private readonly SlotDbClient $client
|
||||
)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->addArgument("slot", InputArgument::REQUIRED, "Slot ID")
|
||||
;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
|
||||
$slotId = $input->getArgument("slot");
|
||||
$slot = $this->client->querySlot($slotId);
|
||||
|
||||
$len = 2 + max(array_map(mb_strlen(...), array_keys($slot)));
|
||||
foreach ($slot as $k=>$v) {
|
||||
$output->writeln(
|
||||
sprintf(
|
||||
str_starts_with($k,"_")
|
||||
? "<fg=green>%{$len}s</>: <fg=gray>%s</>"
|
||||
: "<fg=green;options=bold>%{$len}s</>: <fg=yellow>%s</>",
|
||||
$k, json_encode($v)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
62
src/Command/Slot/SlotSetCommand.php
Normal file
62
src/Command/Slot/SlotSetCommand.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace SlotDb\Cli\Command\Slot;
|
||||
|
||||
use SlotDb\Client\SlotDbClient;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
#[AsCommand(name:"slot:set", aliases:[ "set" ], description:"Update the properties on a slot")]
|
||||
class SlotSetCommand extends Command
|
||||
{
|
||||
public function __construct(
|
||||
private readonly SlotDbClient $client
|
||||
)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->addArgument("slot", InputArgument::REQUIRED, "Slot ID")
|
||||
->addArgument("update", InputArgument::IS_ARRAY, "key=value pairs to update")
|
||||
;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
|
||||
$slotId = $input->getArgument("slot");
|
||||
$updates = $input->getArgument("update");
|
||||
$props = [];
|
||||
foreach ($updates as $update) {
|
||||
if (str_contains($update, "=")) {
|
||||
[$k,$v] = explode("=", $update, 2);
|
||||
$v = json_decode($v) ?? $v;
|
||||
$props[$k] = $v;
|
||||
} else {
|
||||
$props[$update] = null;
|
||||
}
|
||||
}
|
||||
|
||||
$slot = $this->client->updateSlot($slotId, $props);
|
||||
|
||||
$len = 2 + max(array_map(mb_strlen(...), array_keys($slot)));
|
||||
foreach ($slot as $k=>$v) {
|
||||
$output->writeln(
|
||||
sprintf(
|
||||
str_starts_with($k,"_")
|
||||
? "<fg=green>%{$len}s</>: <fg=gray>%s</>"
|
||||
: "<fg=green;options=bold>%{$len}s</>: <fg=yellow>%s</>",
|
||||
$k, json_encode($v)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user