Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e3ace8a3ea | ||
|
|
90b7786dc9 | ||
|
|
b0c126c271 |
41
src/ExecutableArguments.php
Normal file
41
src/ExecutableArguments.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Droath\RoboDockerCompose\Task;
|
||||
|
||||
use Droath\RoboDockerCompose\ExecutableArguments;
|
||||
use Robo\Common\ExecOneCommand;
|
||||
use Robo\Exception\TaskException;
|
||||
use Robo\Task\BaseTask;
|
||||
@@ -12,6 +13,7 @@ use Robo\Task\BaseTask;
|
||||
abstract class Base extends BaseTask
|
||||
{
|
||||
use ExecOneCommand;
|
||||
use ExecutableArguments;
|
||||
|
||||
/**
|
||||
* Executable.
|
||||
@@ -35,11 +37,9 @@ abstract class Base extends BaseTask
|
||||
*/
|
||||
public function __construct($pathToDockerCompose = null)
|
||||
{
|
||||
$this->executable = $pathToDockerCompose;
|
||||
|
||||
if (!$this->executable) {
|
||||
$this->executable = $this->findExecutablePhar('docker-compose');
|
||||
}
|
||||
$this->executable = isset($pathToDockerCompose)
|
||||
? $pathToDockerCompose
|
||||
: $this->findExecutablePhar('docker-compose');
|
||||
|
||||
if (!$this->executable) {
|
||||
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.
|
||||
*
|
||||
@@ -56,6 +130,6 @@ abstract class Base extends BaseTask
|
||||
*/
|
||||
protected function getCommand()
|
||||
{
|
||||
return "{$this->executable} {$this->action} {$this->arguments}";
|
||||
return "{$this->executable} {$this->executableArgs} {$this->action} {$this->arguments}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,15 +60,4 @@ class Down extends Base
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$command = $this->getCommand();
|
||||
$this->printTaskInfo('Docker Down: {command}', ['command' => $command]);
|
||||
|
||||
return $this->executeCommand($command);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,13 @@ class Up extends Base
|
||||
*/
|
||||
protected $action = 'up';
|
||||
|
||||
/**
|
||||
* Docker compose services.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $services = [];
|
||||
|
||||
/**
|
||||
* Command detached mode.
|
||||
*
|
||||
@@ -21,6 +28,34 @@ class Up extends Base
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
@@ -115,12 +150,10 @@ class Up extends Base
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function run()
|
||||
protected function getCommand()
|
||||
{
|
||||
$command = $this->getCommand();
|
||||
$this->printTaskInfo('Docker Up: {command}', ['command' => $command]);
|
||||
|
||||
return $this->executeCommand($command);
|
||||
// Append the services to the end of the command.
|
||||
return parent::getCommand() . ' ' . implode(' ', $this->services);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user