Pairity/bin/pairity
Funky Waddle 68f3c05868
Some checks are pending
CI / test (8.2) (push) Waiting to run
CI / test (8.3) (push) Waiting to run
Cache and some other things
2026-01-06 10:56:40 -06:00

89 lines
2.5 KiB
PHP

#!/usr/bin/env php
<?php
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
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;
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:
pairity migrate [--path=DIR] [--config=FILE] [--pretend]
pairity rollback [--steps=N] [--config=FILE] [--pretend]
pairity status [--path=DIR] [--config=FILE]
pairity reset [--config=FILE] [--pretend]
pairity make:migration Name [--path=DIR] [--template=FILE]
pairity mongo:index:ensure DB COLLECTION KEYS_JSON [--unique]
pairity mongo:index:drop DB COLLECTION NAME
pairity mongo:index:list DB COLLECTION
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;
echo $help . PHP_EOL;
}
$args = parseArgs($argv);
$cmd = $args['_cmd'] ?? 'help';
$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);
}
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);
exit(1);
}