refactor command as more easily

This commit is contained in:
crazywhalecc
2023-04-22 17:45:43 +08:00
parent 1540af266b
commit 4c0d35c723
10 changed files with 150 additions and 106 deletions

View File

@@ -8,20 +8,17 @@ use SPC\exception\FileSystemException;
use SPC\exception\ValidationException;
use SPC\store\FileSystem;
use SPC\util\ConfigValidator;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* 修改 config 后对其 kv 进行排序的操作
*/
#[AsCommand('sort-config', 'After config edited, sort it by alphabet')]
class SortConfigCommand extends BaseCommand
{
protected static $defaultName = 'sort-config';
public function configure()
{
$this->setDescription('After config edited, sort it by alphabet');
$this->addArgument('config-name', InputArgument::REQUIRED, 'Your config to be sorted, you can sort "lib", "source" and "ext".');
}
@@ -29,9 +26,9 @@ class SortConfigCommand extends BaseCommand
* @throws ValidationException
* @throws FileSystemException
*/
public function execute(InputInterface $input, OutputInterface $output): int
public function handle(): int
{
switch ($name = $input->getArgument('config-name')) {
switch ($name = $this->getArgument('config-name')) {
case 'lib':
$file = json_decode(FileSystem::readFile(ROOT_DIR . '/config/lib.json'), true);
ConfigValidator::validateLibs($file);
@@ -51,10 +48,10 @@ class SortConfigCommand extends BaseCommand
file_put_contents(ROOT_DIR . '/config/ext.json', json_encode($file, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
break;
default:
$output->writeln("<error>invalid config name: {$name}</error>");
$this->output->writeln("<error>invalid config name: {$name}</error>");
return 1;
}
$output->writeln('<info>sort success</info>');
$this->output->writeln('<info>sort success</info>');
return 0;
}
}