Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcdawkins committed Sep 23, 2015
2 parents a7d9ee6 + 9c819c6 commit 138cb03
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 23 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",
"require": {
"doctrine/cache": "~1.4.2",
"platformsh/client": "0.1.22",
"platformsh/client": "0.1.25",
"symfony/console": ">= 2.5.2 < 2.7.0",
"symfony/yaml": "~2.5",
"symfony/finder": "~2.5",
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Command/Activity/ActivityListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
);
}

if ($output instanceof StreamOutput && ($input->getOption('pipe') || !$this->isTerminal($output))) {
if ($output instanceof StreamOutput && $input->getOption('pipe')) {
$stream = $output->getStream();
array_unshift($rows, $headers);
foreach ($rows as $row) {
Expand Down
47 changes: 43 additions & 4 deletions src/Command/Auth/LoginCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Platformsh\Cli\Command\Auth;

use GuzzleHttp\Exception\BadResponseException;
use Platformsh\Cli\Command\PlatformCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -34,6 +35,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

protected function configureAccount(InputInterface $input, OutputInterface $output)
{
/** @var \Platformsh\Cli\Helper\PlatformQuestionHelper $helper */
$helper = $this->getHelper('question');

$question = new Question('Your email address: ');
Expand Down Expand Up @@ -96,10 +98,47 @@ function ($answer) {

try {
$this->authenticateUser($email, $password);
} catch (\InvalidArgumentException $e) {
$output->writeln("\n<error>Login failed. Please check your credentials.</error>\n");
$output->writeln("Forgot your password? Visit: <comment>https://accounts.platform.sh/user/password</comment>\n");
$this->configureAccount($input, $output);
} catch (BadResponseException $e) {
// If a two-factor authentication challenge is received, then ask
// the user for their TOTP code, and then retry authenticateUser().
if ($e->getResponse()->getHeader('X-Drupal-TFA')) {
$question = new Question("Your application verification code: ");
$question->setValidator(function ($answer) use ($email, $password) {
if (trim($answer) == '') {
throw new \RuntimeException("The code cannot be empty.");
}
try {
$this->authenticateUser($email, $password, $answer);
}
catch (BadResponseException $e) {
// If there is a two-factor authentication error, show
// the error description that the server provides.
//
// A RuntimeException here causes the user to be asked
// again for their TOTP code.
if ($e->getResponse()->getHeader('X-Drupal-TFA')) {
$json = $e->getResponse()->json();
throw new \RuntimeException($json['error_description']);
}
else {
throw $e;
}
}

return $answer;
});
$question->setMaxAttempts(5);
$output->writeln("\nTwo-factor authentication is required.");
$helper->ask($input, $output, $question);
}
elseif ($e->getResponse()->getStatusCode() === 401) {
$output->writeln("\n<error>Login failed. Please check your credentials.</error>\n");
$output->writeln("Forgot your password? Visit: <comment>https://accounts.platform.sh/user/password</comment>\n");
$this->configureAccount($input, $output);
}
else {
throw $e;
}
}
}

Expand Down
11 changes: 9 additions & 2 deletions src/Command/Environment/EnvironmentBranchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$this->envArgName = 'parent';
$this->validateInput($input, true);
$selectedProject = $this->getSelectedProject();

$branchName = $input->getArgument('name');
if (empty($branchName)) {
if ($input->isInteractive()) {
// List environments.
return $this->runOtherCommand(
'environments',
array('--project' => $this->getSelectedProject()->id)
array('--project' => $selectedProject->id)
);
}
$this->stdErr->writeln("<error>You must specify the name of the new branch.</error>");
Expand All @@ -79,7 +80,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 1;
}

if ($environment = $this->getEnvironment($machineName, $this->getSelectedProject())) {
if ($environment = $this->getEnvironment($machineName, $selectedProject)) {
$checkout = $this->getHelper('question')
->confirm(
"The environment <comment>$machineName</comment> already exists. Check out?",
Expand Down Expand Up @@ -129,6 +130,9 @@ protected function execute(InputInterface $input, OutputInterface $output)

$activity = $selectedEnvironment->branch($branchName, $machineName);

// Clear the environments cache, as branching has started.
$this->clearEnvironmentsCache($selectedProject);

if ($projectRoot) {
$gitHelper = new GitHelper(new ShellHelper($this->stdErr));
$gitHelper->setDefaultRepositoryDir($projectRoot . '/' . LocalProject::REPOSITORY_DIR);
Expand Down Expand Up @@ -169,6 +173,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
"The environment <info>$branchName</info> has been branched.",
'<error>Branching failed</error>'
);

// Clear the environments cache again.
$this->clearEnvironmentsCache($selectedProject);
}

$build = $input->getOption('build');
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Environment/EnvironmentSshCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$sshUrl = $this->getSelectedEnvironment()
->getSshUrl($input->getOption('app'));

if ($input->getOption('pipe') || !$this->isTerminal($output)) {
if ($input->getOption('pipe')) {
$output->write($sshUrl);

return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Local/LocalDrushAliasesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$projectConfig = LocalProject::getProjectConfig($projectRoot);
$current_group = isset($projectConfig['alias-group']) ? $projectConfig['alias-group'] : $projectConfig['id'];

if ($input->getOption('pipe') || !$this->isTerminal($output)) {
if ($input->getOption('pipe')) {
$output->writeln($current_group);

return 0;
Expand Down
1 change: 1 addition & 0 deletions src/Command/Local/LocalInitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 1;
}
$gitUrl = $project->getGitUrl();
$projectId = $project->id;
}

$inside = strpos(getcwd(), $realPath) === 0;
Expand Down
15 changes: 12 additions & 3 deletions src/Command/PlatformCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,13 @@ public function isLocal()
*
* @param string $email The user's email.
* @param string $password The user's password.
* @param string $totp The user's TFA one-time password.
*/
protected function authenticateUser($email, $password)
protected function authenticateUser($email, $password, $totp = null)
{
$this->getClient(false)
->getConnector()
->logIn($email, $password, true);
->logIn($email, $password, true, $totp);
}

/**
Expand Down Expand Up @@ -450,6 +451,14 @@ public function getProjects($refresh = false)
*/
protected function getProject($id, $host = null, $refresh = false)
{
// Allow the specified project to be a full URL.
if (strpos($id, '//') !== false) {
$url = $id;
$id = basename($url);
$host = parse_url($url, PHP_URL_HOST);
}

// Find the project in the user's main project list.
$projects = $this->getProjects($refresh);
if (isset($projects[$id])) {
return $projects[$id];
Expand Down Expand Up @@ -553,7 +562,7 @@ protected function getEnvironment($id, Project $project = null, $refresh = false
*
* @param Project $project
*/
protected function clearEnvironmentsCache(Project $project = null)
public function clearEnvironmentsCache(Project $project = null)
{
$project = $project ?: $this->getSelectedProject();
self::$cache->delete('environments:' . $project->id);
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Project/ProjectGetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$local = new LocalProject();
$hostname = parse_url($project->getUri(), PHP_URL_HOST) ?: null;
$local->createProjectFiles($projectRoot, $projectId, $hostname);
$local->createProjectFiles($projectRoot, $project->id, $hostname);

$environments = $this->getEnvironments($project, true);

Expand Down
2 changes: 1 addition & 1 deletion src/Command/Project/ProjectListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$projects = $this->getProjects($refresh);

if ($input->getOption('pipe') || !$this->isTerminal($output)) {
if ($input->getOption('pipe')) {
$output->writeln(array_keys($projects));

return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/Command/User/UserRoleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->stdErr->writeln("User <info>$email</info> updated");
}

if ($input->getOption('pipe') || !$this->isTerminal($output)) {
if ($input->getOption('pipe')) {
if ($level == 'project') {
$output->writeln($selectedUser->role);
} elseif ($level == 'environment') {
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Variable/VariableGetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

if ($input->getOption('pipe') || !$this->isTerminal($output)) {
if ($input->getOption('pipe')) {
foreach ($results as $variable) {
$output->writeln($variable['id'] . "\t" . $variable['value']);
}
Expand Down
11 changes: 11 additions & 0 deletions src/Console/EventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\ParseException;
use Platformsh\Cli\Command\PlatformCommand;
use Platformsh\Cli\Exception\ConnectionFailedException;
use Platformsh\Cli\Exception\LoginRequiredException;
use Platformsh\Cli\Exception\PermissionDeniedException;
use Platformsh\Client\Exception\EnvironmentStateException;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand Down Expand Up @@ -77,5 +79,14 @@ public function onException(ConsoleExceptionEvent $event)
$event->stopPropagation();
}
}

// When an environment is found to be in the wrong state, perhaps our
// cache is old - we should invalidate it.
if ($exception instanceof EnvironmentStateException) {
$command = $event->getCommand();
if ($command instanceof PlatformCommand) {
$command->clearEnvironmentsCache();
}
}
}
}

0 comments on commit 138cb03

Please sign in to comment.