Skip to content

Commit 9a8a0cf

Browse files
Merge pull request #6 from mrrobot47/update-post-refactor
Add checks and update functional calls
2 parents 14497bd + 398aafc commit 9a8a0cf

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

src/Shell_Command.php

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +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-
use EE\Model\Site;
16-
1717
class Shell_Command extends EE_Command {
1818

1919
/**
@@ -25,22 +25,31 @@ class Shell_Command extends EE_Command {
2525
* : Name of website to run shell on.
2626
*/
2727
public function __invoke( $args ) {
28+
2829
EE\Utils\delem_log( 'ee shell start' );
29-
$args = EE\SiteUtils\auto_site_name( $args, 'shell', '' );
30+
$args = auto_site_name( $args, 'shell', '' );
3031
$site_name = EE\Utils\remove_trailing_slash( $args[0] );
3132

32-
$site = Site::find( $site_name, [ 'site_enabled', 'site_fs_path' ] );
33+
$site = Site::find( $site_name );
3334

3435
if ( ! $site || ! $site->site_enabled ) {
3536
EE::error( "Site $site_name does not exist or is not enabled." );
3637
}
3738

3839
chdir( $site->site_fs_path );
40+
$this->check_shell_available( 'php', $site );
3941
$this->run( "docker-compose exec --user='www-data' php bash" );
4042
EE\Utils\delem_log( 'ee shell end' );
4143
}
4244

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+
*/
4351
private function run( $cmd, $descriptors = null ) {
52+
4453
EE\Utils\check_proc_available( 'ee_shell' );
4554
if ( ! $descriptors ) {
4655
$descriptors = array( STDIN, STDOUT, STDERR );
@@ -57,4 +66,21 @@ private function run( $cmd, $descriptors = null ) {
5766
}
5867
}
5968

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+
}
6086
}

0 commit comments

Comments
 (0)