Skip to content

Commit 99b3daa

Browse files
committed
Add docker_compose_exec helper function
Signed-off-by: Riddhesh Sanghvi <[email protected]>
1 parent bec0411 commit 99b3daa

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

php/class-ee-docker.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public static function disconnect_site_network_from( $site_name, $from_container
183183
* @return bool success.
184184
*/
185185
public static function docker_compose_up( $dir, $services = [] ) {
186-
$fs = new Filesystem();
186+
187187
$chdir_return_code = chdir( $dir );
188188
if ( $chdir_return_code ) {
189189
if ( empty( $services ) ) {
@@ -198,6 +198,48 @@ public static function docker_compose_up( $dir, $services = [] ) {
198198
return false;
199199
}
200200

201+
/**
202+
* Function to exec and run commands into the containers.
203+
*
204+
* @param string $command Command to exec.
205+
* @param string $service Service to exec command into.
206+
* @param string $shell Shell in which exec command will be executed.
207+
* @param string $user User to execute command into.
208+
* @param String $dir Path to docker-compose.yml.
209+
* @param bool $shell_wrapper If shell wrapper should be enabled or not.
210+
* @param bool $exit_on_error To exit or not on error.
211+
*
212+
* @return bool success.
213+
*/
214+
public static function docker_compose_exec( $command = '', $service = '', $shell = 'sh', $user = '', $dir = '', $shell_wrapper = false, $exit_on_error = false ) {
215+
216+
if ( ! empty( $dir ) ) {
217+
$chdir_return_code = chdir( $dir );
218+
} else {
219+
$chdir_return_code = true;
220+
}
221+
222+
$skip_tty = \EE::get_runner()->config['skip-tty'];
223+
$tty = empty( $skip_tty ) ? '' : '-T';
224+
225+
if ( $chdir_return_code ) {
226+
227+
$user_string = '';
228+
if ( $user ) {
229+
$user_string = empty( $user ) ? '' : "--user='$user'";
230+
}
231+
232+
if ( $shell_wrapper ) {
233+
return EE::exec( \EE_DOCKER::docker_compose_with_custom() . " exec $tty $user_string $service $shell -c \"$command\"", true, true, [], $exit_on_error );
234+
} else {
235+
return EE::exec( \EE_DOCKER::docker_compose_with_custom() . " exec $tty $user_string $service $command", true, true, [], $exit_on_error );
236+
}
237+
238+
}
239+
240+
return false;
241+
}
242+
201243
/**
202244
* Function to check if a network exists
203245
*

0 commit comments

Comments
 (0)