diff --git a/.distignore b/.distignore new file mode 100644 index 0000000..acbaa32 --- /dev/null +++ b/.distignore @@ -0,0 +1,16 @@ +.DS_Store +.git +.gitignore +.gitlab-ci.yml +.editorconfig +.travis.yml +behat.yml +circle.yml +bin/ +features/ +utils/ +*.zip +*.tar.gz +*.swp +*.txt +*.log diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..e14d024 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,25 @@ +# This file is for unifying the coding style for different editors and IDEs +# editorconfig.org + +# WordPress Coding Standards +# https://make.wordpress.org/core/handbook/coding-standards/ + +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +indent_style = tab + +[{.jshintrc,*.json,*.yml,*.feature}] +indent_style = space +indent_size = 2 + +[{*.txt}] +end_of_line = crlf + +[composer.json] +indent_style = space +indent_size = 4 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6b6c3ae --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +.DS_Store +node_modules/ +vendor/ +*.zip +*.tar.gz +*.swp +*.txt +*.log +composer.lock +.idea diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..4e8325c --- /dev/null +++ b/composer.json @@ -0,0 +1,25 @@ +{ + "name": "easyengine/mailhog-command", + "description": "Commanad to enable disable mailhog for sites.", + "type": "ee-cli-package", + "homepage": "https://github.com/easyengine/mailhog-command", + "license": "MIT", + "authors": [], + "minimum-stability": "dev", + "prefer-stable": true, + "autoload": { + "psr-4": { + "": "src/" + }, + "files": [ "mailhog-command.php" ] + }, + "extra": { + "branch-alias": { + "dev-master": "1.x-dev" + }, + "bundled": true, + "commands": [ + "mailhog" + ] + } +} diff --git a/ee.yml b/ee.yml new file mode 100644 index 0000000..41ca6e1 --- /dev/null +++ b/ee.yml @@ -0,0 +1,2 @@ +require: + - mailhog-command.php \ No newline at end of file diff --git a/mailhog-command.php b/mailhog-command.php new file mode 100644 index 0000000..d89d466 --- /dev/null +++ b/mailhog-command.php @@ -0,0 +1,12 @@ + + + WordPress Coding Standards for EE + + + + + + + + + + + . + */ci/* + */features/* + */packages/* + */tests/* + */utils/* + */vendor/* + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Mailhog_Command.php b/src/Mailhog_Command.php new file mode 100644 index 0000000..499853b --- /dev/null +++ b/src/Mailhog_Command.php @@ -0,0 +1,130 @@ +] + * : Name of website to enable mailhog on. + */ + public function up( $args, $assoc_args ) { + + EE\Utils\delem_log( 'mailhog' . __FUNCTION__ . ' start' ); + $args = EE\SiteUtils\auto_site_name( $args, 'mailhog', __FUNCTION__ ); + $this->site_data = Site::find( EE\Utils\remove_trailing_slash( $args[0] ) ); + if ( ! $this->site_data || ! $this->site_data->site_enabled ) { + EE::error( sprintf( 'Site %s does not exist / is not enabled.', $args[0] ) ); + } + + if ( $this->mailhog_enabled() ) { + EE::error( 'Mailhog is already up.' ); + } + EE::docker()::docker_compose_up( $this->site_data->site_fs_path, [ 'mailhog' ] ); + EE::exec( "docker-compose exec postfix postconf -e 'relayhost = mailhog:1025'" ); + EE::success( sprintf( 'Mailhog enabled for %s site', $this->site_data->site_url ) ); + } + + /** + * Disables mailhog on given site. + * + * ## OPTIONS + * + * [] + * : Name of website to disable mailhog on. + */ + public function down( $args, $assoc_args ) { + + EE\Utils\delem_log( 'mailhog' . __FUNCTION__ . ' start' ); + $args = EE\SiteUtils\auto_site_name( $args, 'mailhog', __FUNCTION__ ); + $this->site_data = Site::find( EE\Utils\remove_trailing_slash( $args[0] ) ); + if ( ! $this->site_data || ! $this->site_data->site_enabled ) { + EE::error( sprintf( 'Site %s does not exist / is not enabled.', $args[0] ) ); + } + + if ( ! $this->mailhog_enabled() ) { + EE::error( 'Mailhog is already down.' ); + } + EE::exec( 'docker-compose stop mailhog' ); + EE::exec( 'docker-compose exec postfix postconf -e \'relayhost =\'' ); + EE::success( sprintf( 'Mailhog disabled for %s site', $this->site_data->site_url ) ); + } + + /** + * Outputs status of mailhog for a site. + * + * ## OPTIONS + * + * [] + * : Name of website to know mailhog status for. + */ + public function status( $args, $assoc_args ) { + + EE\Utils\delem_log( 'mailhog' . __FUNCTION__ . ' start' ); + $args = EE\SiteUtils\auto_site_name( $args, 'mailhog', __FUNCTION__ ); + $this->site_data = Site::find( EE\Utils\remove_trailing_slash( $args[0] ) ); + if ( ! $this->site_data || ! $this->site_data->site_enabled ) { + EE::error( sprintf( 'Site %s does not exist / is not enabled.', $args[0] ) ); + } + + if ( $this->mailhog_enabled() ) { + EE::log( sprintf( 'Mailhog is UP for %s site.', $this->site_data->site_url ) ); + } else { + EE::log( sprintf( 'Mailhog is DOWN for %s site.', $this->site_data->site_url ) ); + } + } + + /** + * Function to check if mailhog is present in docker-compose.yml or not. + */ + private function check_mailhog_available() { + + chdir( $this->site_data->site_fs_path ); + $launch = EE::launch( 'docker-compose config --services' ); + $services = explode( PHP_EOL, trim( $launch->stdout ) ); + if ( ! in_array( 'mailhog', $services, true ) ) { + EE::debug( 'Site type: ' . $this->site_data->site_type ); + EE::debug( 'Site command: ' . $this->site_data->app_sub_type ); + EE::error( sprintf( '%s site does not have support to enable/disable mailhog.' ) ); + } + } + + /** + * Function to check if mailhog is possible for the site-type and is enabled or not. + * + * @return bool Status of mailhog enabled or not. + */ + private function mailhog_enabled() { + + $this->check_mailhog_available(); + $launch = EE::launch( 'docker-compose ps -q mailhog' ); + $id = trim( $launch->stdout ); + if ( empty( $id ) ) { + return false; + } + + return 'running' === EE::docker()::container_status( $id ); + } + +}