Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow commands to override the default --instance option #38

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,6 @@ public function doRun(InputInterface $input, OutputInterface $output)
return parent::doRun($input, $output);
}

protected function getDefaultInputDefinition()
{
$definition = parent::getDefaultInputDefinition();

$definition->addOptions([
new InputOption('--instance', '-I', InputOption::VALUE_REQUIRED, 'Specify the concrete5 directory', '.'),
]);

return $definition;
}

public function getHelp()
{
$artWidth = 42;
Expand Down
4 changes: 2 additions & 2 deletions src/Command/Backup/BackupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public function __invoke(?string $filename, InputInterface $input)
*/
public static function register(Container $container, Application $console): void
{
$console
->command(
static::command(
$console,
'backup:backup [filename] [--skip-core] [--temp] [--dir=]',
self::class,
['backup']
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Backup/InspectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function __invoke(string $backupFile, InputInterface $input, ManifestFact

public static function register(Container $container, Application $console): void
{
$console->command('backup:inspect backupfile [-r|--manifest-only] [--ls=]', self::class)
self::command($console, 'backup:inspect backupfile [-r|--manifest-only] [--ls=]', self::class)
->descriptions('Inspects a Concrete installation backup', [
'backupfile' => 'The path to the backup file to inspect',
'--manifest-only' => 'Output the raw manifest json, combine with <info>jq</> command.',
Expand Down
4 changes: 2 additions & 2 deletions src/Command/Backup/RestoreCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public function __invoke(

public static function register(Container $container, Application $console): void
{
$console
->command(
self::command(
$console,
'backup:restore backupFile [-D|--dryrun]
[--skip-db] [--skip-core] [--skip-packages] [--skip-config] [--skip-files] [--skip-application]
[--skip-index] [--skip-database] [--reload-fpm-command=]',
Expand Down
64 changes: 64 additions & 0 deletions src/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Concrete\Console\Command;

use Concrete\Console\Application;
use Concrete\Console\Concrete\Connection\ApplicationEnabledConnectionInterface;
use Concrete\Console\Concrete\Connection\ConnectionAwareInterface;
use Concrete\Console\Concrete\Connection\ConnectionAwareTrait;
Expand All @@ -12,6 +13,7 @@
use Concrete\Console\Installation\InstallationAwareTrait;
use League\Container\ContainerAwareInterface;
use League\Container\ContainerAwareTrait;
use Silly\Command\Command as SillyCommand;

abstract class Command implements
ContainerAwareInterface,
Expand All @@ -27,6 +29,21 @@ abstract class Command implements
use ConnectionAwareTrait;
use InstallationAwareTrait;

/**
* Additional options to add separate from declared commands
*
* @var array<int, array{long: string, short: string, description: string, default: mixed, optional: boolean}>
*/
protected static $extendedOptions = [
[
'long' => 'instance',
'short' => 'I',
'description' => 'Specify the concrete5 directory',
'default' => '.',
'optional' => true,
]
];

/**
* @throws \Concrete\Console\Exception\Installation\VersionMismatch
*/
Expand All @@ -39,4 +56,51 @@ public function getApplication(): \Concrete\Core\Application\Application

return $connection->getApplication();
}

/**
* Static function for resolving the command instance with $extendedOptions included.
*
* @param Application $console
* @param string $expression
* @param string|callable $callable
* @param array $aliases
*
* @return SillyCommand
*/
public static function command(
Application $console,
string $expression,
$callable,
array $aliases = []
): SillyCommand {
$extension = [];
$descriptions = [];
$defaults = [];
foreach (static::$extendedOptions as $option) {
[
'long' => $long,
'short' => $short,
'description' => $description,
'default' => $default,
'optional' => $optional
] = $option;

// Resolve the extension string
$optionalAddition = $optional ? '=' : '';
$shortFlag = $short ? "-{$short}|" : '';
$longFlag = "--{$long}{$optionalAddition}";
$extension[] = "[{$shortFlag}{$longFlag}]";

// resolve defaults and descriptions
$defaults[$long] = $default;
$descriptions['--' . $long] = $description;
}

$extension = $extension ? ' ' . implode(' ', $extension) : '';

// Create a new command with our extension string added
return $console->command("{$expression}{$extension}", $callable, $aliases)
->descriptions('', $descriptions)
->defaults($defaults);
}
}
2 changes: 1 addition & 1 deletion src/Command/Database/DumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function __invoke(string $file, Input $input)

public static function register(Container $container, Application $console): void
{
$console->command('database:dump [file] [-z|--gz]', self::class)
self::command($console, 'database:dump [file] [-z|--gz]', self::class)
->descriptions('Dumps the Concrete database to a file', [
'file' => 'Filename for the dump file',
'--gz' => 'Flag to gzip',
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Site/InfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __invoke()
*/
public static function register(Container $container, Application $console): void
{
$console->command('site:info', self::class, ['info'])
self::command($console, 'site:info', self::class, ['info'])
->descriptions('Get info about the current Concrete installation');
}
}
2 changes: 1 addition & 1 deletion src/Command/Site/SyncCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function __invoke(string $from, InputInterface $input)

public static function register(Container $container, Application $console): void
{
$console->command('site:sync from [--config=]', self::class)
self::command($console, 'site:sync from [--config=]', self::class)
->descriptions(
'Sync a remote site into this site using backups.',
[
Expand Down