From 0d799fca6cff6cf7c621f3c8fe4349c18078771c Mon Sep 17 00:00:00 2001 From: Travis Tomka Date: Sun, 25 Feb 2018 18:09:06 -0700 Subject: [PATCH] Add docker-compose exec command. --- README.md | 1 + src/DockerServicesTrait.php | 2 +- src/Task/Base.php | 5 +- src/Task/Execute.php | 153 ++++++++++++++++++++++++++++++++++++ src/Task/loadTasks.php | 9 +++ 5 files changed, 167 insertions(+), 3 deletions(-) create mode 100644 src/Task/Execute.php diff --git a/README.md b/README.md index 7756038..37df419 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ composer require droath/robo-docker-compose:~0.0.1 The following commands have been implemented: - docker-compose up +- docker-compose exec - docker-compose down - docker-compose start - docker-compose restart diff --git a/src/DockerServicesTrait.php b/src/DockerServicesTrait.php index 62cae37..3b9bcf7 100755 --- a/src/DockerServicesTrait.php +++ b/src/DockerServicesTrait.php @@ -45,7 +45,7 @@ trait DockerServicesTrait /** * {@inheritdoc} */ - protected function getCommand() + public function getCommand() { // Append the services to the end of the command. return parent::getCommand() . ' ' . implode(' ', $this->services); diff --git a/src/Task/Base.php b/src/Task/Base.php index b48a644..7e46b70 100644 --- a/src/Task/Base.php +++ b/src/Task/Base.php @@ -4,13 +4,14 @@ namespace Droath\RoboDockerCompose\Task; use Droath\RoboDockerCompose\ExecutableArguments; use Robo\Common\ExecOneCommand; +use Robo\Contract\CommandInterface; use Robo\Exception\TaskException; use Robo\Task\BaseTask; /** * Docker compose base class. */ -abstract class Base extends BaseTask +abstract class Base extends BaseTask implements CommandInterface { use ExecOneCommand; use ExecutableArguments; @@ -128,7 +129,7 @@ abstract class Base extends BaseTask * * @return string */ - protected function getCommand() + public function getCommand() { return "{$this->executable} {$this->executableArgs} {$this->action} {$this->arguments}"; } diff --git a/src/Task/Execute.php b/src/Task/Execute.php new file mode 100644 index 0000000..b1b496b --- /dev/null +++ b/src/Task/Execute.php @@ -0,0 +1,153 @@ +container = $container; + + return $this; + } + + /** + * Set execute command. + * + * @param string|CommandInterface $command + * The command to execute. + * + * @return $this + */ + public function exec($command) + { + if ($command instanceof CommandInterface) { + $command = $command->getCommand(); + } + $this->command = $command; + + return $this; + } + + /** + * Run command in the background. + * + * @return $this + */ + public function detachedMode() + { + $this->arg('-d'); + + return $this; + } + + /** + * Give extended privileges to the process. + * + * @return $this + */ + public function privileged() + { + $this->option('privileged'); + + return $this; + } + + /** + * Run the command as this user. + * + * @param $user + * The user on which to run the command under. + * + * @return $this + */ + public function user($user) + { + $this->option('user', $user); + + return $this; + } + + /** + * Disable pseudo-tty allocation. + * + * @return $this + */ + public function disablePseudoTty() + { + $this->arg('-T'); + + return $this; + } + + /** + * Index of the container. + * + * @param $index + * The container index. + * + * @return $this + */ + public function index($index) + { + $this->option('index', $index); + + return $this; + } + + /** + * Set environment variables + * + * @param $key + * The environment key. + * @param $value + * The environment value. + * + * @return $this + */ + public function envVariable($key, $value) + { + $this->option('env', "{$key}={$value}"); + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getCommand() + { + return parent::getCommand() . "{$this->container} {$this->command}"; + } +} diff --git a/src/Task/loadTasks.php b/src/Task/loadTasks.php index 516a340..8162800 100755 --- a/src/Task/loadTasks.php +++ b/src/Task/loadTasks.php @@ -46,4 +46,13 @@ trait loadTasks { return $this->task(Restart::class, $pathToDockerCompose); } + + /** + * Docker compose execute task. + */ + protected function taskDockerComposeExecute($pathToDockerCompose = null) + { + return $this->task(Execute::class, $pathToDockerCompose); + } + }