From bedb47d75b6baa708f92e1094cf40c80d0baf5fd Mon Sep 17 00:00:00 2001 From: Marco Date: Wed, 20 Jun 2018 00:05:11 +1000 Subject: [PATCH] [ADD] Add version v1.10.0 and plugin v1.2.0. (#22) * Feature/diff command (#20) * [ADD] Add diff command - update AwsUpload handle diff / df agrs - add DiffCommand * [ADD] Add editor config file * [ADD] Add RsyncCommands - add interface to scope the const to define the available commands you can perform on rsync * [MOD] Restructure Rsync to handle different commands - update RsyncTest to test getExclude, upload and diff commands - update DiffCommand to use RsyncCommands - update UploadCommand to use RsyncCommands * [MOD] Update help message, add diff * [MOD] Update CHANGELOG for diff command * [MOD] Update phpdocs Rsync * [MOD] Fix spacing * [FIX] Fix no editor no sed (#21) * [FIX] Fix case no $EDITOR variable present - update SettingFile to get a default editor - update tests for Envs and SettingFiles * [FIX] Replace sed to activate aws-upload plugin - change algorithm to activate plugin - create test cases * [REF] Extract getEditor from SettingFile to System * [REF] Extract updatePluginLine from OhMyZsh::activate * [FIX] Fix case * in diff * [REF] Fix indetation issues * WIP * WIP * [REF] Restructure OhMyZsh extract Zshrc * WIP * [MOD] Update CHANGELOG and and version in aws-upload * WIP * [FIX] Line ending unix * [FIX] Line ending * WIP * WIP * [MOD] Update autocomplete as well on selfupdate --- .editorconfig | 13 ++ CHANGELOG.md | 22 ++- aws-upload | 3 +- src/AwsUpload/AwsUpload.php | 5 +- src/AwsUpload/Command/AutoCompleteCommand.php | 34 +++-- src/AwsUpload/Command/DeleteCommand.php | 2 +- src/AwsUpload/Command/DiffCommand.php | 93 ++++++++++++ src/AwsUpload/Command/SelfUpdateCommand.php | 5 + src/AwsUpload/Command/UploadCommand.php | 2 + src/AwsUpload/Message/CheckMessage.php | 36 ++--- src/AwsUpload/Message/CopyMessage.php | 4 +- src/AwsUpload/Message/DeleteMessage.php | 9 +- src/AwsUpload/Message/EditMessage.php | 9 +- src/AwsUpload/Message/EnvsMessage.php | 12 +- src/AwsUpload/Message/ErrorMessage.php | 67 +++++---- src/AwsUpload/Message/ExportMessage.php | 9 +- src/AwsUpload/Message/HelpMessage.php | 6 +- src/AwsUpload/Message/ImportMessage.php | 15 +- src/AwsUpload/Message/NewMessage.php | 19 ++- src/AwsUpload/Message/RsyncMessage.php | 8 +- src/AwsUpload/Message/VersionMessage.php | 6 +- src/AwsUpload/Model/Settings.php | 2 +- src/AwsUpload/Setting/SettingFile.php | 4 +- src/AwsUpload/System/OhMyZsh.php | 15 +- src/AwsUpload/System/Rsync.php | 111 +++++++++++++- src/AwsUpload/System/RsyncCommands.php | 19 +++ src/AwsUpload/System/System.php | 41 ++++++ src/AwsUpload/System/Zsh.php | 6 +- src/AwsUpload/System/Zshrc.php | 137 ++++++++++++++++++ tests/AwsUpload/Command/EnvsTest.php | 29 ++-- tests/AwsUpload/Command/ImportTest.php | 3 +- tests/AwsUpload/Command/KeysTest.php | 12 +- tests/AwsUpload/Command/ProjsTest.php | 10 +- tests/AwsUpload/Setting/SettingFileTest.php | 4 +- tests/AwsUpload/System/OhMyZshTest.php | 79 ++++++++++ tests/AwsUpload/System/RsyncTest.php | 46 +++++- tests/AwsUpload/System/SystemTest.php | 36 +++++ 37 files changed, 764 insertions(+), 169 deletions(-) create mode 100644 .editorconfig create mode 100644 src/AwsUpload/Command/DiffCommand.php create mode 100644 src/AwsUpload/System/RsyncCommands.php create mode 100644 src/AwsUpload/System/System.php create mode 100644 src/AwsUpload/System/Zshrc.php create mode 100644 tests/AwsUpload/System/OhMyZshTest.php create mode 100644 tests/AwsUpload/System/SystemTest.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..a5d1df3 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +# EditorConfig is awesome: http://EditorConfig.org + +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +[*] +charset = utf-8 +indent_style = space +indent_size = 4 +insert_final_newline = true +end_of_line = lf +trim_trailing_whitespace = true diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a7e684..9af5538 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,19 @@ # Change Log All notable changes to this project will be documented in this file. +## 1.10.0 +## Added +- Add new feature `aws-upload diff` to see files not yet in sync. +- Use `.editorconfig` to have clean files. + +### Changed +- Refactor System\Rsync to handle multiple commands. +- Improve tests Rsync. +- Improve help section. + +### Fixed +- Bug when no $EDITOR variable is defined. +- Bug sed error code m. ## 1.9.0 - 2017-09-05 ### Added @@ -17,9 +30,6 @@ All notable changes to this project will be documented in this file. - Merge Check to SettingFile. - Improve help section. -### Fixed -- namespace for tests - ## 1.8.0 - 2017-07-27 ### Added - Add new feature `aws-upload autocomplete` to enable autocomplete in `aws-upload`. @@ -32,7 +42,7 @@ All notable changes to this project will be documented in this file. - Clean up the script section. ### Fixed -- namespace for tests +- Namespace for tests ## 1.7.2 - 2017-07-12 ### Changed @@ -144,7 +154,7 @@ All notable changes to this project will be documented in this file. - Added `.travis.yml` `phpunit.xml.dist`. ## 0.0.2 - 2017-03-06 -### Added +### Added - Added LICENCE file for MIT. ### Changed @@ -158,4 +168,4 @@ All notable changes to this project will be documented in this file. - Added `composer.json`. - Added Facilitator to print `help`, `banner`, `version`. - Added Rsync to user `rsync`. -- Added SettingFolder to get `getHomeDir`, `getPath`. \ No newline at end of file +- Added SettingFolder to get `getHomeDir`, `getPath`. diff --git a/aws-upload b/aws-upload index 0010d7f..d64c3b5 100755 --- a/aws-upload +++ b/aws-upload @@ -9,6 +9,7 @@ if (file_exists(__DIR__ . '/../../autoload.php')) { use AwsUpload\AwsUpload; -$aws = new AwsUpload('1.9.0', '1.1.0'); +$aws = new AwsUpload('1.10.0', '1.2.0'); $status = $aws->run(); exit($status); + diff --git a/src/AwsUpload/AwsUpload.php b/src/AwsUpload/AwsUpload.php index 208c288..bb12320 100644 --- a/src/AwsUpload/AwsUpload.php +++ b/src/AwsUpload/AwsUpload.php @@ -93,6 +93,7 @@ public function __construct($version = 'test', $plugin = 'test') 'new' => array('new', 'n'), 'envs' => array('envs', 'e'), 'edit' => array('edit', 'E'), + 'diff' => array('diff', 'df'), 'delete' => array('delete', 'rm'), 'copy' => array('copy', 'cp'), 'help' => array('help', 'h'), @@ -121,7 +122,7 @@ public function run() if ($this->args->verbose) { $this->is_verbose = true; } - + if ($this->args->quiet) { $this->is_quiet = true; } @@ -177,6 +178,7 @@ public function fetchArgsCmd() 'projs' => 'AwsUpload\Command\Projs', 'envs' => 'AwsUpload\Command\Envs', 'new' => 'AwsUpload\Command\New', + 'diff' => 'AwsUpload\Command\Diff', 'edit' => 'AwsUpload\Command\Edit', 'copy' => 'AwsUpload\Command\Copy', 'delete' => 'AwsUpload\Command\Delete', @@ -186,7 +188,6 @@ public function fetchArgsCmd() 'selfupdate' => 'AwsUpload\Command\SelfUpdate', 'autocomplete' => 'AwsUpload\Command\AutoComplete', ); - foreach ($cmdList as $arg => $cmdName) { if ($this->args->{$arg} && empty($cmd)) { $cmd = $cmdName; diff --git a/src/AwsUpload/Command/AutoCompleteCommand.php b/src/AwsUpload/Command/AutoCompleteCommand.php index f46688c..ab0396f 100644 --- a/src/AwsUpload/Command/AutoCompleteCommand.php +++ b/src/AwsUpload/Command/AutoCompleteCommand.php @@ -53,8 +53,10 @@ public function run() $this->app->inline(' Plugin already activated'); } - $this->app->inline("\nProcedure complete. Please reload the shell."); + + system('zsh'); + return Status::SUCCESS; } @@ -97,23 +99,23 @@ public function printSystemStatus() $zsh = Zsh::isInstalled(); $omz = OhMyZsh::isInstalled(); - $git_msg = " " . - CommonMessage::plot($git, $check) . - " Git \t" . - CommonMessage::plot($git, $labels); - $zsh_msg = " " . - CommonMessage::plot($zsh, $check) . - " Zsh \t" . - CommonMessage::plot($zsh, $labels); - $omz_msg = " " . - CommonMessage::plot($omz, $check) . - " Oh-my-zsh \t" . - CommonMessage::plot($omz, $labels); + $git_line = " " . + CommonMessage::plot($git, $check) . + " Git \t" . + CommonMessage::plot($git, $labels); + $zsh_line = " " . + CommonMessage::plot($zsh, $check) . + " Zsh \t" . + CommonMessage::plot($zsh, $labels); + $omz_line = " " . + CommonMessage::plot($omz, $check) . + " Oh-my-zsh \t" . + CommonMessage::plot($omz, $labels); $this->app->inline(""); - $this->app->inline($git_msg); - $this->app->inline($zsh_msg); - $this->app->inline($omz_msg); + $this->app->inline($git_line); + $this->app->inline($zsh_line); + $this->app->inline($omz_line); $this->app->inline(""); } diff --git a/src/AwsUpload/Command/DeleteCommand.php b/src/AwsUpload/Command/DeleteCommand.php index 4202072..708d9b4 100644 --- a/src/AwsUpload/Command/DeleteCommand.php +++ b/src/AwsUpload/Command/DeleteCommand.php @@ -49,7 +49,7 @@ public function getConfirmation() { $this->app->inline("Are you sure you want delete " . $this->key . "?(y|n)"); - $handle = fopen("php://stdin","r"); + $handle = fopen("php://stdin", "r"); $line = fgets($handle); fclose($handle); diff --git a/src/AwsUpload/Command/DiffCommand.php b/src/AwsUpload/Command/DiffCommand.php new file mode 100644 index 0000000..6d30276 --- /dev/null +++ b/src/AwsUpload/Command/DiffCommand.php @@ -0,0 +1,93 @@ + + * @copyright 2017 Marco Buttini + * @license http://www.opensource.org/licenses/mit-license.php The MIT License + */ + +namespace AwsUpload\Command; + +use AwsUpload\Model\Status; +use AwsUpload\System\Rsync; +use AwsUpload\Setting\SettingFile; +use AwsUpload\Message\RsyncMessage; +use AwsUpload\System\RsyncCommands; + +class DiffCommand extends FileCommand +{ + /** + * Property true if app is simulate. + * + * @var bool + */ + public $is_simulate; + + /** + * Property true if app is verbose. + * + * @var bool + */ + public $is_verbose; + + /** + * @var string + */ + public $key; + + /** + * @var string + */ + public $proj; + + /** + * @var string + */ + public $env; + + public function init() + { + $this->key = $this->app->args->getFirst('diff'); + $this->is_verbose = $this->app->args->verbose; + $this->is_simulate = $this->app->args->simulate; + + list($proj, $env) = explode('.', $this->key); + $this->proj = $proj; + $this->env = $env; + } + + /** + * Method to run the rsync cmd. + * + * @return mixed The status code. + */ + public function exec() + { + $settings = SettingFile::getObject($this->key); + + $rsync = new Rsync($settings); + $rsync->setVerbose($this->is_verbose); + $rsync->setAction(RsyncCommands::DIFF); + + $msg = RsyncMessage::banner($this->proj, $this->env, $rsync->cmd); + $this->app->inline($msg); + + if ($this->is_simulate) { + $this->app->inline($rsync->getCmd()); + return $this->simulate(); + } + + $rsync->run(); + } + + public function simulate() + { + $this->msg = 'Simulation mode' . "\n"; + + return $this->handleSuccess(); + } +} diff --git a/src/AwsUpload/Command/SelfUpdateCommand.php b/src/AwsUpload/Command/SelfUpdateCommand.php index 5657b71..9079222 100644 --- a/src/AwsUpload/Command/SelfUpdateCommand.php +++ b/src/AwsUpload/Command/SelfUpdateCommand.php @@ -13,6 +13,7 @@ namespace AwsUpload\Command; use AwsUpload\Model\Status; +use AwsUpload\System\OhMyZsh; class SelfUpdateCommand extends BasicCommand { @@ -27,6 +28,10 @@ public function run() system('composer -vvv global require aws-upload/aws-upload'); $this->app->inline("Self-update completed"); + if (OhMyZsh::isPluginActive()) { + system('aws-upload autocomplete'); + } + return Status::SUCCESS; } } diff --git a/src/AwsUpload/Command/UploadCommand.php b/src/AwsUpload/Command/UploadCommand.php index f30026e..5c199f8 100644 --- a/src/AwsUpload/Command/UploadCommand.php +++ b/src/AwsUpload/Command/UploadCommand.php @@ -16,6 +16,7 @@ use AwsUpload\System\Rsync; use AwsUpload\Setting\SettingFile; use AwsUpload\Message\RsyncMessage; +use AwsUpload\System\RsyncCommands; class UploadCommand extends FileCommand { @@ -76,6 +77,7 @@ public function exec() $rsync = new Rsync($settings); $rsync->setVerbose($this->is_verbose); + $rsync->setAction(RsyncCommands::UPLOAD); $msg = RsyncMessage::banner($this->proj, $this->env, $rsync->cmd); $this->app->inline($msg); diff --git a/src/AwsUpload/Message/CheckMessage.php b/src/AwsUpload/Message/CheckMessage.php index 5fe2fba..52c1c66 100644 --- a/src/AwsUpload/Message/CheckMessage.php +++ b/src/AwsUpload/Message/CheckMessage.php @@ -43,44 +43,44 @@ public static function report($report) $local_exists = CommonMessage::plot($report['local_exists'], $exist_labels); // Json - $msg = "Checking...\n\n" - . " File analysing:\n" - . " " . $report['path'] . "" . "\n" - . " " . $check_json . " Json " . $is_valid_json . "\n" - . " " . $report['error_json']; + $text = "Checking...\n\n" . + " File analysing:\n" . + " " . $report['path'] . "" . "\n" . + " " . $check_json . " Json " . $is_valid_json . "\n" . + " " . $report['error_json']; // Pem - $msg .= "\n" - . " Pem File:\n" - . " " . $report['pem'] . "\n" - . " " . $check_pem . " Pem " . $pem_exists . "\n"; + $text .= "\n" . + " Pem File:\n" . + " " . $report['pem'] . "\n" . + " " . $check_pem . " Pem " . $pem_exists . "\n"; if ($report['pem_exists']) { - $msg .= " " . $check_400 . " Pem Perm " . $is_400_perms . "\n"; + $text .= " " . $check_400 . " Pem Perm " . $is_400_perms . "\n"; if (!$report['is_400']) { - $msg .= ' Try to type: chmod 400 ' . $report['pem'] . "\n"; + $text .= ' Try to type: chmod 400 ' . $report['pem'] . "\n"; } } // Local - $msg .= "\n" - . " Local Folder:\n" - . " " . $report['local'] . "" . "\n" - . " " . $check_loc . " Folder " . $local_exists . "\n"; + $text .= "\n" . + " Local Folder:\n" . + " " . $report['local'] . "" . "\n" . + " " . $check_loc . " Folder " . $local_exists . "\n"; - return $msg; + return $text; } public static function noArgs() { - $msg = "It seems that you don't proper arguments for this command.\n\n" . + $text = "It seems that you don't proper arguments for this command.\n\n" . "How to use check:\n\n" . " aws-upload check \n" . " E.g.: aws-upload check blog.dev\n\n" . "\n"; - return $msg; + return $text; } } diff --git a/src/AwsUpload/Message/CopyMessage.php b/src/AwsUpload/Message/CopyMessage.php index 8c9019f..7ed0822 100644 --- a/src/AwsUpload/Message/CopyMessage.php +++ b/src/AwsUpload/Message/CopyMessage.php @@ -18,13 +18,13 @@ class CopyMessage implements ArgCommandMessage { public static function noArgs() { - $msg = "It seems that you don't proper arguments for this command.\n\n" . + $text = "It seems that you don't proper arguments for this command.\n\n" . "How to use copy:\n\n" . " aws-upload copy \n" . " E.g.: aws-upload copy blog.dev blog.prod\n\n" . "\n"; - return $msg; + return $text; } } diff --git a/src/AwsUpload/Message/DeleteMessage.php b/src/AwsUpload/Message/DeleteMessage.php index 2285c92..b99e342 100644 --- a/src/AwsUpload/Message/DeleteMessage.php +++ b/src/AwsUpload/Message/DeleteMessage.php @@ -25,19 +25,20 @@ class DeleteMessage implements ArgCommandMessage */ public static function success($key) { - $msg = "The setting file " . $key . ".json has been deleted successfully.\n\n"; - return $msg; + $text = "The setting file " . $key . ".json has been deleted successfully.\n\n"; + + return $text; } public static function noArgs() { - $msg = "It seems that you don't proper arguments for this command.\n\n" . + $text = "It seems that you don't proper arguments for this command.\n\n" . "How to use delete:\n\n" . " aws-upload delete \n" . " E.g.: aws-upload delete blog.dev\n\n" . "\n"; - return $msg; + return $text; } } diff --git a/src/AwsUpload/Message/EditMessage.php b/src/AwsUpload/Message/EditMessage.php index ec4f106..ca58a08 100644 --- a/src/AwsUpload/Message/EditMessage.php +++ b/src/AwsUpload/Message/EditMessage.php @@ -25,19 +25,20 @@ class EditMessage implements ArgCommandMessage */ public static function success($key) { - $msg = "The setting file " . $key . ".json has been edited successfully.\n\n"; - return $msg; + $text = "The setting file " . $key . ".json has been edited successfully.\n\n"; + + return $text; } public static function noArgs() { - $msg = "It seems that you don't proper arguments for this command.\n\n" . + $text = "It seems that you don't proper arguments for this command.\n\n" . "How to use edit:\n\n" . " aws-upload edit \n" . " E.g.: aws-upload edit blog.dev\n\n" . "\n"; - return $msg; + return $text; } } diff --git a/src/AwsUpload/Message/EnvsMessage.php b/src/AwsUpload/Message/EnvsMessage.php index 0fdd4f9..860f800 100644 --- a/src/AwsUpload/Message/EnvsMessage.php +++ b/src/AwsUpload/Message/EnvsMessage.php @@ -26,19 +26,19 @@ class EnvsMessage */ public static function errorNoEnvsProj($projFilter) { - $projs = SettingFile::getProjs(); - $msg = "The project " . $projFilter . " you are tring to use doesn't exist." . "\n\n"; + $projList = SettingFile::getProjs(); + $text = "The project " . $projFilter . " you are tring to use doesn't exist." . "\n\n"; $next = "These are the available projects: \n\n"; - foreach ($projs as $proj) { + foreach ($projList as $proj) { $next .= " + " . $proj . "\n"; } - if (count($projs) > 0) { + if (count($projList) > 0) { $next .= "\nTo get the envs from one of them, run (for example):\n\n" . - " aws-upload -e " . $projs[0] . "\n"; + " aws-upload -e " . $projList[0] . "\n"; } - return $msg . $next . "\n"; + return $text . $next . "\n"; } } diff --git a/src/AwsUpload/Message/ErrorMessage.php b/src/AwsUpload/Message/ErrorMessage.php index e989496..cabd0b1 100644 --- a/src/AwsUpload/Message/ErrorMessage.php +++ b/src/AwsUpload/Message/ErrorMessage.php @@ -26,10 +26,10 @@ class ErrorMessage */ public static function noArgs() { - $msg = "It seems that you don't have the correct args for this command.\n" - . "\n"; + $text = "It seems that you don't have the correct args for this command.\n" . + "\n"; - return $msg; + return $text; } /** @@ -39,10 +39,11 @@ public static function noArgs() */ public static function noProjects() { - $msg = "It seems that you don't have any project setup.\nTry to type:\n\n" - . " aws-upload new project.test\n" - . "\n"; - return $msg; + $text = "It seems that you don't have any project setup.\nTry to type:\n\n" . + " aws-upload new project.test\n" . + "\n"; + + return $text; } /** @@ -61,21 +62,21 @@ public static function noFileFound($project, $env = null) { $files = SettingFile::getList(); if (count($files) === 0) { - $msg = static::noProjects(); - return $msg; + $text = static::noProjects(); + return $text; } - $msg = "It seems that there is NO setting files for " . $project . - ", " . $env . "\n\n"; + $text = "It seems that there is NO setting files for " . $project . + ", " . $env . "\n\n"; if (is_null($env)) { - $msg = "It seems that there is NO setting files for " . $project . - "\n\n"; + $text = "It seems that there is NO setting files for " . $project . + "\n\n"; } - $msg .= static::getProjEnvTable(); + $text .= static::getProjEnvTable(); - return $msg; + return $text; } /** @@ -87,18 +88,19 @@ public static function noFileFound($project, $env = null) */ public static function noValidKey($key) { - $msg = "It seems that the key " . $key . " is not valid:\n\n" - . "Please try to use this format:\n" - . " - [project].[environmet]\n\n" - . "Examples of valid key to create a new setting file:\n" - . " - my-site.staging\n" - . " - my-site.dev\n" - . " - my-site.prod\n\n" - . "Tips on choosing the key name:\n" - . " - for [project] and [environmet] try to be: short, sweet, to the point\n" - . " - use only one 'dot' . in the name\n" - . "\n"; - return $msg; + $text = "It seems that the key " . $key . " is not valid:\n\n" . + "Please try to use this format:\n" . + " - [project].[environmet]\n\n" . + "Examples of valid key to create a new setting file:\n" . + " - my-site.staging\n" . + " - my-site.dev\n" . + " - my-site.prod\n\n" . + "Tips on choosing the key name:\n" . + " - for [project] and [environmet] try to be: short, sweet, to the point\n" . + " - use only one 'dot' . in the name\n" . + "\n"; + + return $text; } /** @@ -110,11 +112,12 @@ public static function noValidKey($key) */ public static function keyAlreadyExists($key) { - $msg = "It seems that the key " . $key . " already exists try to use another one.\n\n" - . "Please consider you already have the following elements:\n" - . static::getProjEnvTable() - . "\n"; - return $msg; + $text = "It seems that the key " . $key . " already exists try to use another one.\n\n" . + "Please consider you already have the following elements:\n" . + static::getProjEnvTable() . + "\n"; + + return $text; } diff --git a/src/AwsUpload/Message/ExportMessage.php b/src/AwsUpload/Message/ExportMessage.php index 476a168..7809e2e 100644 --- a/src/AwsUpload/Message/ExportMessage.php +++ b/src/AwsUpload/Message/ExportMessage.php @@ -25,13 +25,14 @@ class ExportMessage implements ArgCommandMessage */ public static function success($key) { - $msg = "The setting file " . $key . ".json has been exported successfully.\n\n"; - return $msg; + $text = "The setting file " . $key . ".json has been exported successfully.\n\n"; + + return $text; } public static function noArgs() { - $msg = "It seems that you don't proper arguments for this command.\n\n" . + $text = "It seems that you don't proper arguments for this command.\n\n" . "How to use export:\n\n" . " aws-upload export []\n" . @@ -40,6 +41,6 @@ public static function noArgs() "\n"; - return $msg; + return $text; } } diff --git a/src/AwsUpload/Message/HelpMessage.php b/src/AwsUpload/Message/HelpMessage.php index fb7b38f..89c9dc5 100644 --- a/src/AwsUpload/Message/HelpMessage.php +++ b/src/AwsUpload/Message/HelpMessage.php @@ -22,7 +22,7 @@ class HelpMessage */ public static function success() { - $msg = <<Usage: @@ -32,6 +32,7 @@ public static function success() aws-upload projs [-q|--quiet] aws-upload envs [-q|--quiet] + aws-upload diff # The format is proj.env eg: landing.test aws-upload new # The format is proj.env eg: landing.test aws-upload edit # The format is proj.env eg: landing.test aws-upload copy # and are in the format proj.env @@ -57,6 +58,7 @@ public static function success() Available commands: + -df|diff Show the files that are not yet synced. -k|keys Print all the projects' keys. -p|projs Print all the projects. -e|envs Print all the environments for a specific project. @@ -73,6 +75,6 @@ public static function success() EOT; - return $msg; + return $text; } } diff --git a/src/AwsUpload/Message/ImportMessage.php b/src/AwsUpload/Message/ImportMessage.php index ee37420..636a725 100644 --- a/src/AwsUpload/Message/ImportMessage.php +++ b/src/AwsUpload/Message/ImportMessage.php @@ -25,21 +25,22 @@ class ImportMessage implements ArgCommandMessage */ public static function success($key) { - $msg = "The setting file " . $key . ".json has been imported successfully.\n\n" . + $text = "The setting file " . $key . ".json has been imported successfully.\n\n" . "To edit again the file type:\n" . " aws-upload edit " . $key . "\n" . "\n"; - return $msg; + + return $text; } public static function errorNotFound($path) { - $msg = "It seems that you don't proper arguments for this command.\n\n" . + $text = "It seems that you don't proper arguments for this command.\n\n" . "Argument given:\n\n" . " src: " . $path . "\n\n" . - + "How to use import:\n\n" . " aws-upload import \n" . " E.g.: aws-upload import ~/Desktop/blog.dev.json\n\n" . @@ -50,18 +51,18 @@ public static function errorNotFound($path) " - the argument give was a file but it doesn't exist\n" . "\n"; - return $msg; + return $text; } public static function noArgs() { - $msg = "It seems that you don't proper arguments for this command.\n\n" . + $text = "It seems that you don't proper arguments for this command.\n\n" . "How to use import:\n\n" . " aws-upload import \n" . " E.g.: aws-upload import ~/Desktop/blog.dev.json\n\n" . "\n"; - return $msg; + return $text; } } diff --git a/src/AwsUpload/Message/NewMessage.php b/src/AwsUpload/Message/NewMessage.php index 3a23ba9..65b73ac 100644 --- a/src/AwsUpload/Message/NewMessage.php +++ b/src/AwsUpload/Message/NewMessage.php @@ -25,22 +25,25 @@ class NewMessage implements ArgCommandMessage */ public static function success($key) { - $msg = "The setting file " . $key . ".json has been created successfully.\n\n" - . "To edit again the file type:\n" - . " aws-upload edit " . $key . "\n" - . "\n"; - return $msg; + $text = "The setting file " . $key . ".json has been created successfully.\n\n" . + "To edit again the file type:\n" . + " aws-upload edit " . $key . "\n" . + "\n"; + + return $text; } + /** + * @return string + */ public static function noArgs() { - $msg = "It seems that you don't proper arguments for this command.\n\n" . - + $text = "It seems that you don't proper arguments for this command.\n\n" . "How to use new:\n\n" . " aws-upload new \n" . " E.g.: aws-upload new blog.prod\n\n" . "\n"; - return $msg; + return $text; } } diff --git a/src/AwsUpload/Message/RsyncMessage.php b/src/AwsUpload/Message/RsyncMessage.php index c8c1c58..ea2c11f 100644 --- a/src/AwsUpload/Message/RsyncMessage.php +++ b/src/AwsUpload/Message/RsyncMessage.php @@ -26,10 +26,10 @@ class RsyncMessage */ public static function banner($proj, $env, $cmd) { - $env = escapeshellarg($env); - $proj = escapeshellarg($proj); + $env = escapeshellarg($env); + $proj = escapeshellarg($proj); - $msg = <<================================================================== _ _ @@ -51,6 +51,6 @@ public static function banner($proj, $env, $cmd) ================================================================== EOT; - return $msg; + return $text; } } diff --git a/src/AwsUpload/Message/VersionMessage.php b/src/AwsUpload/Message/VersionMessage.php index 45eb380..9a46e26 100644 --- a/src/AwsUpload/Message/VersionMessage.php +++ b/src/AwsUpload/Message/VersionMessage.php @@ -23,7 +23,9 @@ class VersionMessage */ public static function success($version) { - $msg = "aws-upload version " . $version . " \n"; - return $msg; + $text = "aws-upload version " . $version . " \n"; + + return $text; } } + diff --git a/src/AwsUpload/Model/Settings.php b/src/AwsUpload/Model/Settings.php index 31d1864..b350d68 100644 --- a/src/AwsUpload/Model/Settings.php +++ b/src/AwsUpload/Model/Settings.php @@ -126,7 +126,7 @@ public static function getErrorJson() JSON_ERROR_UTF8 => " - Malformed UTF-8 characters, possibly incorrectly encoded\n", ); $last_error = json_last_error(); - + $msg = ' - Unknown error'; if (array_key_exists($last_error, $errors)) { $msg = $errors[$last_error]; diff --git a/src/AwsUpload/Setting/SettingFile.php b/src/AwsUpload/Setting/SettingFile.php index 18c2f12..92e0467 100644 --- a/src/AwsUpload/Setting/SettingFile.php +++ b/src/AwsUpload/Setting/SettingFile.php @@ -13,6 +13,7 @@ namespace AwsUpload\Setting; use AwsUpload\System\File; +use AwsUpload\System\System; use AwsUpload\Model\Settings; use AwsUpload\Setting\SettingFolder; @@ -186,8 +187,9 @@ public static function create($key) public static function edit($key) { $path = SettingFolder::getPath(); + $editor = System::getEditor(); - system('$EDITOR ' . $path . '/' . $key . '.json < `tty` > `tty`'); + system($editor . ' ' . $path . '/' . $key . '.json < `tty` > `tty`'); } /** diff --git a/src/AwsUpload/System/OhMyZsh.php b/src/AwsUpload/System/OhMyZsh.php index c432dd2..9a49e81 100644 --- a/src/AwsUpload/System/OhMyZsh.php +++ b/src/AwsUpload/System/OhMyZsh.php @@ -12,6 +12,8 @@ namespace AwsUpload\System; +use AwsUpload\Setting\SettingFolder; + class OhMyZsh { /** @@ -23,6 +25,7 @@ public static function isInstalled() { $dir = self::getPath(); $file = self::getPath() . '/oh-my-zsh.sh'; + return is_dir($dir) && is_file($file); } @@ -33,11 +36,12 @@ public static function isInstalled() */ public static function errorMsg() { - $msg = "\n It seems that oh-my-zsh is not installed.\n" . + $text = "\n It seems that oh-my-zsh is not installed.\n" . " Please run (or equivalent for your system):\n\n" . " sh -c \"$(wget https://raw.githubusercontent.com/" . "robbyrussell/oh-my-zsh/master/tools/install.sh -O -)\"\n"; - return $msg; + + return $text; } /** @@ -72,17 +76,18 @@ public static function hasPluginFiles() public static function isPluginActive() { $isActive = exec("grep aws-upload ~/.zshrc"); + return (strlen($isActive) > 0); } /** * Activate the plugin. * - * @return bool + * @return void */ public static function activate() { - $cmd = "sed -i '/^plugins=(/ s/)$/ aws-upload)/' ~/.zshrc"; - return exec($cmd); + $zshrc = new Zshrc(); + $zshrc->enablePlugin('aws-upload'); } } diff --git a/src/AwsUpload/System/Rsync.php b/src/AwsUpload/System/Rsync.php index 309d207..8a122ae 100644 --- a/src/AwsUpload/System/Rsync.php +++ b/src/AwsUpload/System/Rsync.php @@ -12,6 +12,8 @@ namespace AwsUpload\System; +use AwsUpload\System\RsyncCommands; + class Rsync { /** @@ -22,7 +24,7 @@ class Rsync public $cmd; /** - * It containg the settings object + * It contains the settings object * * Eg: * { pem , exclude, remote, local } @@ -31,6 +33,14 @@ class Rsync */ public $settings; + /** + * It contains the action to perform + * + * @see AwsUpload\System\RsyncCommands + * @var string + */ + public $action; + public $is_verbose = false; /** @@ -54,6 +64,11 @@ public function setVerbose($verbose) $this->is_verbose = (bool) $verbose; } + public function setAction($action) + { + $this->action = $action; + } + /** * Method to build the rsync command from the settings object * @@ -61,21 +76,69 @@ public function setVerbose($verbose) */ public function getCmd() { - $settings = $this->settings; + $cmd = ""; - $cmd = "rsync "; - $cmd .= ($this->is_verbose) ? " -v --stats --progress " : ""; - $cmd .= "-ravze \"ssh -i " . $settings->pem . "\" "; + if (RsyncCommands::UPLOAD === $this->action) { + $cmd = $this->getUploadCommand(); + } + + if (RsyncCommands::DIFF === $this->action) { + $cmd = $this->getDiffCommand(); + } + + return $cmd; + } - // exclude + /** + * @return string + */ + public function getUploadCommand() + { + $cmd = "rsync "; + $cmd .= $this->getVerboseFlags(); + $cmd .= $this->getSshDetails(); $cmd .= $this->getExclude(); - $cmd .= " --exclude .DS_Store "; + $cmd .= $this->getLocal(); + $cmd .= $this->getRemote(); + + return $cmd; + } + + /** + * @return string + */ + public function getDiffCommand() + { + $cmd = "rsync --dry-run "; - $cmd .= $settings->local . " " . escapeshellarg($settings->remote); + $cmd .= $this->getVerboseFlags(); + $cmd .= $this->getSshDetails(); + $cmd .= $this->getExclude(); + $cmd .= $this->getOnlyPathLocal(); + $cmd .= $this->getRemote(); return $cmd; } + /** + * @return string + */ + public function getVerboseFlags() + { + return ($this->is_verbose) ? " -v --stats --progress " : ""; + } + + /** + * @return string + */ + public function getSshDetails() + { + return "-ravze \"ssh -i " . $this->settings->pem . "\" "; + } + + /** + * @return string + */ public function getExclude() { $settings = $this->settings; @@ -89,9 +152,41 @@ public function getExclude() $cmd .= " --exclude " . escapeshellarg($elem) . " "; } + $cmd .= " --exclude .DS_Store "; + return $cmd; } + /** + * @return string + */ + public function getLocal() + { + return $this->settings->local . " "; + } + + /** + * @return string + */ + public function getOnlyPathLocal() + { + $local = trim($this->settings->local); + + if (strpos($local, '*') === strlen($local) - 1) { + $local = substr($local, 0, -1); + } + + return escapeshellarg($local) . " "; + } + + /** + * @return string + */ + public function getRemote() + { + return escapeshellarg($this->settings->remote) . " "; + } + /** * Method to run the rsync command * diff --git a/src/AwsUpload/System/RsyncCommands.php b/src/AwsUpload/System/RsyncCommands.php new file mode 100644 index 0000000..5119bb7 --- /dev/null +++ b/src/AwsUpload/System/RsyncCommands.php @@ -0,0 +1,19 @@ + + * @copyright 2017 Marco Buttini + * @license http://www.opensource.org/licenses/mit-license.php The MIT License + */ + +namespace AwsUpload\System; + +interface RsyncCommands +{ + const UPLOAD = 'UPLOAD'; + const DIFF = 'DIFF'; +} diff --git a/src/AwsUpload/System/System.php b/src/AwsUpload/System/System.php new file mode 100644 index 0000000..d6383c2 --- /dev/null +++ b/src/AwsUpload/System/System.php @@ -0,0 +1,41 @@ + + * @copyright 2017 Marco Buttini + * @license http://www.opensource.org/licenses/mit-license.php The MIT License + */ + +namespace AwsUpload\System; + +class System +{ + private static function getValue($array, $key) + { + return isset($array[$key]) ? $array[$key] : ''; + } + + /** + * Method to get a default editor. + * + * @return string + */ + public static function getEditor() + { + $editor = self::getValue($_ENV, 'EDITOR'); + + if (empty($editor)) { + $editor = self::getValue($_SERVER, 'EDITOR'); + } + + if (empty($editor)) { + $editor = 'vim'; + } + + return $editor; + } +} diff --git a/src/AwsUpload/System/Zsh.php b/src/AwsUpload/System/Zsh.php index 3faf9a6..8e65275 100644 --- a/src/AwsUpload/System/Zsh.php +++ b/src/AwsUpload/System/Zsh.php @@ -22,6 +22,7 @@ class Zsh public static function isInstalled() { $count = (int) exec('grep /zsh$ /etc/shells | wc -l'); + return ($count >= 1); } @@ -32,10 +33,11 @@ public static function isInstalled() */ public static function errorMsg() { - $msg = "\n It seems that zsh is not installed.\n" . + $text = "\n It seems that zsh is not installed.\n" . " Please run (or equivalent for your system):\n\n" . " sudo apt-get install zsh\n" . " sudo chsh zsh\n"; - return $msg; + + return $text; } } diff --git a/src/AwsUpload/System/Zshrc.php b/src/AwsUpload/System/Zshrc.php new file mode 100644 index 0000000..da17d7e --- /dev/null +++ b/src/AwsUpload/System/Zshrc.php @@ -0,0 +1,137 @@ + + * @copyright 2017 Marco Buttini + * @license http://www.opensource.org/licenses/mit-license.php The MIT License + */ + +namespace AwsUpload\System; + +use AwsUpload\Setting\SettingFolder; + +class Zshrc +{ + /** + * @var string + */ + public $zshrc; + + + public function __construct() + { + $this->zshrc = SettingFolder::getHomeDir() . '/../.zshrc'; + } + + /** + * Activate the plugin. + * + * @return bool + */ + public function enablePlugin($plugin_name) + { + $zshrc_body = $this->getZshrcContent(); + $zshrc_body = $this->writeInZshrcPluginVariable($zshrc_body, $plugin_name); + + return $this->updateZshrcContent($zshrc_body); + } + + /** + * @param string $line + * @return bool + */ + private function isValidPluginLine($line, $plugin_name) + { + $trimLine = trim($line); + $isCommentLine = ($trimLine[0] === '#'); + $hasPluginsSyntax = (strpos($line, 'plugins=(') !== false); + $hasEndParentesys = (strpos($line, ')') !== false); + $pluginIsNotYetActive = (strpos($line, $plugin_name) === false); + + return (! $isCommentLine && + $hasPluginsSyntax && + $hasEndParentesys && + $pluginIsNotYetActive); + } + + /** + * @param string[] $zshrc_body + * @return bool + */ + private function hasOnePluginLine($zshrc_body) + { + $hasPluginLine = false; + foreach ($zshrc_body as $key => $line) { + $trimLine = trim($line); + + if (empty($trimLine)) { + continue; + } + + $isCommentLine = ($trimLine[0] === '#'); + $hasPluginsSyntax = (strpos($line, 'plugins=(') !== false); + $hasEndParentesys = (strpos($line, ')') !== false); + + $hasPluginLine = (! $isCommentLine && + $hasPluginsSyntax && + $hasEndParentesys) ? true : $hasPluginLine; + } + + return $hasPluginLine; + } + + /** + * @param string[] $zshrc_body + * @param string $plugin_name + * @return string[] $zshrc_body + */ + private function writeInZshrcPluginVariable($zshrc_body, $plugin_name) + { + $zshrc_body = $this->attemptCaseBasicInsert($zshrc_body, $plugin_name); + $zshrc_body = $this->attemptCaseNoPlugin($zshrc_body, $plugin_name); + + return $zshrc_body; + } + + private function attemptCaseBasicInsert($zshrc_body, $plugin_name) + { + // attempt basic insert in one line + foreach ($zshrc_body as $key => $line) { + $trimLine = trim($line); + + if (empty($trimLine)) { + continue; + } + + if ($this->isValidPluginLine($line, $plugin_name)) { + $zshrc_body[$key] = str_replace(')', ' ' . $plugin_name . ')', $line); + } + } + + return $zshrc_body; + } + + private function attemptCaseNoPlugin($zshrc_body, $plugin_name) + { + // case no plugin line at all + if (!$this->hasOnePluginLine($zshrc_body)) { + $zshrc_body[] = "\n" . 'plugins=(' . $plugin_name . ')'; + } + + return $zshrc_body; + } + + private function getZshrcContent() + { + return file($this->zshrc); + } + + private function updateZshrcContent($zshrc_body) + { + return file_put_contents($this->zshrc, implode('', $zshrc_body)); + } +} diff --git a/tests/AwsUpload/Command/EnvsTest.php b/tests/AwsUpload/Command/EnvsTest.php index 11b6cd1..d7765ed 100644 --- a/tests/AwsUpload/Command/EnvsTest.php +++ b/tests/AwsUpload/Command/EnvsTest.php @@ -2,8 +2,11 @@ namespace AwsUpload\Tests\Command; +use AwsUpload\Io\Output; use AwsUpload\AwsUpload; use AwsUpload\Tests\BaseTestCase; +use AwsUpload\Message\ErrorMessage; +use AwsUpload\Message\EnvsMessage; use Symfony\Component\Filesystem\Filesystem; class EnvsTest extends BaseTestCase @@ -11,9 +14,9 @@ class EnvsTest extends BaseTestCase public function test_noProjects_expectedNoProjectMsg() { - $this->expectOutputString("It seems that you don't have any project setup.\nTry to type:\n\n" - . " \e[32maws-upload new project.test\e[0m\n" - . "\n\n"); + $msg = ErrorMessage::noProjects(); + $msg = Output::color($msg); + $this->expectOutputString($msg . "\n"); self::clearArgv(); self::pushToArgv(array('asd.php', '-e', 'proj-3')); @@ -27,28 +30,18 @@ public function test_noProjects_expectedNoProjectMsg() public function test_moreFilesSameProj_expectedProposeAlternative() { - $this->expectOutputString("The project \e[31mproj-3\e[0m you are tring to use doesn't exist. - -These are the available projects: - - + \e[32mproject-1\e[0m - + \e[32mproject-2\e[0m - -To get the envs from one of them, run (for example): - - aws-upload -e project-1 - - -"); - $filesystem = new Filesystem(); $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', '{}'); $filesystem->dumpFile($this->aws_home . '/project-2.prod.json', '{}'); $filesystem->dumpFile($this->aws_home . '/project-1.staging.json', '{}'); - + self::clearArgv(); self::pushToArgv(array('asd.php', '-e', 'proj-3')); + $error = EnvsMessage::errorNoEnvsProj('proj-3') . "\n"; + $error = Output::color($error); + $this->expectOutputString($error); + $aws = new AwsUpload(); $aws->setOutput(new \AwsUpload\Io\OutputEcho()); diff --git a/tests/AwsUpload/Command/ImportTest.php b/tests/AwsUpload/Command/ImportTest.php index 45cb0ef..028c9fc 100644 --- a/tests/AwsUpload/Command/ImportTest.php +++ b/tests/AwsUpload/Command/ImportTest.php @@ -50,7 +50,6 @@ public function test_noValidKey_expected_NoArgsMsg_oneParam() $cmd->run(); } - // "file_exists" => SettingFile::fileExists($dest), public function test_validKeyNoExists_expected_DestAlreadyExists() { $filesystem = new Filesystem(); @@ -89,7 +88,7 @@ public function test_importFile() $cmd = new \AwsUpload\Command\ImportCommand($aws); $cmd->run(); - + $settings = SettingFile::getObject('project-1.dev'); $this->assertEquals('', $settings->pem); diff --git a/tests/AwsUpload/Command/KeysTest.php b/tests/AwsUpload/Command/KeysTest.php index 99d5374..1576035 100644 --- a/tests/AwsUpload/Command/KeysTest.php +++ b/tests/AwsUpload/Command/KeysTest.php @@ -3,7 +3,9 @@ namespace AwsUpload\Tests\Command; use AwsUpload\AwsUpload; +use AwsUpload\Io\Output; use AwsUpload\Tests\BaseTestCase; +use AwsUpload\Message\ErrorMessage; use Symfony\Component\Filesystem\Filesystem; class KeysTest extends BaseTestCase @@ -13,9 +15,9 @@ class KeysTest extends BaseTestCase */ public function test_noProjects_expectedNoProjectMsg() { - $this->expectOutputString("It seems that you don't have any project setup.\nTry to type:\n\n" - . " \e[32maws-upload new project.test\e[0m\n" - . "\n\n"); + $msg = ErrorMessage::noProjects(); + $msg = Output::color($msg); + $this->expectOutputString($msg . "\n"); $aws = new AwsUpload(); $aws->setOutput(new \AwsUpload\Io\OutputEcho()); @@ -27,7 +29,7 @@ public function test_noProjects_expectedNoProjectMsg() public function test_oneFile_expectedProjName() { $this->expectOutputString("project-1.dev\n\n"); - + $filesystem = new Filesystem(); $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', '{}'); @@ -53,7 +55,7 @@ public function test_moreFilesSameProj_expectedProjName() $cmd = new \AwsUpload\Command\KeysCommand($aws); $cmd->run(); } - + public function test_moreFilesDiffProj_expectedProjsName() { $this->expectOutputString("project-1.prod project-1.staging project-2.dev\n\n"); diff --git a/tests/AwsUpload/Command/ProjsTest.php b/tests/AwsUpload/Command/ProjsTest.php index c471d41..50c4b25 100644 --- a/tests/AwsUpload/Command/ProjsTest.php +++ b/tests/AwsUpload/Command/ProjsTest.php @@ -3,7 +3,9 @@ namespace AwsUpload\Tests\Command; use AwsUpload\AwsUpload; +use AwsUpload\Io\Output; use AwsUpload\Tests\BaseTestCase; +use AwsUpload\Message\ErrorMessage; use Symfony\Component\Filesystem\Filesystem; class ProjsTest extends BaseTestCase @@ -13,9 +15,9 @@ class ProjsTest extends BaseTestCase */ public function test_noProjects_expectedNoProjectMsg() { - $this->expectOutputString("It seems that you don't have any project setup.\nTry to type:\n\n" - . " \e[32maws-upload new project.test\e[0m\n" - . "\n\n"); + $msg = ErrorMessage::noProjects(); + $msg = Output::color($msg); + $this->expectOutputString($msg . "\n"); $aws = new AwsUpload(); $aws->setOutput(new \AwsUpload\Io\OutputEcho()); @@ -27,7 +29,7 @@ public function test_noProjects_expectedNoProjectMsg() public function test_oneFile_expectedProjName() { $this->expectOutputString("project-1\n\n"); - + $filesystem = new Filesystem(); $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', '{}'); diff --git a/tests/AwsUpload/Setting/SettingFileTest.php b/tests/AwsUpload/Setting/SettingFileTest.php index baddcea..ef6a3a1 100644 --- a/tests/AwsUpload/Setting/SettingFileTest.php +++ b/tests/AwsUpload/Setting/SettingFileTest.php @@ -102,11 +102,11 @@ public function testGetProjsMoreFilesSameProj() $projs = SettingFile::getProjs(); $envs = SettingFile::getEnvs('project-1'); - + $this->assertEquals(['project-1'], $projs); $this->assertEquals(['dev', 'prod', 'staging'], $envs); } - + public function testGetProjsMoreFilesDiffProj() { $filesystem = new Filesystem(); diff --git a/tests/AwsUpload/System/OhMyZshTest.php b/tests/AwsUpload/System/OhMyZshTest.php new file mode 100644 index 0000000..b7e3b29 --- /dev/null +++ b/tests/AwsUpload/System/OhMyZshTest.php @@ -0,0 +1,79 @@ +aws_home . '/../.zshrc'; + $zshrc_body = 'aaaa'; + + $filesystem = new Filesystem(); + $filesystem->dumpFile($zshrc_path, $zshrc_body); + + $pre_content = file_get_contents($zshrc_path); + $this->assertEquals($pre_content, 'aaaa'); + + OhMyZsh::activate(); + + $post_content = file_get_contents($zshrc_path); + $this->assertEquals($post_content, 'aaaa' . "\n" . 'plugins=(aws-upload)'); + } + + public function test_activate_plugin_one_line() + { + $zshrc_path = $this->aws_home . '/../.zshrc'; + $zshrc_body = 'plugins=(git)'; + + $filesystem = new Filesystem(); + $filesystem->dumpFile($zshrc_path, $zshrc_body); + + $pre_content = file_get_contents($zshrc_path); + $this->assertEquals($pre_content, 'plugins=(git)'); + + OhMyZsh::activate(); + + $post_content = file_get_contents($zshrc_path); + $this->assertEquals('plugins=(git aws-upload)', $post_content); + } + + public function test_activate_plugin_one_line_has_plugin_already() + { + $zshrc_path = $this->aws_home . '/../.zshrc'; + $zshrc_body = 'plugins=(git aws-upload)'; + + $filesystem = new Filesystem(); + $filesystem->dumpFile($zshrc_path, $zshrc_body); + + $pre_content = file_get_contents($zshrc_path); + $this->assertEquals($pre_content, 'plugins=(git aws-upload)'); + + OhMyZsh::activate(); + + $post_content = file_get_contents($zshrc_path); + $this->assertEquals('plugins=(git aws-upload)', $post_content); + } + + public function test_activate_plugin_do_not_touch_comments_line() + { + $zshrc_path = $this->aws_home . '/../.zshrc'; + $zshrc_body = ' # plugins=(git)'; + + $filesystem = new Filesystem(); + $filesystem->dumpFile($zshrc_path, $zshrc_body); + + $pre_content = file_get_contents($zshrc_path); + $this->assertEquals($pre_content, ' # plugins=(git)'); + + OhMyZsh::activate(); + + $post_content = file_get_contents($zshrc_path); + $this->assertEquals(' # plugins=(git)' . "\n" . 'plugins=(aws-upload)', + $post_content); + } +} diff --git a/tests/AwsUpload/System/RsyncTest.php b/tests/AwsUpload/System/RsyncTest.php index b6076c2..5fd447d 100644 --- a/tests/AwsUpload/System/RsyncTest.php +++ b/tests/AwsUpload/System/RsyncTest.php @@ -5,15 +5,16 @@ use AwsUpload\System\Rsync; use AwsUpload\Tests\BaseTestCase; use AwsUpload\Setting\SettingFile; +use AwsUpload\System\RsyncCommands; use Symfony\Component\Filesystem\Filesystem; class RsyncTest extends BaseTestCase { - public function test_buildCmd_object_true() + public function test_buildCmd_upload() { $cmd = 'rsync -ravze "ssh -i /Users/jhon.doe/Documents/certificates/site.pem" --exclude \'.env\' ' . '--exclude \'.git/\' --exclude .DS_Store /Users/jhon.doe/Documents/w/html/ ' . - '\'ec2-user@ec2-xx-xx-xx-xx.ap-southeast-2.compute.amazonaws.com:/var/www/html/site\''; + '\'ec2-user@ec2-xx-xx-xx-xx.ap-southeast-2.compute.amazonaws.com:/var/www/html/site\' '; $json = '{ "pem": "/Users/jhon.doe/Documents/certificates/site.pem", @@ -27,6 +28,47 @@ public function test_buildCmd_object_true() $settings = SettingFile::getObject('project-1.dev'); $rsync = new Rsync($settings); + $rsync->setAction(RsyncCommands::UPLOAD); + $this->assertEquals($rsync->getCmd(), $cmd); + } + + public function test_getExclude() + { + $json = '{ + "pem": "/Users/jhon.doe/Documents/certificates/site.pem", + "local":"/Users/jhon.doe/Documents/w/html/", + "remote":"ec2-user@ec2-xx-xx-xx-xx.ap-southeast-2.compute.amazonaws.com:/var/www/html/site", + "exclude":[".env", ".git/"] + }'; + + $filesystem = new Filesystem(); + $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', $json); + $settings = SettingFile::getObject('project-1.dev'); + + $rsync = new Rsync($settings); + $this->assertEquals($rsync->getExclude(), + " --exclude '.env' --exclude '.git/' --exclude .DS_Store "); + } + + public function test_buildCmd_diff() + { + $cmd = 'rsync --dry-run -ravze "ssh -i /Users/jhon.doe/Documents/certificates/site.pem" --exclude \'.env\' ' . + '--exclude \'.git/\' --exclude .DS_Store \'/Users/jhon.doe/Documents/w/html/\' ' . + '\'ec2-user@ec2-xx-xx-xx-xx.ap-southeast-2.compute.amazonaws.com:/var/www/html/site\' '; + + $json = '{ + "pem": "/Users/jhon.doe/Documents/certificates/site.pem", + "local":"/Users/jhon.doe/Documents/w/html/", + "remote":"ec2-user@ec2-xx-xx-xx-xx.ap-southeast-2.compute.amazonaws.com:/var/www/html/site", + "exclude":[".env", ".git/"] + }'; + + $filesystem = new Filesystem(); + $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', $json); + $settings = SettingFile::getObject('project-1.dev'); + + $rsync = new Rsync($settings); + $rsync->setAction(RsyncCommands::DIFF); $this->assertEquals($rsync->getCmd(), $cmd); } } diff --git a/tests/AwsUpload/System/SystemTest.php b/tests/AwsUpload/System/SystemTest.php new file mode 100644 index 0000000..e16c02e --- /dev/null +++ b/tests/AwsUpload/System/SystemTest.php @@ -0,0 +1,36 @@ +assertEquals($editor, 'env_case'); + } + + public function test_getEditor_SERVER_case() + { + unset($_ENV['EDITOR']); + $_SERVER['EDITOR'] = 'server_case'; + $editor = System::getEditor(); + + $this->assertEquals($editor, 'server_case'); + } + + public function test_getEditor_default_case() + { + unset($_ENV['EDITOR']); + unset($_SERVER['EDITOR']); + $editor = System::getEditor(); + + $this->assertEquals($editor, 'vim'); + } +}