diff --git a/src/Command/CommandBase.php b/src/Command/CommandBase.php index 72bc10fd33..1636850048 100644 --- a/src/Command/CommandBase.php +++ b/src/Command/CommandBase.php @@ -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; @@ -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) { @@ -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.'); } } diff --git a/src/Command/Project/ProjectCreateCommand.php b/src/Command/Project/ProjectCreateCommand.php index 6edb694105..5b51216dbb 100644 --- a/src/Command/Project/ProjectCreateCommand.php +++ b/src/Command/Project/ProjectCreateCommand.php @@ -167,7 +167,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $setRemote = $questionHelper->confirm(sprintf( 'Set the new project %s as the remote for this repository?', $options['title'] - ), false); + )); } $this->stdErr->writeln(''); } diff --git a/src/Console/BufferedOutputWithErrorOutput.php b/src/Console/BufferedOutputWithErrorOutput.php new file mode 100644 index 0000000000..d776d8d5de --- /dev/null +++ b/src/Console/BufferedOutputWithErrorOutput.php @@ -0,0 +1,31 @@ +setErrorOutput($errorOutput); + } + + public function getErrorOutput() + { + return $this->stdErr; + } + + public function setErrorOutput(OutputInterface $error) + { + $this->stdErr = $error; + } +}