Pairity/bin/pairity

89 lines
2.5 KiB
Plaintext
Raw Normal View History

2025-12-10 13:01:07 +00:00
#!/usr/bin/env php
<?php
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
2026-01-06 16:56:40 +00:00
use Pairity\Console\MigrateCommand;
use Pairity\Console\RollbackCommand;
use Pairity\Console\StatusCommand;
use Pairity\Console\ResetCommand;
use Pairity\Console\MakeMigrationCommand;
use Pairity\Console\MongoIndexEnsureCommand;
use Pairity\Console\MongoIndexDropCommand;
use Pairity\Console\MongoIndexListCommand;
2025-12-10 13:01:07 +00:00
function parseArgs(array $argv): array {
$args = ['_cmd' => $argv[1] ?? 'help'];
for ($i = 2; $i < count($argv); $i++) {
$a = $argv[$i];
if (str_starts_with($a, '--')) {
$eq = strpos($a, '=');
if ($eq !== false) {
$key = substr($a, 2, $eq - 2);
$val = substr($a, $eq + 1);
$args[$key] = $val;
} else {
$key = substr($a, 2);
$args[$key] = true;
}
} else {
$args[] = $a;
}
}
return $args;
}
function cmd_help(): void
{
$help = <<<TXT
Pairity CLI
Usage:
2026-01-06 16:56:40 +00:00
pairity migrate [--path=DIR] [--config=FILE] [--pretend]
pairity rollback [--steps=N] [--config=FILE] [--pretend]
2025-12-10 13:01:07 +00:00
pairity status [--path=DIR] [--config=FILE]
2026-01-06 16:56:40 +00:00
pairity reset [--config=FILE] [--pretend]
pairity make:migration Name [--path=DIR] [--template=FILE]
2025-12-10 14:02:24 +00:00
pairity mongo:index:ensure DB COLLECTION KEYS_JSON [--unique]
pairity mongo:index:drop DB COLLECTION NAME
pairity mongo:index:list DB COLLECTION
2025-12-10 13:01:07 +00:00
Environment:
DB_DRIVER, DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, DB_PASSWORD, DB_PATH (for sqlite)
If --config is provided, it must be a PHP file returning the ConnectionManager config array.
TXT;
2026-01-06 16:56:40 +00:00
echo $help . PHP_EOL;
2025-12-10 13:01:07 +00:00
}
$args = parseArgs($argv);
$cmd = $args['_cmd'] ?? 'help';
2026-01-06 16:56:40 +00:00
$commands = [
'migrate' => MigrateCommand::class,
'rollback' => RollbackCommand::class,
'status' => StatusCommand::class,
'reset' => ResetCommand::class,
'make:migration' => MakeMigrationCommand::class,
'mongo:index:ensure' => MongoIndexEnsureCommand::class,
'mongo:index:drop' => MongoIndexDropCommand::class,
'mongo:index:list' => MongoIndexListCommand::class,
];
if ($cmd === 'help' || !isset($commands[$cmd])) {
cmd_help();
exit($cmd === 'help' ? 0 : 1);
}
2025-12-10 14:02:24 +00:00
2026-01-06 16:56:40 +00:00
try {
$class = $commands[$cmd];
/** @var \Pairity\Console\CommandInterface $instance */
$instance = new $class();
$instance->execute($args);
} catch (\Throwable $e) {
fwrite(STDERR, 'Error: ' . $e->getMessage() . PHP_EOL);
2025-12-10 13:01:07 +00:00
exit(1);
}