Skip to content

Commit 13674e5

Browse files
committed
Merge branch 'develop'
2 parents d0e8bac + 9a8a0cf commit 13674e5

File tree

1 file changed

+43
-15
lines changed

1 file changed

+43
-15
lines changed

src/Shell_Command.php

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
<?php
22

3+
use EE\Utils;
4+
use EE\Model\Site;
5+
use function EE\Site\Utils\auto_site_name;
6+
37
/**
4-
* Executes wp-cli command on a site.
8+
* Brings up a shell to run wp-cli, composer etc.
59
*
610
* ## EXAMPLES
711
*
8-
* # Create simple WordPress site
9-
* $ ee wp test.local plugin list
12+
* # Open shell of example.com
13+
* $ ee shell example.com
1014
*
1115
* @package ee-cli
1216
*/
13-
14-
use EE\Utils;
15-
1617
class Shell_Command extends EE_Command {
1718

1819
/**
@@ -24,28 +25,38 @@ class Shell_Command extends EE_Command {
2425
* : Name of website to run shell on.
2526
*/
2627
public function __invoke( $args ) {
28+
2729
EE\Utils\delem_log( 'ee shell start' );
28-
$args = EE\Utils\set_site_arg( $args, 'shell' );
30+
$args = auto_site_name( $args, 'shell', '' );
2931
$site_name = EE\Utils\remove_trailing_slash( $args[0] );
30-
if ( EE::db()::site_in_db( $site_name ) ) {
31-
$db_select = EE::db()::select( ['site_path'], ['sitename' => $site_name]);
32-
$site_root = $db_select[0]['site_path'];
33-
} else {
34-
EE::error( "Site $site_name does not exist." );
32+
33+
$site = Site::find( $site_name );
34+
35+
if ( ! $site || ! $site->site_enabled ) {
36+
EE::error( "Site $site_name does not exist or is not enabled." );
3537
}
36-
chdir($site_root);
38+
39+
chdir( $site->site_fs_path );
40+
$this->check_shell_available( 'php', $site );
3741
$this->run( "docker-compose exec --user='www-data' php bash" );
3842
EE\Utils\delem_log( 'ee shell end' );
3943
}
4044

45+
/**
46+
* Run the command to open shell.
47+
*
48+
* @param string $cmd Command to be executed to open shell.
49+
* @param null|array $descriptors File descriptors for proc.
50+
*/
4151
private function run( $cmd, $descriptors = null ) {
52+
4253
EE\Utils\check_proc_available( 'ee_shell' );
4354
if ( ! $descriptors ) {
4455
$descriptors = array( STDIN, STDOUT, STDERR );
4556
}
46-
57+
4758
$final_cmd = EE\Utils\force_env_on_nix_systems( $cmd );
48-
$proc = EE\Utils\proc_open_compat( $final_cmd, $descriptors, $pipes );
59+
$proc = EE\Utils\proc_open_compat( $final_cmd, $descriptors, $pipes );
4960
if ( ! $proc ) {
5061
exit( 1 );
5162
}
@@ -55,4 +66,21 @@ private function run( $cmd, $descriptors = null ) {
5566
}
5667
}
5768

69+
/**
70+
* Function to check if container supporting shell is present in docker-compose.yml or not.
71+
*
72+
* @param string $shell_container Container to be checked.
73+
* @param Object $site Contains relevant site info.
74+
*/
75+
private function check_shell_available( $shell_container, $site ) {
76+
77+
$launch = EE::launch( 'docker-compose config --services' );
78+
$services = explode( PHP_EOL, trim( $launch->stdout ) );
79+
if ( in_array( $shell_container, $services, true ) ) {
80+
return;
81+
}
82+
EE::debug( 'Site type: ' . $site->site_type );
83+
EE::debug( 'Site command: ' . $site->app_sub_type );
84+
EE::error( sprintf( '%s site does not have support to launch %s shell.', $site->site_url, $shell_container ) );
85+
}
5886
}

0 commit comments

Comments
 (0)