Skip to content

Commit

Permalink
feature(php): Add the format option
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
zak39 committed Oct 31, 2023
1 parent 343e7dd commit d761f59
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion lib/Commands/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
Expand All @@ -73,6 +81,7 @@ protected function configure(): void {

protected function execute(InputInterface $input, OutputInterface $output): int {

$outputMessage = '<info>success</info>';
$spacename = $input->getArgument('name');

if ($input->hasParameterOption('--user-workspace-manager')) {
Expand All @@ -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')) {
Expand All @@ -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;
}
Expand All @@ -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:
Expand Down

0 comments on commit d761f59

Please sign in to comment.