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

Offer to create a project for various commands if you have none #1313

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
24 changes: 23 additions & 1 deletion src/Command/CommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use GuzzleHttp\Exception\BadResponseException;
use Platformsh\Cli\Command\Self\SelfInstallCommand;
use Platformsh\Cli\Console\ArrayArgument;
use Platformsh\Cli\Console\BufferedOutputWithErrorOutput;
use Platformsh\Cli\Console\HiddenInputOption;
use Platformsh\Cli\Event\EnvironmentsChangedEvent;
use Platformsh\Cli\Exception\LoginRequiredException;
Expand Down Expand Up @@ -1058,6 +1059,27 @@ protected function selectProject($projectId = null, $host = null, $detectCurrent

return $this->selectProject($projectId);
}
if ($this->config()->isCommandEnabled('project:create')) {
$this->debug('No project specified: offering to create one...');
$questionText = 'You do not have any ' . $this->config()->get('service.name') . ' projects yet.'
. "\nDo you want to create one?";
/** @var \Platformsh\Cli\Service\QuestionHelper $questionHelper */
$questionHelper = $this->getService('question_helper');
if ($questionHelper->confirm($questionText)) {
$this->stdErr->writeln('');
$buffer = new BufferedOutputWithErrorOutput($this->stdErr);
if ($this->runOtherCommand('project:create', [], $buffer) !== 0) {
throw new ConsoleInvalidArgumentException('A project is required');
}
if (!$id = trim($buffer->fetch())) {
throw new \RuntimeException('Failed to receive project ID from create command');
}
if (!$project = $this->api()->getProject($id)) {
throw new \RuntimeException('Failed to fetch project: ' . $id);
}
$this->project = $project;
}
}
}
if (!$this->project) {
if ($detectCurrent) {
Expand All @@ -1066,7 +1088,7 @@ protected function selectProject($projectId = null, $host = null, $detectCurrent
. "\n\nSpecify it using --project, or go to a project directory."
);
} else {
throw new \RuntimeException('You must specify a project.');
throw new ConsoleInvalidArgumentException('You must specify a project.');
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Command/Project/ProjectCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$setRemote = $questionHelper->confirm(sprintf(
'Set the new project <info>%s</info> as the remote for this repository?',
$options['title']
), false);
));
}
$this->stdErr->writeln('');
}
Expand Down
31 changes: 31 additions & 0 deletions src/Console/BufferedOutputWithErrorOutput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Platformsh\Cli\Console;

use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* A Symfony Console output class implementing a buffer for stdout and another output class for stderr.
*/
class BufferedOutputWithErrorOutput extends BufferedOutput implements ConsoleOutputInterface
{
private $stdErr;

public function __construct(OutputInterface $errorOutput)
{
parent::__construct();
$this->setErrorOutput($errorOutput);
}

public function getErrorOutput()
{
return $this->stdErr;
}

public function setErrorOutput(OutputInterface $error)
{
$this->stdErr = $error;
}
}