87 lines
2.6 KiB
PHP
87 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace Toolbox;
|
|
|
|
use Toolbox\Commands\AddCommand;
|
|
use Toolbox\Commands\AdoptCommand;
|
|
use Toolbox\Commands\CatalogCommand;
|
|
use Toolbox\Commands\CommandsCommand;
|
|
use Toolbox\Commands\DoctorCommand;
|
|
use Toolbox\Commands\InstallCommand;
|
|
use Toolbox\Commands\ListCommand;
|
|
use Toolbox\Commands\RemoveCommand;
|
|
use Toolbox\Commands\SelfUpdateCommand;
|
|
use Toolbox\Commands\UpdateCommand;
|
|
|
|
final class Cli
|
|
{
|
|
/** @var array<string, class-string> */
|
|
private const COMMANDS = [
|
|
'install' => InstallCommand::class,
|
|
'add' => AddCommand::class,
|
|
'remove' => RemoveCommand::class,
|
|
'update' => UpdateCommand::class,
|
|
'list' => ListCommand::class,
|
|
'doctor' => DoctorCommand::class,
|
|
'catalog' => CatalogCommand::class,
|
|
'adopt' => AdoptCommand::class,
|
|
'commands' => CommandsCommand::class,
|
|
'self-update' => SelfUpdateCommand::class,
|
|
];
|
|
|
|
public function __construct(private ?Console $console = null)
|
|
{
|
|
$this->console ??= new Console();
|
|
}
|
|
|
|
/**
|
|
* @param list<string> $argv
|
|
*/
|
|
public function run(array $argv): int
|
|
{
|
|
$args = array_slice($argv, 1);
|
|
$command = $args[0] ?? null;
|
|
|
|
if ($command === null || in_array($command, ['help', '--help', '-h'], true)) {
|
|
$this->printHelp();
|
|
return 0;
|
|
}
|
|
|
|
if (in_array($command, ['--version', '-V'], true)) {
|
|
$this->console->out('duckbrain-toolbox ' . Config::VERSION);
|
|
return 0;
|
|
}
|
|
|
|
if (!isset(self::COMMANDS[$command])) {
|
|
$this->console->error("Comando desconocido: {$command}");
|
|
$this->printHelp();
|
|
return 1;
|
|
}
|
|
|
|
$class = self::COMMANDS[$command];
|
|
if (!class_exists($class)) {
|
|
$this->console->error("El comando '{$command}' no está disponible en esta instalación.");
|
|
return 1;
|
|
}
|
|
|
|
return (new $class($this->console))->run(array_slice($args, 1));
|
|
}
|
|
|
|
public function printHelp(): void
|
|
{
|
|
$this->console->out('duckbrain-toolbox ' . Config::VERSION);
|
|
$this->console->out('');
|
|
$this->console->out('Uso: duckbrain <comando> [opciones]');
|
|
$this->console->out('');
|
|
$this->console->out('Comandos:');
|
|
|
|
foreach (self::COMMANDS as $name => $class) {
|
|
if (!class_exists($class)) {
|
|
continue;
|
|
}
|
|
$description = method_exists($class, 'describe') ? $class::describe() : '';
|
|
$this->console->out(sprintf(' %-9s %s', $name, $description));
|
|
}
|
|
}
|
|
}
|