From d761f5957e72bbb504fc0fe436a5a95150aa89c5 Mon Sep 17 00:00:00 2001 From: Baptiste Fotia Date: Tue, 31 Oct 2023 11:18:29 +0100 Subject: [PATCH] feature(php): Add the format option I added the format option to specify an output to json. By default it's just a "success" text message, now. Signed-off-by: Baptiste Fotia --- lib/Commands/Create.php | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/Commands/Create.php b/lib/Commands/Create.php index 9d5e9582b..1f386ca84 100644 --- a/lib/Commands/Create.php +++ b/lib/Commands/Create.php @@ -39,6 +39,7 @@ class Create extends Command { public const OUTPUT_FORMAT_PLAIN = 'plain'; public const OUTPUT_FORMAT_JSON_PRETTY = 'json_pretty'; + public const OPTION_FORMAT_AVAILABLE = [ 'json' ]; public function __construct(private SpaceManager $spaceManager, private AdminGroup $adminGroup, @@ -61,6 +62,13 @@ protected function configure(): void { ->setName('workspace:create') ->setDescription('This command allows you to create a workspace') ->addArgument('name', InputArgument::REQUIRED, 'The name of your workspace.') + ->addOption( + 'format', + 'F', + InputOption::VALUE_REQUIRED, + 'Output json', + 'json' + ) ->addOption( 'user-workspace-manager', 'uwm', @@ -73,6 +81,7 @@ protected function configure(): void { protected function execute(InputInterface $input, OutputInterface $output): int { + $outputMessage = 'success'; $spacename = $input->getArgument('name'); if ($input->hasParameterOption('--user-workspace-manager')) { @@ -82,6 +91,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int } } + if ($this->checkValueFormatOptionIsValid($input)) { + throw new \Exception( + sprintf( + "The value is not valid.\nPlease, define an option valid : %s", + implode(', ', self::OPTION_FORMAT_AVAILABLE) + ) + ); + } + $workspace = $this->spaceManager->create($spacename); if ($input->hasParameterOption('--user-workspace-manager')) { @@ -92,7 +110,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int ); } - $output->writeln($this->formatOutput($input, $workspace)); + if ($input->hasParameterOption('--format')) { + $value = $input->getOption('format'); + if (in_array($value, self::OPTION_FORMAT_AVAILABLE)) { + $outputMessage = $this->formatOutput($input, $workspace); + } + } + + $output->writeln($outputMessage); return 0; } @@ -105,6 +130,17 @@ private function addUserToAdminGroupManager(string $username, array $workspace): return true; } + private function checkValueFormatOptionIsValid(InputInterface $input): bool { + if ($input->hasParameterOption('--format')) { + $value = $input->getOption('format'); + if ($value !== 'json') { + return true; + } + } + + return false; + } + private function formatOutput(InputInterface $input, array $items): string { switch ($input->getOption('output')) { case self::OUTPUT_FORMAT_JSON_PRETTY: