3 Commits
0.0.1 ... 0.0.3

4 changed files with 159 additions and 22 deletions

View File

@@ -0,0 +1,41 @@
<?php
namespace Droath\RoboDockerCompose;
use Robo\Common\CommandArguments;
/**
* Define command executable arguments.
*/
trait ExecutableArguments
{
/**
* Executable arguments.
*
* @var string
*/
protected $executableArgs = '';
/**
* Add executable options.
*
* @param string $option
* The executable option name.
* @param string $value
* The executable option value.
*/
protected function execOption($option, $value = null)
{
if (isset($option)) {
if (strpos($option, '-') !== 0) {
$option = "--$option";
}
$this->executableArgs .= null == $option ? '' : ' ' . $option;
$this->executableArgs .= null == $value ? '' : ' ' . CommandArguments::escape($value);
}
return $this;
}
}

View File

@@ -2,6 +2,7 @@
namespace Droath\RoboDockerCompose\Task; namespace Droath\RoboDockerCompose\Task;
use Droath\RoboDockerCompose\ExecutableArguments;
use Robo\Common\ExecOneCommand; use Robo\Common\ExecOneCommand;
use Robo\Exception\TaskException; use Robo\Exception\TaskException;
use Robo\Task\BaseTask; use Robo\Task\BaseTask;
@@ -12,6 +13,7 @@ use Robo\Task\BaseTask;
abstract class Base extends BaseTask abstract class Base extends BaseTask
{ {
use ExecOneCommand; use ExecOneCommand;
use ExecutableArguments;
/** /**
* Executable. * Executable.
@@ -35,11 +37,9 @@ abstract class Base extends BaseTask
*/ */
public function __construct($pathToDockerCompose = null) public function __construct($pathToDockerCompose = null)
{ {
$this->executable = $pathToDockerCompose; $this->executable = isset($pathToDockerCompose)
? $pathToDockerCompose
if (!$this->executable) { : $this->findExecutablePhar('docker-compose');
$this->executable = $this->findExecutablePhar('docker-compose');
}
if (!$this->executable) { if (!$this->executable) {
throw new TaskException( throw new TaskException(
@@ -49,6 +49,80 @@ abstract class Base extends BaseTask
} }
} }
/**
* Specify an alternate compose file.
*
* @param string $filename
* An alternative docker composer file
*/
public function file($filename)
{
if (!file_exists($filename)) {
throw new \InvalidArgumentException(
sprintf("File %s wasn't found on the filesystem", $filename)
);
}
$this->execOption('file', $filename);
return $this;
}
/**
* Specify multiple composer files.
*
* @param array $files
* An array of alternative composer files.
*/
public function files(array $files)
{
foreach ($files as $filename) {
$this->file($filename);
}
return $this;
}
/**
* Specify an alternate project name.
*
* @param string
* An alternative docker compose project name.
*/
public function projectName($name)
{
$this->execOption('project-name', $name);
return $this;
}
/**
* Daemon socket to connect to.
*
* @param string $hostname
* The name of the host to connect to.
*/
public function host($hostname)
{
$this->execOption('host', $hostname);
return $this;
}
/**
* {@inheritdoc}
*/
public function run()
{
$command = $this->getCommand();
$this->printTaskInfo(
'Running Docker-Compose: {command}',
['command' => $command]
);
return $this->executeCommand($command);
}
/** /**
* Get docker-compose command. * Get docker-compose command.
* *
@@ -56,6 +130,6 @@ abstract class Base extends BaseTask
*/ */
protected function getCommand() protected function getCommand()
{ {
return "{$this->executable} {$this->action} {$this->arguments}"; return "{$this->executable} {$this->executableArgs} {$this->action} {$this->arguments}";
} }
} }

View File

@@ -60,15 +60,4 @@ class Down extends Base
return $this; return $this;
} }
/**
* {@inheritdoc}
*/
public function run()
{
$command = $this->getCommand();
$this->printTaskInfo('Docker Down: {command}', ['command' => $command]);
return $this->executeCommand($command);
}
} }

View File

@@ -14,6 +14,13 @@ class Up extends Base
*/ */
protected $action = 'up'; protected $action = 'up';
/**
* Docker compose services.
*
* @var array
*/
protected $services = [];
/** /**
* Command detached mode. * Command detached mode.
* *
@@ -21,6 +28,34 @@ class Up extends Base
*/ */
protected $detachedMode = false; protected $detachedMode = false;
/**
* Add docker composer service.
*
* @param string $service
* The docker services.
*/
public function setService($service)
{
$this->services[] = $service;
return $this;
}
/**
* Add docker composer services.
*
* @param array $services
* An array of services.
*/
public function setServices(array $services)
{
foreach ($services as $service) {
$this->setService($service);
}
return $this;
}
/** /**
* Run containers in the background. * Run containers in the background.
*/ */
@@ -115,12 +150,10 @@ class Up extends Base
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function run() protected function getCommand()
{ {
$command = $this->getCommand(); // Append the services to the end of the command.
$this->printTaskInfo('Docker Up: {command}', ['command' => $command]); return parent::getCommand() . ' ' . implode(' ', $this->services);
return $this->executeCommand($command);
} }
/** /**