Skip to content

Commit

Permalink
Merge pull request #4 from elliottmangham/feature/db-connect
Browse files Browse the repository at this point in the history
Database Connect
  • Loading branch information
garethmidwood authored Aug 7, 2017
2 parents 77ad2af + ad7f2ec commit 9a30972
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
7 changes: 7 additions & 0 deletions local-build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

# credit to: https://moquet.net/blog/distributing-php-cli/

box build

mv cdev.phar /usr/local/bin/cdev-local
58 changes: 58 additions & 0 deletions src/Creode/Cdev/Command/Env/DbConnectEnvCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
namespace Creode\Cdev\Command\Env;

use Creode\Cdev\Command\Env\EnvCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class DbConnectEnvCommand extends EnvCommand
{
protected function configure()
{
$this->setName('env:db:connect');
$this->setDescription('Connects to database container');

$this->addOption(
'path',
'p',
InputOption::VALUE_REQUIRED,
'Path to run commands on. Defaults to the directory the command is run from',
getcwd()
);

$this->addOption(
'database',
'd',
InputOption::VALUE_REQUIRED,
'The database to connect to',
'website'
);

$this->addOption(
'user',
'u',
InputOption::VALUE_REQUIRED,
'The user to connect as',
'webuser'
);

$this->addOption(
'password',
'w',
InputOption::VALUE_REQUIRED,
'The user password',
'webpassword'
);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->_environment->input($input);

$output->writeln(
$this->_environment->dbConnect()
);
}
}
14 changes: 14 additions & 0 deletions src/Creode/Environment/Docker/Docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,20 @@ public function ssh()
$this->_compose->ssh($path, $user);
}

public function dbConnect()
{
$this->logTitle('Connecting to database...');

$path = $this->_input->getOption('path');
$database = $this->_input->getOption('database');
$user = $this->_input->getOption('user');
$password = $this->_input->getOption('password');

$this->logMessage("Connecting to $database as $user");

$this->_compose->dbConnect($path, $database, $user, $password);
}

public function cacheClear()
{
$commands = $this->_framework->clearCache();
Expand Down
30 changes: 30 additions & 0 deletions src/Creode/Environment/Docker/System/Compose/Compose.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,36 @@ public function ssh($path, $user)
);
}

/**
* Connects to database
* @param string $path
* @param string $database Database to connect to
* @param string $user User to connect as
* @param string $password Password for user
*/
public function dbConnect($path, $database, $user, $password)
{
$this->requiresConfig();

$this->run(
self::COMMAND,
[
'-f',
Config::CONFIG_DIR . self::FILE,
'-p',
$this->_networkName,
'exec',
'mysql',
'mysql',
'-u',
$user,
'-p'.$password,
$database
],
$path
);
}

/**
* Runs a command on the running environment
* @param string $path
Expand Down
2 changes: 2 additions & 0 deletions src/Creode/Environment/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ abstract public function cleanup();

abstract public function ssh();

abstract public function dbConnect();

abstract public function runCommand(array $command = array(), $elevatePermissions = false);

abstract public function cacheClear();
Expand Down
7 changes: 7 additions & 0 deletions src/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
<call method="add">
<argument type="service" id="cdev.env_stop_command" />
</call>
<call method="add">
<argument type="service" id="cdev.env_db_connect_command" />
</call>
</service>


Expand Down Expand Up @@ -119,6 +122,10 @@
<argument type="service" id="cdev.environment.type" />
</service>

<service id="cdev.env_db_connect_command" class="Creode\Cdev\Command\Env\DbConnectEnvCommand">
<argument type="service" id="cdev.environment.type" />
</service>

<service id="cdev.env_start_command" class="Creode\Cdev\Command\Env\StartEnvCommand">
<argument type="service" id="cdev.environment.type" />
</service>
Expand Down

0 comments on commit 9a30972

Please sign in to comment.