protected function execute(InputInterface $input, OutputInterface $output)
{
$commandLine = $input->getArgument('cmd');
$commandArgs = explode(' ', $commandLine);
$commandName = reset($commandArgs);
if (!$commandName) {
throw new \InvalidArgumentException('Invalid command: ' . $commandLine);
}
/** @var \Platformsh\Cli\Application $application */
$application = $this->getApplication();
$command = $application->find($commandName);
if (!$command instanceof MultiAwareInterface || !$command->canBeRunMultipleTimes()) {
$this->stdErr->writeln(sprintf('The command <error>%s</error> cannot be run via "%s multi".', $commandName, self::$config->get('application.executable')));
return 1;
} elseif (!$command->getDefinition()->hasOption('project')) {
$this->stdErr->writeln(sprintf('The command <error>%s</error> does not have a --project option.', $commandName));
return 1;
}
$projects = $this->getSelectedProjects($input);
if ($projects === false) {
return 1;
}
$success = true;
$continue = $input->getOption('continue');
$this->stdErr->writeln(sprintf("Running command '%s' on %d %s.", $commandLine, count($projects), count($projects) === 1 ? 'project' : 'projects'));
foreach ($projects as $project) {
$this->stdErr->writeln('');
$this->stdErr->writeln('<options=reverse>*</> Project: ' . $this->api()->getProjectLabel($project, false));
try {
$application->setCurrentCommand($command);
$commandInput = new StringInput($commandLine . ' --project ' . escapeshellarg($project->id));
if ($command instanceof MultiAwareInterface) {
$command->setRunningViaMulti(true);
}
$returnCode = $command->run($commandInput, $output);
$application->setCurrentCommand($this);
if ($returnCode !== 0) {
$success = false;
}
} catch (\Exception $e) {
if (!$continue) {
throw $e;
}
$this->getApplication()->renderException($e, $this->stdErr);
$success = false;
}
}
return $success ? 0 : 1;
}