├── .editorconfig ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── LICENCE ├── README.md ├── aws-upload ├── composer.json ├── composer.lock ├── phpunit.xml.dist ├── src └── AwsUpload │ ├── AwsUpload.php │ ├── Command │ ├── AutoCompleteCommand.php │ ├── BasicCommand.php │ ├── CheckCommand.php │ ├── Command.php │ ├── CopyCommand.php │ ├── DeleteCommand.php │ ├── DiffCommand.php │ ├── EditCommand.php │ ├── EnvsCommand.php │ ├── ExportCommand.php │ ├── FileCommand.php │ ├── FullInfoCommand.php │ ├── HelpCommand.php │ ├── ImportCommand.php │ ├── KeysCommand.php │ ├── NewCommand.php │ ├── ProjsCommand.php │ ├── SelfUpdateCommand.php │ ├── UploadCommand.php │ ├── ValidCommand.php │ └── VersionCommand.php │ ├── Io │ ├── Args.php │ ├── Output.php │ └── OutputEcho.php │ ├── Message │ ├── ArgCommandMessage.php │ ├── CheckMessage.php │ ├── CommonMessage.php │ ├── CopyMessage.php │ ├── DeleteMessage.php │ ├── EditMessage.php │ ├── EnvsMessage.php │ ├── ErrorMessage.php │ ├── ExportMessage.php │ ├── HelpMessage.php │ ├── ImportMessage.php │ ├── NewMessage.php │ ├── RsyncMessage.php │ └── VersionMessage.php │ ├── Model │ ├── Settings.php │ └── Status.php │ ├── Setting │ ├── SettingFile.php │ └── SettingFolder.php │ └── System │ ├── File.php │ ├── Git.php │ ├── OhMyZsh.php │ ├── Rsync.php │ ├── RsyncCommands.php │ ├── System.php │ ├── Zsh.php │ └── Zshrc.php └── tests └── AwsUpload ├── AwsUploadTest.php ├── BaseTestCase.php ├── Command ├── CheckTest.php ├── CopyTest.php ├── DeleteTest.php ├── EditTest.php ├── EnvsTest.php ├── ExportTest.php ├── ImportTest.php ├── KeysTest.php ├── NewTest.php └── ProjsTest.php ├── Io ├── ArgsTest.php └── OutputTest.php ├── Setting ├── SettingFileTest.php └── SettingFolderTest.php └── System ├── OhMyZshTest.php ├── RsyncTest.php └── SystemTest.php /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: http://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | # Unix-style newlines with a newline ending every file 7 | [*] 8 | charset = utf-8 9 | indent_style = space 10 | indent_size = 4 11 | insert_final_newline = true 12 | end_of_line = lf 13 | trim_trailing_whitespace = true 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | /aws_upload/ 3 | /external/ 4 | /build/ 5 | composer.phar 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | php: 3 | - '5.6' 4 | - '7.0' 5 | - '7.1' 6 | 7 | before_script: 8 | - composer update --dev --no-interaction 9 | 10 | script: 11 | - vendor/bin/phpunit -c phpunit.xml.dist 12 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to this project will be documented in this file. 3 | 4 | ## 1.10.0 5 | ## Added 6 | - Add new feature `aws-upload diff` to see files not yet in sync. 7 | - Use `.editorconfig` to have clean files. 8 | 9 | ### Changed 10 | - Refactor System\Rsync to handle multiple commands. 11 | - Improve tests Rsync. 12 | - Improve help section. 13 | 14 | ### Fixed 15 | - Bug when no $EDITOR variable is defined. 16 | - Bug sed error code m. 17 | 18 | ## 1.9.0 - 2017-09-05 19 | ### Added 20 | - Add new feature `aws-upload import` to enable import in `aws-upload`. 21 | - Add new feature `aws-upload export` to enable export in `aws-upload`. 22 | - Add new feature `aws-upload delete` to enable delete in `aws-upload`. 23 | - Add `--dry-run` as alias of `--simulate`. 24 | - Use phpstan in dev. 25 | 26 | ### Changed 27 | - Changed behaviour `autocomplete`. Now it update the zsh plugin as well. 28 | - Allow verbose output on upload. 29 | - Refactor Facilitator to Message. 30 | - Merge Check to SettingFile. 31 | - Improve help section. 32 | 33 | ## 1.8.0 - 2017-07-27 34 | ### Added 35 | - Add new feature `aws-upload autocomplete` to enable autocomplete in `aws-upload`. 36 | - Add `Model` folder. 37 | - Add `System` folder. 38 | 39 | ### Changed 40 | - Changed behaviour of `aws-upload new key`. 41 | - Added `CodeSniffer` to dev. 42 | - Clean up the script section. 43 | 44 | ### Fixed 45 | - Namespace for tests 46 | 47 | ## 1.7.2 - 2017-07-12 48 | ### Changed 49 | - Improve help section 50 | - Improve abstraction 51 | 52 | ### Bugfix 53 | - Fixed issue on new 54 | 55 | ## 1.7.1 - 2017-05-11 56 | ### Added 57 | - Add new feature `aws-upload copy proj.env newproj.dev` to copy a setting file. 58 | 59 | ### Bugfix 60 | - Fixed phpunit names on test. 61 | 62 | ## 1.6.1 - 2017-05-11 63 | ### Added 64 | - Add new feature `aws-upload selfupdate` to self update `aws-upload`. 65 | - Add new feature `aws-upload check proj.env` to check for json syntax, pem permission, and local forlder. 66 | 67 | ## 1.4.1 - 2017-05-09 68 | ### Added 69 | - Add new feature `aws-upload keys` to get projects' keys. 70 | 71 | ## 1.3.1 - 2017-05-05 72 | ### Added 73 | - Add new feature `aws-upload edit proj.env` to edit setting files. 74 | 75 | ### Changed 76 | - Update convention phpunit methods. 77 | - Update way to instantiate a command in `AwsUpload::run`. 78 | 79 | ## 1.2.1 - 2017-04-02 80 | ### Added 81 | - Add new feature `aws-upload new proj.env` to create new project setting. 82 | 83 | ### Changed 84 | - Update convention phpunit methods. 85 | 86 | ## 1.1.2 - 2017-04-03 87 | ### Added 88 | - Output to manage colors, exit, stdout. 89 | 90 | ### Changed 91 | - Now Facilitator contains only che text for each message. 92 | - Cleanup `AwsUpload::run`. 93 | 94 | ## 1.1.1 - 2017-03-30 95 | ### Added 96 | - escapeshellargs 97 | 98 | ### Changed 99 | - Improved `--help`: added description for `--simulate`. 100 | - Improved instruction for `aws-upload-zsh`. 101 | - Improved demo. 102 | 103 | ## 1.1.0 - 2017-03-11 104 | ### Added 105 | - Ability to use double notation or key notation. 106 | - Error handle for cli/Arguments. 107 | 108 | ## 1.0.6 - 2017-03-09 109 | ### Added 110 | - Demo in `README.md`. 111 | - What?, Why?, Why Not?. 112 | - Test for Check. 113 | 114 | ### Fixed 115 | - `Check::fileExists` as static. 116 | - Help output, fixed allign. 117 | 118 | ## 1.0.5 - 2017-03-08 119 | ### Changed 120 | - Updated all the initial doc block in each file. 121 | 122 | ### Fixed 123 | - Fix `SettingFiles::getList()` to exclude folders from the returned array. 124 | - Fix `AwsUpload::getWildArgs()` to avoid collisions with `-q` and `--quiet`. 125 | 126 | ## 1.0.4 - 2017-03-07 127 | ### Fixed 128 | - Fix position of the bin script from bin to root. `mv bin/aws-upload aws-upload`. 129 | - Fix in `aws-upload` the way to import `autoload.php`. 130 | 131 | ## 1.0.3 - 2017-03-07 132 | ### Changed 133 | - Fix issues installation on packagist. 134 | - Version on construct. 135 | - README.md update installation. 136 | 137 | ## 0.1.2 - 2017-03-07 138 | ### Added 139 | - graceExit() for phpunit. 140 | 141 | ### Changed 142 | - Improved tests for AwsUpload, Facilitator, Rsync. 143 | - README.md removed aws-upload.plugin.zsh. 144 | 145 | ## 0.1.0 - 2017-03-06 146 | ### Changed 147 | - README.md removed `aws-upload.plugin.zsh`. 148 | 149 | ### Fixed 150 | - Fixed `phpunit.xml.dist` `.travis.yml`. 151 | 152 | ## 0.0.3 - 2017-03-06 153 | ### Added 154 | - Added `.travis.yml` `phpunit.xml.dist`. 155 | 156 | ## 0.0.2 - 2017-03-06 157 | ### Added 158 | - Added LICENCE file for MIT. 159 | 160 | ### Changed 161 | - Update comments on Rsync. 162 | 163 | ### Removed 164 | - Removed `aws-upload.plugin.zsh` moved in different repo. 165 | 166 | ## 0.0.1 - 2017-03-06 167 | ### Added 168 | - Added `composer.json`. 169 | - Added Facilitator to print `help`, `banner`, `version`. 170 | - Added Rsync to user `rsync`. 171 | - Added SettingFolder to get `getHomeDir`, `getPath`. 172 | -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017 Marco Buttini 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 |

4 | Build Status 5 | Latest Stable Version 6 | Turbo Commit 7 |

8 | 9 |

10 | aws-upload Demo 11 |

12 | 13 | ## What is aws-upload? 14 | aws-upload is cli tool that allows you to rapid upload files in an elegant and efficient way. The main idea is that deploy files should be brain zero. Quick and fast. No thoughts. 15 | aws-upload try to remove the pain from deployment process following three core values: 16 | 17 | - Explore: No need to remember all your projects. Guess and tab. 18 | - Support: It helps you in the configuration process and during the debug. 19 | - Efficient: Save time. Be fast and reliable when it matters the most. 20 | 21 | ## Installation 22 | 23 | composer global require aws-upload/aws-upload 24 | 25 | ## Documentation 26 | To check out examples and docs, visit [aws-upload.com](https://aws-upload.com). 27 | 28 | ## Stay In Touch 29 | For the latest releases and announcements, follow on Twitter: [@borracciaBlu_u](https://twitter.com/borracciaBlu_u) 30 | 31 | ## License 32 | aws-upload is open-sourced software licensed under the [MIT Licence](http://opensource.org/licenses/MIT). 33 | 34 | -------------------------------------------------------------------------------- /aws-upload: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | run(); 14 | exit($status); 15 | 16 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aws-upload/aws-upload", 3 | "description": "aws-upload - A delicious CLI Tool for uploading files to ec2", 4 | "keywords": ["rsync","upload", "aws", "cli", "ec2"], 5 | "homepage": "http://aws-upload.com", 6 | "authors": [ 7 | { 8 | "name": "Marco Buttini", 9 | "email": "marco.asdman@gmail.com" 10 | } 11 | ], 12 | "minimum-stability": "stable", 13 | "license": "MIT", 14 | "type": "library", 15 | "require": { 16 | "php": ">=5.6.4", 17 | "wp-cli/php-cli-tools": "^0.11.2" 18 | }, 19 | "require-dev": { 20 | "symfony/filesystem": "^3.2", 21 | "phpunit/phpunit": "^5.6", 22 | "squizlabs/php_codesniffer": "3.*" 23 | }, 24 | "autoload": { 25 | "psr-4": { 26 | "AwsUpload\\": "src/AwsUpload" 27 | } 28 | }, 29 | "autoload-dev": { 30 | "psr-4": { 31 | "AwsUpload\\Tests\\": "tests/AwsUpload" 32 | } 33 | }, 34 | "scripts": { 35 | "test": "vendor/bin/phpunit -c phpunit.xml.dist", 36 | "cs": "vendor/bin/phpcs --standard=PSR2 src/AwsUpload", 37 | "cbf": "vendor/bin/phpcbf --standard=PSR2 src/AwsUpload" 38 | }, 39 | "bin": ["aws-upload"] 40 | } 41 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | tests/AwsUpload/ 7 | 8 | 9 | 10 | 11 | 12 | src/AwsUpload/ 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/AwsUpload/AwsUpload.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload; 14 | 15 | use AwsUpload\Io\Args; 16 | use AwsUpload\Io\Output; 17 | 18 | class AwsUpload 19 | { 20 | /** 21 | * Application version. 22 | * Given a version number MAJOR.MINOR.PATCH, increment the: 23 | * 24 | * MAJOR version when you make incompatible API changes, 25 | * MINOR version when you add functionality in a backwards-compatible manner, and 26 | * PATCH version when you make backwards-compatible bug fixes. 27 | * 28 | * @see http://semver.org/ 29 | * @var string VERSION 30 | */ 31 | public $version; 32 | 33 | /** 34 | * Zsh plugin version. 35 | * Given a version number MAJOR.MINOR.PATCH, increment the: 36 | * 37 | * MAJOR version when you make incompatible API changes, 38 | * MINOR version when you add functionality in a backwards-compatible manner, and 39 | * PATCH version when you make backwards-compatible bug fixes. 40 | * 41 | * @see http://semver.org/ 42 | * @var string VERSION 43 | */ 44 | public $plugin; 45 | 46 | /** 47 | * It define if aws-upload has to print additional info. 48 | * 49 | * @var bool 50 | */ 51 | public $is_verbose = false; 52 | 53 | /** 54 | * It define if aws-upload has to stay quiet and do not print additional information. 55 | * 56 | * @var bool 57 | */ 58 | public $is_quiet = false; 59 | 60 | /** 61 | * It containst to arguments passed to the shell script. 62 | * 63 | * @var \AwsUpload\Io\Args 64 | */ 65 | public $args; 66 | 67 | /** 68 | * It containst output class to manage the script output. 69 | * 70 | * @var Output 71 | */ 72 | protected $out; 73 | 74 | /** 75 | * Initializes the command. 76 | * 77 | * The main purpose is to define the args for the script 78 | * and populate `$this->args`. 79 | */ 80 | public function __construct($version = 'test', $plugin = 'test') 81 | { 82 | 83 | $this->version = $version; 84 | $this->plugin = $plugin; 85 | 86 | $args = new Args(); 87 | $args->addFlags(array( 88 | 'quiet' => array('quiet', 'q'), 89 | 'verbose' => array('verbose', 'v'), 90 | 'simulate' => array('simulate', 'dry-run') 91 | )); 92 | $args->addCmds(array( 93 | 'new' => array('new', 'n'), 94 | 'envs' => array('envs', 'e'), 95 | 'edit' => array('edit', 'E'), 96 | 'diff' => array('diff', 'df'), 97 | 'delete' => array('delete', 'rm'), 98 | 'copy' => array('copy', 'cp'), 99 | 'help' => array('help', 'h'), 100 | 'keys' => array('keys', 'k'), 101 | 'projs' => array('projs', 'p'), 102 | 'check' => array('check', 'c'), 103 | 'import' => array('import', 'i'), 104 | 'export' => array('export', 'ex'), 105 | 'version' => array('version', 'V'), 106 | 'selfupdate' => array('self-update', 'selfupdate'), 107 | 'autocomplete' => array('autocomplete'), 108 | )); 109 | $args->parse(); 110 | 111 | $this->args = $args; 112 | $this->out = new Output(); 113 | } 114 | 115 | /** 116 | * Method to run the aws-upload cmd. 117 | * 118 | * @return int The status code. 119 | */ 120 | public function run() 121 | { 122 | if ($this->args->verbose) { 123 | $this->is_verbose = true; 124 | } 125 | 126 | if ($this->args->quiet) { 127 | $this->is_quiet = true; 128 | } 129 | 130 | $cmdName = $this->getCmdName(); 131 | $cmd = new $cmdName($this); 132 | 133 | return $cmd->run(); 134 | } 135 | 136 | /** 137 | * Method to set different output. 138 | * 139 | * @return void 140 | */ 141 | public function setOutput(\AwsUpload\Io\Output $output) 142 | { 143 | $this->out = $output; 144 | } 145 | 146 | /** 147 | * Method to decide which cmd to run. 148 | * 149 | * @return string 150 | */ 151 | public function getCmdName() 152 | { 153 | $cmd = $this->fetchArgsCmd(); 154 | 155 | if (empty($cmd)) { 156 | $cmd = 'AwsUpload\Command\FullInfo'; 157 | 158 | if ($this->args->wild) { 159 | $cmd = 'AwsUpload\Command\Upload'; 160 | } 161 | } 162 | 163 | return $cmd . 'Command'; 164 | } 165 | 166 | /** 167 | * Method to check cmd to run against list of cmds with arguments. 168 | * 169 | * @return string 170 | */ 171 | public function fetchArgsCmd() 172 | { 173 | $cmd = ''; 174 | $cmdList = array( 175 | 'help' => 'AwsUpload\Command\Help', 176 | 'version' => 'AwsUpload\Command\Version', 177 | 'keys' => 'AwsUpload\Command\Keys', 178 | 'projs' => 'AwsUpload\Command\Projs', 179 | 'envs' => 'AwsUpload\Command\Envs', 180 | 'new' => 'AwsUpload\Command\New', 181 | 'diff' => 'AwsUpload\Command\Diff', 182 | 'edit' => 'AwsUpload\Command\Edit', 183 | 'copy' => 'AwsUpload\Command\Copy', 184 | 'delete' => 'AwsUpload\Command\Delete', 185 | 'check' => 'AwsUpload\Command\Check', 186 | 'import' => 'AwsUpload\Command\Import', 187 | 'export' => 'AwsUpload\Command\Export', 188 | 'selfupdate' => 'AwsUpload\Command\SelfUpdate', 189 | 'autocomplete' => 'AwsUpload\Command\AutoComplete', 190 | ); 191 | foreach ($cmdList as $arg => $cmdName) { 192 | if ($this->args->{$arg} && empty($cmd)) { 193 | $cmd = $cmdName; 194 | } 195 | } 196 | 197 | return $cmd; 198 | } 199 | 200 | /** 201 | * Method to wrap render and graceExit. 202 | * 203 | * The main idea is to setup the system to print and be ready 204 | * for phpunit. 205 | * 206 | * @param string|null $msg 207 | * 208 | * @return void 209 | */ 210 | public function inline($msg) 211 | { 212 | $this->out->render($msg . "\n"); 213 | } 214 | 215 | /** 216 | * Method used to print additional text with the flag verbose. 217 | * 218 | * @param string $msg The text to print in verbose state 219 | * 220 | * @return void 221 | */ 222 | public function verbose($msg) 223 | { 224 | if ($this->is_verbose) { 225 | $this->inline($msg . "\n\n"); 226 | } 227 | } 228 | } 229 | -------------------------------------------------------------------------------- /src/AwsUpload/Command/AutoCompleteCommand.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Command; 14 | 15 | use AwsUpload\System\Git; 16 | use AwsUpload\System\Zsh; 17 | use AwsUpload\Model\Status; 18 | use AwsUpload\System\OhMyZsh; 19 | use AwsUpload\Message\CommonMessage; 20 | 21 | class AutoCompleteCommand extends BasicCommand 22 | { 23 | /** 24 | * Method used to install the autocomplete plugin. 25 | * 26 | * @return int The status code. 27 | */ 28 | public function run() 29 | { 30 | $this->app->inline('Checking System:'); 31 | 32 | $this->printSystemStatus(); 33 | 34 | if (!$this->isValidSystem()) { 35 | $this->app->inline($this->error_msg); 36 | return Status::SYSTEM_NOT_READY; 37 | } 38 | 39 | $this->app->inline("Procede to the installation:\n"); 40 | 41 | if (!OhMyZsh::hasPluginFiles()) { 42 | $this->app->inline(' Download plugin'); 43 | $this->clone(); 44 | } else { 45 | $this->app->inline(' Plugin files already present.'); 46 | $this->pull(); 47 | } 48 | 49 | if (!OhMyZsh::isPluginActive()) { 50 | $this->app->inline(' Activating plugin'); 51 | OhMyZsh::activate(); 52 | } else { 53 | $this->app->inline(' Plugin already activated'); 54 | } 55 | 56 | $this->app->inline("\nProcedure complete. Please reload the shell."); 57 | 58 | system('zsh'); 59 | 60 | return Status::SUCCESS; 61 | } 62 | 63 | 64 | /** 65 | * Method to check if the system is good to proceed. 66 | * 67 | * @return boolean 68 | */ 69 | public function isValidSystem() 70 | { 71 | $tests = array( 72 | "no_git" => Git::isInstalled(), 73 | "no_zsh" => Zsh::isInstalled(), 74 | "no_omz" => OhMyZsh::isInstalled(), 75 | ); 76 | 77 | $msgs = array( 78 | "no_git" => Git::errorMsg(), 79 | "no_zsh" => Zsh::errorMsg(), 80 | "no_omz" => OhMyZsh::errorMsg(), 81 | ); 82 | 83 | $valid = $this->validate($tests, $msgs); 84 | 85 | return $valid; 86 | } 87 | 88 | /** 89 | * Print the system status report. 90 | * 91 | * @return void 92 | */ 93 | public function printSystemStatus() 94 | { 95 | $check = array('✔', '✖'); 96 | $labels = array('INSTALLED', 'NOT INSTALLED'); 97 | 98 | $git = Git::isInstalled(); 99 | $zsh = Zsh::isInstalled(); 100 | $omz = OhMyZsh::isInstalled(); 101 | 102 | $git_line = " " . 103 | CommonMessage::plot($git, $check) . 104 | " Git \t" . 105 | CommonMessage::plot($git, $labels); 106 | $zsh_line = " " . 107 | CommonMessage::plot($zsh, $check) . 108 | " Zsh \t" . 109 | CommonMessage::plot($zsh, $labels); 110 | $omz_line = " " . 111 | CommonMessage::plot($omz, $check) . 112 | " Oh-my-zsh \t" . 113 | CommonMessage::plot($omz, $labels); 114 | 115 | $this->app->inline(""); 116 | $this->app->inline($git_line); 117 | $this->app->inline($zsh_line); 118 | $this->app->inline($omz_line); 119 | $this->app->inline(""); 120 | } 121 | 122 | /** 123 | * Clone the aws-upload-plugin repository. 124 | * 125 | * @return void 126 | */ 127 | public function clone() 128 | { 129 | $dest = OhMyZsh::getPath() . '/plugins/aws-upload/'; 130 | $repo = '--branch v' . $this->app->plugin . 131 | ' https://github.com/borracciaBlu/aws-upload-zsh.git'; 132 | Git::clone($repo, $dest); 133 | } 134 | 135 | public function pull() 136 | { 137 | $repo = OhMyZsh::getPath() . '/plugins/aws-upload/'; 138 | 139 | $this->app->inline(' - Update repo'); 140 | Git::pull($repo); 141 | 142 | $this->app->inline(' - Checkout version v' . $this->app->plugin); 143 | Git::checkoutTag($repo, 'v' . $this->app->plugin); 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /src/AwsUpload/Command/BasicCommand.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Command; 14 | 15 | use AwsUpload\Model\Status; 16 | 17 | abstract class BasicCommand implements Command 18 | { 19 | /** 20 | * The AwsUpload object 21 | */ 22 | public $app; 23 | 24 | /** 25 | * The success messsage. 26 | * 27 | * @var string 28 | */ 29 | public $msg; 30 | 31 | /** 32 | * The error message. 33 | * 34 | * @var string 35 | */ 36 | public $error_msg; 37 | 38 | /** 39 | * Initializes the command. 40 | * 41 | * The main purpose is to define the app for the script 42 | * and populate `$this->app`. 43 | */ 44 | public function __construct($app) 45 | { 46 | $this->app = $app; 47 | } 48 | 49 | /** 50 | * Method to check to set the error msg. 51 | * 52 | * @param array $tests The conditions checked. 53 | * @param array $msgs The msgs for each test condition. 54 | * 55 | * @return boolean 56 | */ 57 | public function validate($tests, $msgs) 58 | { 59 | $valid = true; 60 | 61 | foreach ($tests as $test_key => $evaluation) { 62 | if (!$evaluation) { 63 | $this->error_msg = $msgs[$test_key]; 64 | $valid = false; 65 | } 66 | } 67 | 68 | return $valid; 69 | } 70 | 71 | /** 72 | * Method to handle an error case. 73 | * 74 | * @return int The status code. 75 | */ 76 | public function handleError() 77 | { 78 | $this->app->inline($this->error_msg); 79 | 80 | return Status::ERROR_INVALID; 81 | } 82 | 83 | /** 84 | * Method to handle a success case. 85 | * 86 | * @return int The status code. 87 | */ 88 | public function handleSuccess() 89 | { 90 | $this->app->inline($this->msg); 91 | 92 | return Status::SUCCESS; 93 | } 94 | 95 | abstract public function run(); 96 | } 97 | -------------------------------------------------------------------------------- /src/AwsUpload/Command/CheckCommand.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Command; 14 | 15 | use AwsUpload\Message\CheckMessage; 16 | use AwsUpload\Setting\SettingFile; 17 | 18 | class CheckCommand extends FileCommand 19 | { 20 | /** 21 | * Initializes the command. 22 | */ 23 | public function init() 24 | { 25 | $this->key = $this->app->args->getFirst('check'); 26 | } 27 | 28 | /** 29 | * Exec the check on the setting file. 30 | * 31 | * @see FileCommand::run 32 | * @return void 33 | */ 34 | public function exec() 35 | { 36 | $report = $this->getReport(); 37 | $this->msg = CheckMessage::report($report); 38 | } 39 | 40 | public function getReport() 41 | { 42 | $path = SettingFile::getPath($this->key); 43 | $settings = SettingFile::getObject($this->key); 44 | 45 | $pem_exists = file_exists($settings->pem); 46 | $pem_perms = ($pem_exists) ? decoct(fileperms($settings->pem) & 0777) : '-'; 47 | $is_400 = ($pem_perms === '400'); 48 | $clean_local = str_replace('*', '', $settings->local); 49 | $local_exists = file_exists($clean_local); 50 | 51 | $report = array( 52 | "path" => $path, 53 | "is_valid_json" => $settings->is_valid_json, 54 | "error_json" => $settings->error_json, 55 | "pem" => $settings->pem, 56 | "pem_exists" => $pem_exists, 57 | "pem_perms" => $pem_perms, 58 | "is_400" => $is_400, 59 | "local" => $settings->local, 60 | "local_exists" => $local_exists, 61 | ); 62 | 63 | return $report; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/AwsUpload/Command/Command.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Command; 14 | 15 | interface Command 16 | { 17 | public function run(); 18 | } 19 | -------------------------------------------------------------------------------- /src/AwsUpload/Command/CopyCommand.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Command; 14 | 15 | use AwsUpload\Message\NewMessage; 16 | use AwsUpload\Message\CopyMessage; 17 | use AwsUpload\Message\ErrorMessage; 18 | use AwsUpload\Setting\SettingFile; 19 | 20 | class CopyCommand extends FileCommand 21 | { 22 | /** 23 | * @var array 24 | */ 25 | public $keys; 26 | 27 | /** 28 | * @see FileCommand::init 29 | * @return void 30 | */ 31 | public function init() 32 | { 33 | $this->keys = $this->app->args->getParams('copy'); 34 | } 35 | 36 | /** 37 | * Exec the copy files. 38 | * 39 | * @see FileCommand::run 40 | * @return void 41 | */ 42 | public function exec() 43 | { 44 | list($source, $dest) = $this->keys; 45 | 46 | SettingFile::copy($source, $dest); 47 | $this->msg = NewMessage::success($dest); 48 | } 49 | 50 | /** 51 | * Method to check if keys isValid and good to proceed. 52 | * 53 | * @return boolean 54 | */ 55 | public function isValid() 56 | { 57 | if (!$this->isValidArgs($this->keys)) { 58 | $this->error_msg = CopyMessage::noArgs(); 59 | $valid = false; 60 | 61 | return $valid; 62 | } 63 | 64 | list($source, $dest) = $this->keys; 65 | 66 | $tests = array( 67 | "dest_not_exists" => !SettingFile::exists($dest), 68 | "src_exists" => SettingFile::exists($source), 69 | "is_valid_key_src" => SettingFile::isValidKey($source), 70 | "is_valid_key_dst" => SettingFile::isValidKey($dest), 71 | ); 72 | 73 | $msgs = array( 74 | "dest_not_exists" => ErrorMessage::keyAlreadyExists($dest), 75 | "src_exists" => ErrorMessage::noFileFound($source), 76 | "is_valid_key_src" => ErrorMessage::noValidKey($source), 77 | "is_valid_key_dst" => ErrorMessage::noValidKey($dest), 78 | ); 79 | 80 | $valid = $this->validate($tests, $msgs); 81 | 82 | return $valid; 83 | } 84 | 85 | /** 86 | * Method to check the validity of the copy arguments 87 | * 88 | * @param array $keys The copy arguments. 89 | * 90 | * @return boolean 91 | */ 92 | public function isValidArgs($keys) 93 | { 94 | return (empty($keys) || count($keys) < 2) ? false : true; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/AwsUpload/Command/DeleteCommand.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Command; 14 | 15 | use AwsUpload\Setting\SettingFile; 16 | use AwsUpload\Message\DeleteMessage; 17 | 18 | class DeleteCommand extends FileCommand 19 | { 20 | 21 | /** 22 | * @see FileCommand::init 23 | * @return void 24 | */ 25 | public function init() 26 | { 27 | $this->key = $this->app->args->getFirst('delete'); 28 | } 29 | 30 | /** 31 | * Method used to delete a setting file. 32 | * 33 | * @see FileCommand::run 34 | * @return void 35 | */ 36 | public function exec() 37 | { 38 | $line = $this->getConfirmation(); 39 | if (! $this->isYes($line)) { 40 | $this->app->inline("Aborting delete operation"); 41 | return; 42 | } 43 | 44 | SettingFile::delete($this->key); 45 | $this->msg = DeleteMessage::success($this->key); 46 | } 47 | 48 | public function getConfirmation() 49 | { 50 | $this->app->inline("Are you sure you want delete " . $this->key . "?(y|n)"); 51 | 52 | $handle = fopen("php://stdin", "r"); 53 | $line = fgets($handle); 54 | fclose($handle); 55 | 56 | return $line; 57 | } 58 | 59 | public function isYes($line) 60 | { 61 | $line = trim($line); 62 | $line = strtolower($line); 63 | 64 | return ($line === 'yes' || $line === 'y'); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/AwsUpload/Command/DiffCommand.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Command; 14 | 15 | use AwsUpload\Model\Status; 16 | use AwsUpload\System\Rsync; 17 | use AwsUpload\Setting\SettingFile; 18 | use AwsUpload\Message\RsyncMessage; 19 | use AwsUpload\System\RsyncCommands; 20 | 21 | class DiffCommand extends FileCommand 22 | { 23 | /** 24 | * Property true if app is simulate. 25 | * 26 | * @var bool 27 | */ 28 | public $is_simulate; 29 | 30 | /** 31 | * Property true if app is verbose. 32 | * 33 | * @var bool 34 | */ 35 | public $is_verbose; 36 | 37 | /** 38 | * @var string 39 | */ 40 | public $key; 41 | 42 | /** 43 | * @var string 44 | */ 45 | public $proj; 46 | 47 | /** 48 | * @var string 49 | */ 50 | public $env; 51 | 52 | public function init() 53 | { 54 | $this->key = $this->app->args->getFirst('diff'); 55 | $this->is_verbose = $this->app->args->verbose; 56 | $this->is_simulate = $this->app->args->simulate; 57 | 58 | list($proj, $env) = explode('.', $this->key); 59 | $this->proj = $proj; 60 | $this->env = $env; 61 | } 62 | 63 | /** 64 | * Method to run the rsync cmd. 65 | * 66 | * @return mixed The status code. 67 | */ 68 | public function exec() 69 | { 70 | $settings = SettingFile::getObject($this->key); 71 | 72 | $rsync = new Rsync($settings); 73 | $rsync->setVerbose($this->is_verbose); 74 | $rsync->setAction(RsyncCommands::DIFF); 75 | 76 | $msg = RsyncMessage::banner($this->proj, $this->env, $rsync->cmd); 77 | $this->app->inline($msg); 78 | 79 | if ($this->is_simulate) { 80 | $this->app->inline($rsync->getCmd()); 81 | return $this->simulate(); 82 | } 83 | 84 | $rsync->run(); 85 | } 86 | 87 | public function simulate() 88 | { 89 | $this->msg = 'Simulation mode' . "\n"; 90 | 91 | return $this->handleSuccess(); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/AwsUpload/Command/EditCommand.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Command; 14 | 15 | use AwsUpload\Message\EditMessage; 16 | use AwsUpload\Setting\SettingFile; 17 | 18 | class EditCommand extends FileCommand 19 | { 20 | 21 | /** 22 | * @see FileCommand::init 23 | * @return void 24 | */ 25 | public function init() 26 | { 27 | $this->key = $this->app->args->getFirst('edit'); 28 | } 29 | 30 | /** 31 | * Method used to edit a setting file. 32 | * 33 | * @see FileCommand::run 34 | * @return void 35 | */ 36 | public function exec() 37 | { 38 | SettingFile::edit($this->key); 39 | 40 | $this->msg = EditMessage::success($this->key); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/AwsUpload/Command/EnvsCommand.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Command; 14 | 15 | use AwsUpload\Model\Status; 16 | use AwsUpload\Command\Command; 17 | use AwsUpload\Message\EnvsMessage; 18 | use AwsUpload\Message\ErrorMessage; 19 | use AwsUpload\Setting\SettingFile; 20 | 21 | class EnvsCommand extends BasicCommand implements ValidCommand 22 | { 23 | /** 24 | * The project to use as filter. 25 | * 26 | * @var string 27 | */ 28 | public $proj; 29 | 30 | /** 31 | * Method used to print the environments available for a project. 32 | * 33 | * The main idea is that for each project you have different envs. 34 | * Eg: 35 | * - proj.dev.json -> env: dev 36 | * - proj.stagin.json -> env: staging 37 | * - proj.prod.json -> env: prod 38 | * 39 | * @return int The status code. 40 | */ 41 | public function run() 42 | { 43 | $quiet = $this->app->is_quiet; 44 | $this->proj = $this->app->args->getFirst('envs'); 45 | 46 | if (!$this->isValid() && !$quiet) { 47 | return $this->handleError(); 48 | } 49 | 50 | $envs = SettingFile::getEnvs($this->proj); 51 | $envs = implode(' ', $envs); 52 | $this->msg = $envs . "\n"; 53 | 54 | return $this->handleSuccess(); 55 | } 56 | 57 | /** 58 | * Method to check if key isValid and good to proceed. 59 | * 60 | * @return boolean 61 | */ 62 | public function isValid() 63 | { 64 | $projs = SettingFile::getProjs(); 65 | $envs = SettingFile::getEnvs($this->proj); 66 | 67 | $tests = array( 68 | "is_env" => count($envs) > 0, 69 | "is_project" => count($projs) > 0, 70 | ); 71 | 72 | $msgs = array( 73 | "is_env" => EnvsMessage::errorNoEnvsProj($this->proj), 74 | "is_project" => ErrorMessage::noProjects(), 75 | ); 76 | 77 | $valid = $this->validate($tests, $msgs); 78 | 79 | return $valid; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/AwsUpload/Command/ExportCommand.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Command; 14 | 15 | use AwsUpload\System\File; 16 | use AwsUpload\Model\Settings; 17 | use AwsUpload\Setting\SettingFile; 18 | use AwsUpload\Message\ErrorMessage; 19 | use AwsUpload\Message\ExportMessage; 20 | use AwsUpload\Setting\SettingFolder; 21 | 22 | class ExportCommand extends FileCommand 23 | { 24 | /** 25 | * @var array 26 | */ 27 | public $keys; 28 | 29 | /** 30 | * Initializes the command. 31 | * 32 | * @see FileCommand::init 33 | * @return void 34 | */ 35 | public function init() 36 | { 37 | $this->key = $this->app->args->getFirst('export'); 38 | $this->keys = $this->app->args->getParams('export'); 39 | } 40 | 41 | /** 42 | * Exec the export. 43 | * 44 | * @see FileCommand::run 45 | * @return void 46 | */ 47 | public function exec() 48 | { 49 | $oldPath = $this->getOldPath(); 50 | $newPath = $this->getNewPath(); 51 | 52 | File::copy($oldPath, $newPath); 53 | 54 | $this->msg = ExportMessage::success($this->key); 55 | } 56 | 57 | public function getOldPath() 58 | { 59 | $path = SettingFolder::getPath(); 60 | return $path . '/' . $this->key . '.json'; 61 | } 62 | 63 | public function getNewPath() 64 | { 65 | $path = $this->getNewDir(); 66 | return $path . '/' . $this->key . '.json'; 67 | } 68 | 69 | public function getNewDir() 70 | { 71 | $dir = getcwd(); 72 | if (count($this->keys) === 2) { 73 | if (is_dir($this->keys[1])) { 74 | $dir = realpath($this->keys[1]); 75 | } else { 76 | $dir = dirname($this->keys[1]); 77 | } 78 | } 79 | 80 | return $dir; 81 | } 82 | 83 | /** 84 | * Method to check if key isValid and good to proceed. 85 | * 86 | * @return boolean 87 | */ 88 | public function isValid() 89 | { 90 | $new_settings = new Settings($this->getNewPath()); 91 | 92 | $tests = array( 93 | "dest_not_exists" => !$new_settings->fileExists(), 94 | "src_exists" => SettingFile::exists($this->key), 95 | "is_valid_key" => SettingFile::isValidKey($this->key), 96 | "no_args" => count($this->keys) > 0, 97 | ); 98 | 99 | $msgs = array( 100 | "is_valid_key" => ErrorMessage::noValidKey($this->key), 101 | "src_exists" => ErrorMessage::noFileFound($this->key), 102 | "dest_not_exists" => 'file alreay present', 103 | "no_args" => ExportMessage::noArgs(), 104 | ); 105 | 106 | $valid = $this->validate($tests, $msgs); 107 | 108 | return $valid; 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /src/AwsUpload/Command/FileCommand.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Command; 14 | 15 | use AwsUpload\Setting\SettingFile; 16 | use AwsUpload\Message\ErrorMessage; 17 | 18 | abstract class FileCommand extends BasicCommand implements Command, ValidCommand 19 | { 20 | /** 21 | * @var string 22 | */ 23 | public $key; 24 | 25 | public function __construct($app) 26 | { 27 | parent::__construct($app); 28 | $this->init(); 29 | } 30 | 31 | /** 32 | * Initializes the props. 33 | */ 34 | abstract public function init(); 35 | 36 | /** 37 | * Execute the main operations. 38 | */ 39 | abstract public function exec(); 40 | 41 | /** 42 | * Run the command. 43 | * 44 | * @return int The status code. 45 | */ 46 | public function run() 47 | { 48 | if (!$this->isValid()) { 49 | return $this->handleError(); 50 | } 51 | 52 | $res = $this->exec(); 53 | if (!is_null($res)) { 54 | return $res; 55 | } 56 | 57 | return $this->handleSuccess(); 58 | } 59 | 60 | public function hasArgs() 61 | { 62 | return !empty($this->key); 63 | } 64 | 65 | public function getErrorMsg() 66 | { 67 | $class = str_replace('Command', 'Message', static::class); 68 | $msg = ErrorMessage::noArgs(); 69 | 70 | if (class_exists($class)) { 71 | $msg = call_user_func(array($class, 'noArgs')); 72 | } 73 | 74 | return $msg; 75 | } 76 | 77 | /** 78 | * Method to check if key isValid and good to proceed. 79 | * 80 | * @return boolean 81 | */ 82 | public function isValid() 83 | { 84 | $tests = array( 85 | "file_exists" => SettingFile::exists($this->key), 86 | "is_valid_key" => SettingFile::isValidKey($this->key), 87 | "has_args" => $this->hasArgs(), 88 | ); 89 | 90 | $msgs = array( 91 | "file_exists" => ErrorMessage::noFileFound($this->key), 92 | "is_valid_key" => ErrorMessage::noValidKey($this->key), 93 | "has_args" => $this->getErrorMsg(), 94 | ); 95 | 96 | $valid = $this->validate($tests, $msgs); 97 | 98 | return $valid; 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/AwsUpload/Command/FullInfoCommand.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Command; 14 | 15 | use AwsUpload\Facilitator; 16 | use AwsUpload\Message\HelpMessage; 17 | use AwsUpload\Message\CommonMessage; 18 | use AwsUpload\Message\VersionMessage; 19 | 20 | class FullInfoCommand extends BasicCommand 21 | { 22 | /** 23 | * Method used to print the full aws-upload info. 24 | * 25 | * - banner 26 | * - version 27 | * - help 28 | * 29 | * @return int The status code. 30 | */ 31 | public function run() 32 | { 33 | $msg = CommonMessage::banner(); 34 | $msg .= VersionMessage::success($this->app->version); 35 | $msg .= HelpMessage::success(); 36 | $this->msg = $msg; 37 | 38 | return $this->handleSuccess(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/AwsUpload/Command/HelpCommand.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Command; 14 | 15 | use AwsUpload\Message\HelpMessage; 16 | 17 | class HelpCommand extends BasicCommand 18 | { 19 | /** 20 | * Method used to print the help. 21 | * 22 | * @return int The status code. 23 | */ 24 | public function run() 25 | { 26 | $this->msg = HelpMessage::success(); 27 | 28 | return $this->handleSuccess(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/AwsUpload/Command/ImportCommand.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Command; 14 | 15 | use AwsUpload\System\File; 16 | use AwsUpload\Model\Settings; 17 | use AwsUpload\Setting\SettingFile; 18 | use AwsUpload\Message\ErrorMessage; 19 | use AwsUpload\Message\ImportMessage; 20 | use AwsUpload\Setting\SettingFolder; 21 | 22 | class ImportCommand extends FileCommand 23 | { 24 | /** 25 | * @var string 26 | */ 27 | public $setting_path; 28 | 29 | /** 30 | * @var string 31 | */ 32 | public $new_key; 33 | 34 | /** 35 | * Initializes the command. 36 | * 37 | * @see FileCommand::init 38 | * @return void 39 | */ 40 | public function init() 41 | { 42 | $this->setting_path = $this->app->args->getFirst('import'); 43 | $this->new_key = basename($this->setting_path, ".json"); 44 | } 45 | 46 | /** 47 | * Exec the import. 48 | * 49 | * @see FileCommand::run 50 | * @return void 51 | */ 52 | public function exec() 53 | { 54 | $oldPath = $this->setting_path; 55 | $newPath = $this->getNewPath(); 56 | File::copy($oldPath, $newPath); 57 | 58 | $this->msg = ImportMessage::success($this->new_key); 59 | } 60 | 61 | public function getNewPath() 62 | { 63 | $path = SettingFolder::getPath(); 64 | return $path . '/' . $this->new_key . '.json'; 65 | } 66 | 67 | /** 68 | * Method to check if key isValid and good to proceed. 69 | * 70 | * @return boolean 71 | */ 72 | public function isValid() 73 | { 74 | $new_settings = new Settings($this->setting_path); 75 | 76 | $tests = array( 77 | "is_valid_key" => SettingFile::isValidKey($this->new_key), 78 | "dest_not_exists" => !SettingFile::exists($this->new_key), 79 | "src_exists" => $new_settings->fileExists(), 80 | "has_arg" => !empty($this->setting_path), 81 | ); 82 | 83 | $msgs = array( 84 | "dest_not_exists" => ErrorMessage::keyAlreadyExists($this->new_key), 85 | "is_valid_key" => ErrorMessage::noValidKey($this->new_key), 86 | "src_exists" => ImportMessage::errorNotFound($this->setting_path), 87 | "has_arg" => ImportMessage::noArgs(), 88 | ); 89 | 90 | $valid = $this->validate($tests, $msgs); 91 | 92 | return $valid; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/AwsUpload/Command/KeysCommand.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Command; 14 | 15 | use AwsUpload\Message\ErrorMessage; 16 | use AwsUpload\Setting\SettingFile; 17 | 18 | class KeysCommand extends BasicCommand implements ValidCommand 19 | { 20 | /** 21 | * @var array 22 | */ 23 | public $keys; 24 | 25 | /** 26 | * Property true if app is quiet. 27 | * 28 | * @var bool 29 | */ 30 | public $is_quiet; 31 | 32 | /** 33 | * Method used to print the projects' keys available. 34 | * 35 | * The main idea is that you can get the projects' keys from the files in 36 | * the aws-upload home folder. 37 | * Eg: 38 | * - proj-1.dev.json -> key: proj-1.dev 39 | * - proj-1.stagin.json -> key: proj-1.staging 40 | * - proj-2.prod.json -> key: proj-2.prod 41 | * 42 | * @return int The status code. 43 | */ 44 | public function run() 45 | { 46 | $this->keys = SettingFile::getKeys(); 47 | $this->is_quiet = $this->app->is_quiet; 48 | 49 | if (!$this->isValid() && !$this->is_quiet) { 50 | return $this->handleError(); 51 | } 52 | 53 | $keys = implode(' ', $this->keys); 54 | $this->msg = $keys . "\n"; 55 | 56 | return $this->handleSuccess(); 57 | } 58 | 59 | /** 60 | * Method to check if key isValid and good to proceed. 61 | * 62 | * @return boolean 63 | */ 64 | public function isValid() 65 | { 66 | $tests = array( 67 | "is_project" => (bool) (count($this->keys) > 0), 68 | ); 69 | 70 | $msgs = array( 71 | "is_project" => ErrorMessage::noProjects(), 72 | ); 73 | 74 | $valid = $this->validate($tests, $msgs); 75 | 76 | return $valid; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/AwsUpload/Command/NewCommand.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Command; 14 | 15 | use AwsUpload\Model\Status; 16 | use AwsUpload\Command\Command; 17 | use AwsUpload\Message\NewMessage; 18 | use AwsUpload\Setting\SettingFile; 19 | use AwsUpload\Message\ErrorMessage; 20 | 21 | class NewCommand extends FileCommand 22 | { 23 | public function init() 24 | { 25 | $this->key = $this->app->args->getFirst('new'); 26 | } 27 | 28 | /** 29 | * Method used tocreate a new setting file. 30 | * 31 | * @return int The status code. 32 | */ 33 | public function exec() 34 | { 35 | SettingFile::create($this->key); 36 | SettingFile::edit($this->key); 37 | 38 | $this->msg = NewMessage::success($this->key); 39 | } 40 | 41 | /** 42 | * Method to check if key isValid and good to proceed. 43 | * 44 | * @return boolean 45 | */ 46 | public function isValid() 47 | { 48 | $tests = array( 49 | "file_not_exists" => !SettingFile::exists($this->key), 50 | "is_valid_key" => SettingFile::isValidKey($this->key), 51 | "has_args" => $this->hasArgs(), 52 | ); 53 | 54 | $msgs = array( 55 | "file_not_exists" => ErrorMessage::keyAlreadyExists($this->key), 56 | "is_valid_key" => ErrorMessage::noValidKey($this->key), 57 | "has_args" => $this->getErrorMsg(), 58 | ); 59 | 60 | $valid = $this->validate($tests, $msgs); 61 | 62 | return $valid; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/AwsUpload/Command/ProjsCommand.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Command; 14 | 15 | use AwsUpload\Model\Status; 16 | use AwsUpload\Command\Command; 17 | use AwsUpload\Message\ErrorMessage; 18 | use AwsUpload\Setting\SettingFile; 19 | 20 | class ProjsCommand extends BasicCommand implements ValidCommand 21 | { 22 | /** 23 | * It contains the projects label, if any. 24 | * 25 | * @var array 26 | */ 27 | public $projs; 28 | 29 | /** 30 | * Method used to print the projects available. 31 | * 32 | * The main idea is that you can get the projects from the files in 33 | * the aws-upload home folder. 34 | * Eg: 35 | * - proj-1.dev.json -> proj: proj-1 36 | * - proj-1.stagin.json -> proj: proj-1 37 | * - proj-2.prod.json -> proj: proj-2 38 | * 39 | * @return int The status code. 40 | */ 41 | public function run() 42 | { 43 | $quiet = $this->app->is_quiet; 44 | $this->projs = SettingFile::getProjs(); 45 | 46 | if (!$this->isValid() && !$quiet) { 47 | return $this->handleError(); 48 | } 49 | 50 | $projs = implode(' ', $this->projs); 51 | $this->msg = $projs . "\n"; 52 | 53 | return $this->handleSuccess(); 54 | } 55 | 56 | /** 57 | * Method to check if key isValid and good to proceed. 58 | * 59 | * @return boolean 60 | */ 61 | public function isValid() 62 | { 63 | $tests = array( 64 | "is_project" => (count($this->projs) > 0), 65 | ); 66 | $msgs = array( 67 | "is_project" => ErrorMessage::noProjects(), 68 | ); 69 | 70 | $valid = $this->validate($tests, $msgs); 71 | 72 | return $valid; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/AwsUpload/Command/SelfUpdateCommand.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Command; 14 | 15 | use AwsUpload\Model\Status; 16 | use AwsUpload\System\OhMyZsh; 17 | 18 | class SelfUpdateCommand extends BasicCommand 19 | { 20 | /** 21 | * Method used to update aws-upload via composer. 22 | * 23 | * @return int The status code. 24 | */ 25 | public function run() 26 | { 27 | $this->app->inline('Self-update running..'); 28 | system('composer -vvv global require aws-upload/aws-upload'); 29 | $this->app->inline("Self-update completed"); 30 | 31 | if (OhMyZsh::isPluginActive()) { 32 | system('aws-upload autocomplete'); 33 | } 34 | 35 | return Status::SUCCESS; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/AwsUpload/Command/UploadCommand.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Command; 14 | 15 | use AwsUpload\Model\Status; 16 | use AwsUpload\System\Rsync; 17 | use AwsUpload\Setting\SettingFile; 18 | use AwsUpload\Message\RsyncMessage; 19 | use AwsUpload\System\RsyncCommands; 20 | 21 | class UploadCommand extends FileCommand 22 | { 23 | /** 24 | * Property true if app is simulate. 25 | * 26 | * @var bool 27 | */ 28 | public $is_simulate; 29 | 30 | /** 31 | * Property true if app is verbose. 32 | * 33 | * @var bool 34 | */ 35 | public $is_verbose; 36 | 37 | /** 38 | * @var string 39 | */ 40 | public $key; 41 | 42 | /** 43 | * @var string 44 | */ 45 | public $proj; 46 | 47 | /** 48 | * @var string 49 | */ 50 | public $env; 51 | 52 | public function init() 53 | { 54 | $items = $this->app->args->getParams('wild'); 55 | $this->is_verbose = $this->app->args->verbose; 56 | $this->is_simulate = $this->app->args->simulate; 57 | 58 | list($proj, $env) = SettingFile::extractProjEnv($items); 59 | $this->key = $proj . "." . $env; 60 | $this->proj = $proj; 61 | $this->env = $env; 62 | } 63 | 64 | /** 65 | * Method to run the rsync cmd. 66 | * 67 | * The main idea is: 68 | * 1 - get [$proj].[$env].json file 69 | * 2 - convert the file to an obj 70 | * 3 - run rsync with the details in the obj 71 | * 72 | * @return mixed The status code. 73 | */ 74 | public function exec() 75 | { 76 | $settings = SettingFile::getObject($this->key); 77 | 78 | $rsync = new Rsync($settings); 79 | $rsync->setVerbose($this->is_verbose); 80 | $rsync->setAction(RsyncCommands::UPLOAD); 81 | 82 | $msg = RsyncMessage::banner($this->proj, $this->env, $rsync->cmd); 83 | $this->app->inline($msg); 84 | 85 | if ($this->is_simulate) { 86 | $this->app->inline($rsync->getCmd()); 87 | return $this->simulate(); 88 | } 89 | 90 | $rsync->run(); 91 | } 92 | 93 | public function simulate() 94 | { 95 | $this->msg = 'Simulation mode' . "\n"; 96 | 97 | return $this->handleSuccess(); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/AwsUpload/Command/ValidCommand.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Command; 14 | 15 | interface ValidCommand 16 | { 17 | public function isValid(); 18 | } 19 | -------------------------------------------------------------------------------- /src/AwsUpload/Command/VersionCommand.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Command; 14 | 15 | use AwsUpload\Message\VersionMessage; 16 | 17 | class VersionCommand extends BasicCommand 18 | { 19 | /** 20 | * Method used to print the version. 21 | * 22 | * @return int The status code. 23 | */ 24 | public function run() 25 | { 26 | $this->msg = VersionMessage::success($this->app->version); 27 | 28 | return $this->handleSuccess(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/AwsUpload/Io/Args.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Io; 14 | 15 | /** 16 | * @property bool quiet If args quiet is present. 17 | * @property bool envs If args envs is present. 18 | * @property bool proj If args proj is present. 19 | * @property bool version If args version is present. 20 | * @property bool verbose If args verbose is present. 21 | * @property bool wild If args wild is present. 22 | */ 23 | class Args 24 | { 25 | 26 | /** 27 | * Flag that define if input $is_argv or a normal array. 28 | * 29 | * @var boolean 30 | */ 31 | public $is_argv = true; 32 | 33 | /** 34 | * The input array to parse. 35 | * 36 | * @var array 37 | */ 38 | public $input = array(); 39 | 40 | /** 41 | * The flags array. 42 | * 43 | * @var array 44 | */ 45 | protected $flags = array(); 46 | 47 | /** 48 | * The command array. 49 | * 50 | * @var array 51 | */ 52 | protected $cmds = array(); 53 | 54 | /** 55 | * Multi dimensional array of boolean values to define if 56 | * a flag/option has been used. 57 | * 58 | * @var array 59 | */ 60 | protected $props = array("wild" => false); 61 | 62 | /** 63 | * Multi dimensional array to contain the values for 64 | * a flag/option used. 65 | * 66 | * @var array 67 | */ 68 | protected $params = array("wild" => array()); 69 | 70 | /** 71 | * Define the parser state. 72 | * So which command we are currently parsing. 73 | * 74 | * @var string 75 | */ 76 | protected $state = "wild"; 77 | 78 | /** 79 | * The constructor for the Args. 80 | * You can define an alternative input source. 81 | * 82 | * @param array $input Alternative input. 83 | */ 84 | public function __construct($input = null) 85 | { 86 | $this->is_argv = is_null($input); 87 | $this->input = ($this->is_argv) ? $_SERVER['argv'] : $input; 88 | } 89 | 90 | /** 91 | * Magic method to access the $props array. 92 | * 93 | * E.g.: $args->quiet 94 | * 95 | * @param string $key The property we are trying to access. 96 | * 97 | * @return bool 98 | */ 99 | public function __get($key) 100 | { 101 | return (array_key_exists($key, $this->props)) ? $this->props[$key] : false; 102 | } 103 | 104 | /** 105 | * Method to get the params collected for a specific command. 106 | * 107 | * @param string $key The command key. 108 | * 109 | * @return array 110 | */ 111 | public function getParams($key) 112 | { 113 | $params = (array_key_exists($key, $this->params)) ? $this->params[$key] : array(); 114 | return $params; 115 | } 116 | 117 | /** 118 | * Method to get the first element from params[key]. 119 | * 120 | * @param string $key The command key. 121 | * 122 | * @return string 123 | */ 124 | public function getFirst($key) 125 | { 126 | $params = $this->getParams($key); 127 | 128 | return (array_key_exists(0, $params)) ? $params[0] : null; 129 | } 130 | 131 | /** 132 | * Add the flags to parse. 133 | * 134 | * E.g.: $flags = [ 135 | * "copy" => ["copy", "cp"], 136 | * "verbose" => ["verbose", "v"], 137 | * ]; 138 | * 139 | * @param array $flags 140 | */ 141 | public function addFlags($flags) 142 | { 143 | $this->flags = $flags; 144 | } 145 | 146 | /** 147 | * Add the flags and options to parse as commands. 148 | * 149 | * E.g.: $cmd = [ 150 | * "copy" => ["copy", "cp"], 151 | * "verbose" => ["verbose", "v"], 152 | * ]; 153 | * 154 | * @param array $cmds 155 | */ 156 | public function addCmds($cmds) 157 | { 158 | $this->cmds = $cmds; 159 | } 160 | 161 | /** 162 | * Method to init the $props array. 163 | * 164 | * It contains if a command or an option exists. 165 | * e.g.: $this->props['wild'] = true; 166 | * 167 | * @return void 168 | */ 169 | public function initProps() 170 | { 171 | foreach ($this->cmds as $key => $parseValues) { 172 | $this->props[$key] = false; 173 | } 174 | 175 | foreach ($this->flags as $key => $parseValues) { 176 | $this->props[$key] = false; 177 | } 178 | } 179 | 180 | /** 181 | * Method to init the $params array. 182 | * 183 | * It contains the arguments passed to a command. 184 | * e.g.: $this->params['wild'] = ['blog', 'dev']; 185 | * 186 | * @return void 187 | */ 188 | public function initParams() 189 | { 190 | foreach ($this->cmds as $key => $parseValues) { 191 | $this->params[$key] = array(); 192 | } 193 | } 194 | 195 | /** 196 | * Method to parse the input. 197 | * 198 | * @return void 199 | */ 200 | public function parse() 201 | { 202 | $this->initProps(); 203 | $this->initParams(); 204 | $this->load(); 205 | } 206 | 207 | /** 208 | * Method to load the values from input. 209 | * 210 | * @return void 211 | */ 212 | public function load() 213 | { 214 | $input = $this->getCleanInput(); 215 | $this->populate($input); 216 | } 217 | 218 | /** 219 | * Method to remove script name if input is from argv. 220 | * 221 | * @return array 222 | */ 223 | public function getCleanInput() 224 | { 225 | $input = $this->input; 226 | 227 | if ($this->is_argv) { 228 | unset($input[0]); 229 | } 230 | 231 | return $input; 232 | } 233 | 234 | /** 235 | * The parser. 236 | * 237 | * @param array $input The cleaned input array. 238 | * 239 | * @return void 240 | */ 241 | public function populate($input) 242 | { 243 | foreach ($input as $arg) { 244 | if ($this->isFlag($arg)) { 245 | continue; 246 | } 247 | 248 | if ($this->isCmd($arg)) { 249 | continue; 250 | } 251 | 252 | if ($this->state === 'wild') { 253 | $this->props['wild'] = true; 254 | } 255 | 256 | $this->params[$this->state][] = $arg; 257 | } 258 | } 259 | 260 | /** 261 | * Check if it's a isFlag 262 | * 263 | * @param string $arg One of the input values. 264 | * 265 | * @return boolean 266 | */ 267 | public function isFlag($arg) 268 | { 269 | $clean_arg = trim($arg, "-"); 270 | $is_flag = false; 271 | 272 | foreach ($this->flags as $key => $parseValues) { 273 | foreach ($parseValues as $flag) { 274 | if ($flag === $clean_arg) { 275 | $this->props[$key] = true; 276 | $is_flag = true; 277 | } 278 | } 279 | } 280 | 281 | return $is_flag; 282 | } 283 | 284 | /** 285 | * Check if it's a isCmd 286 | * 287 | * @param string $arg One of the input values. 288 | * 289 | * @return boolean 290 | */ 291 | public function isCmd($arg) 292 | { 293 | $clean_arg = trim($arg, "-"); 294 | $is_cmd = false; 295 | 296 | foreach ($this->cmds as $key => $parseValues) { 297 | foreach ($parseValues as $cmd) { 298 | if ($cmd === $clean_arg) { 299 | $this->props[$key] = true; 300 | $this->state = $key; 301 | $is_cmd = true; 302 | } 303 | } 304 | } 305 | 306 | return $is_cmd; 307 | } 308 | } 309 | -------------------------------------------------------------------------------- /src/AwsUpload/Io/Output.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Io; 14 | 15 | use function cli\out; 16 | 17 | class Output 18 | { 19 | 20 | /** 21 | * Method to color the bash output. 22 | * 23 | * The method is going to replace some custom tags with the equivalent 24 | * color in bash. 25 | * 26 | * Eg: 27 | * -> \e[31m 28 | * -> \e[32m 29 | * -> \e[33m 30 | * 31 | * @param string $text The text to parse and inject with the colors. 32 | * 33 | * @return string 34 | */ 35 | public static function color($text) 36 | { 37 | $text = str_replace("", "\e[31m", $text); 38 | $text = str_replace("", "\e[0m", $text); 39 | $text = str_replace("", "\e[32m", $text); 40 | $text = str_replace("", "\e[0m", $text); 41 | $text = str_replace('', "\e[33m", $text); 42 | $text = str_replace('', "\e[0m", $text); 43 | $text = str_replace('', "\e[34m", $text); 44 | $text = str_replace('', "\e[0m", $text); 45 | 46 | return $text; 47 | } 48 | 49 | /** 50 | * Method to render the text in the bash output. 51 | * 52 | * The method is going to write on the STDOUT. 53 | * 54 | * @param string $text The text to put on STDOUT. 55 | * 56 | * @return void 57 | */ 58 | public function render($text) 59 | { 60 | $text = $this->color($text); 61 | 62 | out($text); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/AwsUpload/Io/OutputEcho.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Io; 14 | 15 | use function cli\out; 16 | use AwsUpload\Io\Output; 17 | 18 | class OutputEcho extends Output 19 | { 20 | /** 21 | * Method to render the text with echo. 22 | * 23 | * @param string $text The text to echo. 24 | * 25 | * @return void 26 | */ 27 | public function render($text) 28 | { 29 | $text = $this->color($text); 30 | 31 | echo $text; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/AwsUpload/Message/ArgCommandMessage.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Message; 14 | 15 | interface ArgCommandMessage 16 | { 17 | public static function noArgs(); 18 | } 19 | -------------------------------------------------------------------------------- /src/AwsUpload/Message/CheckMessage.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Message; 14 | 15 | use AwsUpload\Message\CommonMessage; 16 | use AwsUpload\Message\ArgCommandMessage; 17 | 18 | class CheckMessage implements ArgCommandMessage 19 | { 20 | /** 21 | * Method to echo the aws-upload check report. 22 | * 23 | * @param array $report The report value 24 | * 25 | * @return string 26 | */ 27 | public static function report($report) 28 | { 29 | // Labels 30 | $check_labels = array('✔', '✖'); 31 | $valid_labels = array("VALID", "INVALID"); 32 | $exist_labels = array("EXISTS", "NOT EXISTS"); 33 | $perms_labels = array($report['pem_perms'], $report['pem_perms']); 34 | 35 | $check_json = CommonMessage::plot($report['is_valid_json'], $check_labels); 36 | $check_pem = CommonMessage::plot($report['pem_exists'], $check_labels); 37 | $check_400 = CommonMessage::plot($report['is_400'], $check_labels); 38 | $check_loc = CommonMessage::plot($report['local_exists'], $check_labels); 39 | 40 | $is_valid_json = CommonMessage::plot($report['is_valid_json'], $valid_labels); 41 | $pem_exists = CommonMessage::plot($report['pem_exists'], $exist_labels); 42 | $is_400_perms = CommonMessage::plot($report['is_400'], $perms_labels); 43 | $local_exists = CommonMessage::plot($report['local_exists'], $exist_labels); 44 | 45 | // Json 46 | $text = "Checking...\n\n" . 47 | " File analysing:\n" . 48 | " " . $report['path'] . "" . "\n" . 49 | " " . $check_json . " Json " . $is_valid_json . "\n" . 50 | " " . $report['error_json']; 51 | 52 | // Pem 53 | $text .= "\n" . 54 | " Pem File:\n" . 55 | " " . $report['pem'] . "\n" . 56 | " " . $check_pem . " Pem " . $pem_exists . "\n"; 57 | 58 | if ($report['pem_exists']) { 59 | $text .= " " . $check_400 . " Pem Perm " . $is_400_perms . "\n"; 60 | 61 | if (!$report['is_400']) { 62 | $text .= ' Try to type: chmod 400 ' . $report['pem'] . "\n"; 63 | } 64 | } 65 | 66 | // Local 67 | $text .= "\n" . 68 | " Local Folder:\n" . 69 | " " . $report['local'] . "" . "\n" . 70 | " " . $check_loc . " Folder " . $local_exists . "\n"; 71 | 72 | return $text; 73 | } 74 | 75 | public static function noArgs() 76 | { 77 | $text = "It seems that you don't proper arguments for this command.\n\n" . 78 | 79 | "How to use check:\n\n" . 80 | " aws-upload check \n" . 81 | " E.g.: aws-upload check blog.dev\n\n" . 82 | "\n"; 83 | 84 | return $text; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/AwsUpload/Message/CommonMessage.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Message; 14 | 15 | class CommonMessage 16 | { 17 | /** 18 | * Method to echo the aws-upload banner. 19 | * 20 | * @return string 21 | */ 22 | public static function banner() 23 | { 24 | $banner = <<" . $banner . ""; 36 | } 37 | 38 | /** 39 | * Method to facilitate the report building. 40 | * 41 | * @param bool $condition The state to evaluate 42 | * @param string[] $labels The possible values to display 43 | * 44 | * @return string 45 | */ 46 | public static function plot($condition, $labels) 47 | { 48 | return ($condition) ? "" . $labels[0] . "" : "" . $labels[1] . ""; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/AwsUpload/Message/CopyMessage.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Message; 14 | 15 | use AwsUpload\Message\ArgCommandMessage; 16 | 17 | class CopyMessage implements ArgCommandMessage 18 | { 19 | public static function noArgs() 20 | { 21 | $text = "It seems that you don't proper arguments for this command.\n\n" . 22 | 23 | "How to use copy:\n\n" . 24 | " aws-upload copy \n" . 25 | " E.g.: aws-upload copy blog.dev blog.prod\n\n" . 26 | "\n"; 27 | 28 | return $text; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/AwsUpload/Message/DeleteMessage.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Message; 14 | 15 | use AwsUpload\Message\ArgCommandMessage; 16 | 17 | class DeleteMessage implements ArgCommandMessage 18 | { 19 | /** 20 | * Method to support if when AwsUpload::new is successfull. 21 | * 22 | * @param string $key E.g: proj.env 23 | * 24 | * @return string 25 | */ 26 | public static function success($key) 27 | { 28 | $text = "The setting file " . $key . ".json has been deleted successfully.\n\n"; 29 | 30 | return $text; 31 | } 32 | 33 | public static function noArgs() 34 | { 35 | $text = "It seems that you don't proper arguments for this command.\n\n" . 36 | 37 | "How to use delete:\n\n" . 38 | " aws-upload delete \n" . 39 | " E.g.: aws-upload delete blog.dev\n\n" . 40 | "\n"; 41 | 42 | return $text; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/AwsUpload/Message/EditMessage.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Message; 14 | 15 | use AwsUpload\Message\ArgCommandMessage; 16 | 17 | class EditMessage implements ArgCommandMessage 18 | { 19 | /** 20 | * Method to support if when AwsUpload::edit is successfull. 21 | * 22 | * @param string $key E.g: proj.env 23 | * 24 | * @return string 25 | */ 26 | public static function success($key) 27 | { 28 | $text = "The setting file " . $key . ".json has been edited successfully.\n\n"; 29 | 30 | return $text; 31 | } 32 | 33 | public static function noArgs() 34 | { 35 | $text = "It seems that you don't proper arguments for this command.\n\n" . 36 | 37 | "How to use edit:\n\n" . 38 | " aws-upload edit \n" . 39 | " E.g.: aws-upload edit blog.dev\n\n" . 40 | "\n"; 41 | 42 | return $text; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/AwsUpload/Message/EnvsMessage.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Message; 14 | 15 | use AwsUpload\Setting\SettingFile; 16 | 17 | class EnvsMessage 18 | { 19 | /** 20 | * Method to echo the help message about when the project 21 | * selected doesn't exist. 22 | * 23 | * @param string $projFilter The project name. 24 | * 25 | * @return string 26 | */ 27 | public static function errorNoEnvsProj($projFilter) 28 | { 29 | $projList = SettingFile::getProjs(); 30 | $text = "The project " . $projFilter . " you are tring to use doesn't exist." . "\n\n"; 31 | 32 | $next = "These are the available projects: \n\n"; 33 | foreach ($projList as $proj) { 34 | $next .= " + " . $proj . "\n"; 35 | } 36 | 37 | if (count($projList) > 0) { 38 | $next .= "\nTo get the envs from one of them, run (for example):\n\n" . 39 | " aws-upload -e " . $projList[0] . "\n"; 40 | } 41 | 42 | return $text . $next . "\n"; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/AwsUpload/Message/ErrorMessage.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Message; 14 | 15 | use cli\Table; 16 | use AwsUpload\Io\Output; 17 | use AwsUpload\Setting\SettingFile; 18 | 19 | class ErrorMessage 20 | { 21 | 22 | /** 23 | * Generic method for when no args. 24 | * 25 | * @return string 26 | */ 27 | public static function noArgs() 28 | { 29 | $text = "It seems that you don't have the correct args for this command.\n" . 30 | "\n"; 31 | 32 | return $text; 33 | } 34 | 35 | /** 36 | * Method to echo the help message about no project. 37 | * 38 | * @return string 39 | */ 40 | public static function noProjects() 41 | { 42 | $text = "It seems that you don't have any project setup.\nTry to type:\n\n" . 43 | " aws-upload new project.test\n" . 44 | "\n"; 45 | 46 | return $text; 47 | } 48 | 49 | /** 50 | * Method to echo the help message about when the pair project 51 | * environment doesn't exist. 52 | * 53 | * So actually, we check that project.env.json it does exist 54 | * otherwise we print this message. 55 | * 56 | * @param string $project The project name or key. 57 | * @param string $env The env name. 58 | * 59 | * @return string 60 | */ 61 | public static function noFileFound($project, $env = null) 62 | { 63 | $files = SettingFile::getList(); 64 | if (count($files) === 0) { 65 | $text = static::noProjects(); 66 | return $text; 67 | } 68 | 69 | $text = "It seems that there is NO setting files for " . $project . 70 | ", " . $env . "\n\n"; 71 | 72 | if (is_null($env)) { 73 | $text = "It seems that there is NO setting files for " . $project . 74 | "\n\n"; 75 | } 76 | 77 | $text .= static::getProjEnvTable(); 78 | 79 | return $text; 80 | } 81 | 82 | /** 83 | * Method support if in AwsUpload::new the key is not valid. 84 | * 85 | * @param string $key The key prompted. 86 | * 87 | * @return string 88 | */ 89 | public static function noValidKey($key) 90 | { 91 | $text = "It seems that the key " . $key . " is not valid:\n\n" . 92 | "Please try to use this format:\n" . 93 | " - [project].[environmet]\n\n" . 94 | "Examples of valid key to create a new setting file:\n" . 95 | " - my-site.staging\n" . 96 | " - my-site.dev\n" . 97 | " - my-site.prod\n\n" . 98 | "Tips on choosing the key name:\n" . 99 | " - for [project] and [environmet] try to be: short, sweet, to the point\n" . 100 | " - use only one 'dot' . in the name\n" . 101 | "\n"; 102 | 103 | return $text; 104 | } 105 | 106 | /** 107 | * Method to support if in AwsUpload::new the key already exists. 108 | * 109 | * @param string $key E.g: proj.env 110 | * 111 | * @return string 112 | */ 113 | public static function keyAlreadyExists($key) 114 | { 115 | $text = "It seems that the key " . $key . " already exists try to use another one.\n\n" . 116 | "Please consider you already have the following elements:\n" . 117 | static::getProjEnvTable() . 118 | "\n"; 119 | 120 | return $text; 121 | } 122 | 123 | 124 | /** 125 | * Method to get the proj/env table. 126 | * 127 | * @return string 128 | */ 129 | public static function getProjEnvTable() 130 | { 131 | $files = SettingFile::getList(); 132 | 133 | $headers = array('Project', 'Environment'); 134 | $data = array(); 135 | foreach ($files as $file) { 136 | list($proj, $env, $ext) = explode(".", $file); 137 | $proj = Output::color("" . $proj . ""); 138 | $env = Output::color("" . $env . ""); 139 | 140 | $data[] = array($proj, $env); 141 | } 142 | 143 | $table = new Table(); 144 | $table->setHeaders($headers); 145 | $table->setRows($data); 146 | 147 | $msg = ''; 148 | foreach ($table->getDisplayLines() as $key => $line) { 149 | $msg .= $line . "\n"; 150 | } 151 | 152 | return $msg; 153 | } 154 | } 155 | -------------------------------------------------------------------------------- /src/AwsUpload/Message/ExportMessage.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Message; 14 | 15 | use AwsUpload\Message\ArgCommandMessage; 16 | 17 | class ExportMessage implements ArgCommandMessage 18 | { 19 | /** 20 | * Method to support if when AwsUpload::export is successfull. 21 | * 22 | * @param string $key E.g: proj.env 23 | * 24 | * @return string 25 | */ 26 | public static function success($key) 27 | { 28 | $text = "The setting file " . $key . ".json has been exported successfully.\n\n"; 29 | 30 | return $text; 31 | } 32 | 33 | public static function noArgs() 34 | { 35 | $text = "It seems that you don't proper arguments for this command.\n\n" . 36 | 37 | "How to use export:\n\n" . 38 | " aws-upload export []\n" . 39 | " E.g.: aws-upload exprot blog.dev\n" . 40 | " E.g.: aws-upload exprot blog.dev ~/Desktop/\n\n" . 41 | 42 | "\n"; 43 | 44 | return $text; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/AwsUpload/Message/HelpMessage.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Message; 14 | 15 | class HelpMessage 16 | { 17 | 18 | /** 19 | * Method to echo the help message. 20 | * 21 | * @return string 22 | */ 23 | public static function success() 24 | { 25 | $text = <<Usage: 28 | 29 | aws-upload [--simulate|--dry-run] [-v|--verbose] 30 | 31 | aws-upload keys [-q|--quiet] 32 | aws-upload projs [-q|--quiet] 33 | aws-upload envs [-q|--quiet] 34 | 35 | aws-upload diff # The format is proj.env eg: landing.test 36 | aws-upload new # The format is proj.env eg: landing.test 37 | aws-upload edit # The format is proj.env eg: landing.test 38 | aws-upload copy # and are in the format proj.env 39 | aws-upload delete # The format is proj.env eg: landing.test 40 | aws-upload import # The is the path to a json file. 41 | aws-upload export [] # The is a the directory path. 42 | aws-upload check # The format is proj.env eg: landing.test 43 | 44 | aws-upload (self-update | selfupdate) 45 | aws-upload autocomplete 46 | 47 | Output Options: 48 | 49 | -v|--verbose Output more verbose information. 50 | -q|--quiet Reduce or suppress additional information. 51 | 52 | Miscellaneous Options: 53 | 54 | -h|--help Prints this usage information. 55 | -V|--version Prints the application version. 56 | --dry-run It simulates the rsync command without upload anything. 57 | --simulate It simulates the rsync command without upload anything. 58 | 59 | Available commands: 60 | 61 | -df|diff Show the files that are not yet synced. 62 | -k|keys Print all the projects' keys. 63 | -p|projs Print all the projects. 64 | -e|envs Print all the environments for a specific project. 65 | -n|new Create a new setting file. 66 | -E|edit Edit a setting file. 67 | -cp|copy Copy a setting file. 68 | -rm|delete Delete a setting file. 69 | -i|import Import a setting file. 70 | -ex|export Export a setting file. 71 | -c|check Check a setting file for debug. 72 | self-update Updates aws-upload to the latest version. 73 | selfupdate Updates aws-upload to the latest version. 74 | autocomplete Enable the autocomplete for oh-my-zsh. 75 | 76 | 77 | EOT; 78 | return $text; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/AwsUpload/Message/ImportMessage.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Message; 14 | 15 | use AwsUpload\Message\ArgCommandMessage; 16 | 17 | class ImportMessage implements ArgCommandMessage 18 | { 19 | /** 20 | * Method to support if when AwsUpload::import is successfull. 21 | * 22 | * @param string $key E.g: proj.env 23 | * 24 | * @return string 25 | */ 26 | public static function success($key) 27 | { 28 | $text = "The setting file " . $key . ".json has been imported successfully.\n\n" . 29 | "To edit again the file type:\n" . 30 | " aws-upload edit " . $key . "\n" . 31 | "\n"; 32 | 33 | return $text; 34 | } 35 | 36 | public static function errorNotFound($path) 37 | { 38 | $text = "It seems that you don't proper arguments for this command.\n\n" . 39 | 40 | "Argument given:\n\n" . 41 | " src: " . $path . "\n\n" . 42 | 43 | 44 | "How to use import:\n\n" . 45 | " aws-upload import \n" . 46 | " E.g.: aws-upload import ~/Desktop/blog.dev.json\n\n" . 47 | 48 | "The cause it may be:\n\n" . 49 | " - no file given\n" . 50 | " - the argument give was a folder\n" . 51 | " - the argument give was a file but it doesn't exist\n" . 52 | "\n"; 53 | 54 | return $text; 55 | } 56 | 57 | public static function noArgs() 58 | { 59 | $text = "It seems that you don't proper arguments for this command.\n\n" . 60 | 61 | "How to use import:\n\n" . 62 | " aws-upload import \n" . 63 | " E.g.: aws-upload import ~/Desktop/blog.dev.json\n\n" . 64 | "\n"; 65 | 66 | return $text; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/AwsUpload/Message/NewMessage.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Message; 14 | 15 | use AwsUpload\Message\ArgCommandMessage; 16 | 17 | class NewMessage implements ArgCommandMessage 18 | { 19 | /** 20 | * Method to support if when AwsUpload::new is successfull. 21 | * 22 | * @param string $key E.g: proj.env 23 | * 24 | * @return string 25 | */ 26 | public static function success($key) 27 | { 28 | $text = "The setting file " . $key . ".json has been created successfully.\n\n" . 29 | "To edit again the file type:\n" . 30 | " aws-upload edit " . $key . "\n" . 31 | "\n"; 32 | 33 | return $text; 34 | } 35 | 36 | /** 37 | * @return string 38 | */ 39 | public static function noArgs() 40 | { 41 | $text = "It seems that you don't proper arguments for this command.\n\n" . 42 | "How to use new:\n\n" . 43 | " aws-upload new \n" . 44 | " E.g.: aws-upload new blog.prod\n\n" . 45 | "\n"; 46 | 47 | return $text; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/AwsUpload/Message/RsyncMessage.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Message; 14 | 15 | class RsyncMessage 16 | { 17 | 18 | /** 19 | * Method to echo the aws-upload banner. 20 | * 21 | * @param string $proj The project name. 22 | * @param string $env The env name. 23 | * @param string $cmd The rsync cmd. 24 | * 25 | * @return string 26 | */ 27 | public static function banner($proj, $env, $cmd) 28 | { 29 | $env = escapeshellarg($env); 30 | $proj = escapeshellarg($proj); 31 | 32 | $text = <<================================================================== 34 | 35 | _ _ 36 | | | | | 37 | __ ___ _____ ______ _ _ _ __ | | ___ __ _ __| | 38 | / _` \ \ /\ / / __|______| | | | '_ \| |/ _ \ / _` |/ _` | 39 | | (_| |\ V V /\__ \ | |_| | |_) | | (_) | (_| | (_| | 40 | \__,_| \_/\_/ |___/ \__,_| .__/|_|\___/ \__,_|\__,_| 41 | | | 42 | |_| 43 | 44 | 45 | ================================================================== 46 | 47 | Start processing: 48 | Proj: $proj 49 | Env: $env 50 | 51 | ================================================================== 52 | 53 | EOT; 54 | return $text; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/AwsUpload/Message/VersionMessage.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Message; 14 | 15 | class VersionMessage 16 | { 17 | /** 18 | * Method to echo the current version. 19 | * 20 | * @param string $version The version. 21 | * 22 | * @return string 23 | */ 24 | public static function success($version) 25 | { 26 | $text = "aws-upload version " . $version . " \n"; 27 | 28 | return $text; 29 | } 30 | } 31 | 32 | -------------------------------------------------------------------------------- /src/AwsUpload/Model/Settings.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Model; 14 | 15 | class Settings 16 | { 17 | /** 18 | * The path to the setting file. 19 | * 20 | * @var string 21 | */ 22 | public $path; 23 | 24 | /** 25 | * The setting file exists. 26 | * 27 | * @var bool 28 | */ 29 | public $file_exists; 30 | 31 | /** 32 | * Status of the setting file as json. 33 | * 34 | * @var bool 35 | */ 36 | public $is_valid_json; 37 | 38 | /** 39 | * Error related to json. 40 | * 41 | * @var string 42 | */ 43 | public $error_json = ''; 44 | 45 | /** 46 | * @var string 47 | */ 48 | public $pem; 49 | 50 | /** 51 | * @var string 52 | */ 53 | public $local; 54 | 55 | /** 56 | * @var string 57 | */ 58 | public $remote; 59 | 60 | /** 61 | * @var array 62 | */ 63 | public $exclude; 64 | 65 | public function __construct($path) 66 | { 67 | $this->path = $path; 68 | 69 | $this->file_exists = $this->fileExists(); 70 | $this->is_valid_json = $this->isValidJson(); 71 | $this->error_json = $this->getErrorJson(); 72 | 73 | $this->load(); 74 | } 75 | 76 | /** 77 | * Method to check if the setting file it does exist. 78 | * 79 | * @return bool 80 | */ 81 | public function fileExists() 82 | { 83 | return file_exists($this->path) && is_file($this->path); 84 | } 85 | 86 | /** 87 | * Method to get the json if the file exists. 88 | * 89 | * @return bool 90 | */ 91 | public function getJson() 92 | { 93 | $json = ''; 94 | if ($this->file_exists) { 95 | $tmp = file_get_contents($this->path); 96 | $json = json_decode($tmp, true); 97 | } 98 | 99 | return $json; 100 | } 101 | 102 | /** 103 | * Method to chek if a file is a valid json. 104 | * 105 | * @return bool 106 | */ 107 | public function isValidJson() 108 | { 109 | $this->getJson(); 110 | return (json_last_error() === JSON_ERROR_NONE); 111 | } 112 | 113 | /** 114 | * Method to get the json error description. 115 | * 116 | * @return string 117 | */ 118 | public static function getErrorJson() 119 | { 120 | $errors = array( 121 | JSON_ERROR_NONE => '', 122 | JSON_ERROR_DEPTH => " - Maximum stack depth exceeded\n", 123 | JSON_ERROR_STATE_MISMATCH => " - Underflow or the modes mismatch\n", 124 | JSON_ERROR_CTRL_CHAR => " - Unexpected control character found\n", 125 | JSON_ERROR_SYNTAX => " - Syntax error, malformed JSON\n", 126 | JSON_ERROR_UTF8 => " - Malformed UTF-8 characters, possibly incorrectly encoded\n", 127 | ); 128 | $last_error = json_last_error(); 129 | 130 | $msg = ' - Unknown error'; 131 | if (array_key_exists($last_error, $errors)) { 132 | $msg = $errors[$last_error]; 133 | } 134 | 135 | return $msg; 136 | } 137 | 138 | /** 139 | * Load the values in the obj. 140 | * 141 | * @return void 142 | */ 143 | public function load() 144 | { 145 | $tmp = (object) $this->getJson(); 146 | 147 | $this->pem = property_exists($tmp, 'pem') ? $tmp->pem : ''; 148 | $this->local = property_exists($tmp, 'local') ? $tmp->local : ''; 149 | $this->remote = property_exists($tmp, 'remote') ? $tmp->remote : ''; 150 | $this->exclude = property_exists($tmp, 'exclude') ? $tmp->exclude : array(); 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /src/AwsUpload/Model/Status.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Model; 14 | 15 | class Status 16 | { 17 | /** 18 | * @var int 19 | */ 20 | const SUCCESS = 0; 21 | 22 | /** 23 | * To use when the error is relative to aws-upload. 24 | * 25 | * @var int 26 | */ 27 | const ERROR_INVALID = 1; 28 | 29 | /** 30 | * To use when the error is relative to the rest of the system. 31 | * 32 | * @var int 33 | */ 34 | const SYSTEM_NOT_READY = 2; 35 | } 36 | -------------------------------------------------------------------------------- /src/AwsUpload/Setting/SettingFile.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Setting; 14 | 15 | use AwsUpload\System\File; 16 | use AwsUpload\System\System; 17 | use AwsUpload\Model\Settings; 18 | use AwsUpload\Setting\SettingFolder; 19 | 20 | class SettingFile 21 | { 22 | /** 23 | * Returns the list or setting files in the aws-upload folder. 24 | * 25 | * @return array 26 | */ 27 | public static function getList() 28 | { 29 | $path = SettingFolder::getPath(); 30 | $items = scandir($path); 31 | 32 | // clean . and .. 33 | unset($items[0]); 34 | unset($items[1]); 35 | 36 | $files = array(); 37 | foreach ($items as $key => $item) { 38 | if (is_dir($path . "/" . $item)) { 39 | continue; 40 | } 41 | 42 | if (strpos($item, ".json") === false) { 43 | continue; 44 | } 45 | 46 | $files[] = $item; 47 | } 48 | 49 | return $files; 50 | } 51 | 52 | /** 53 | * Returns the setting file path from a key. 54 | * 55 | * @param string $key The setting file identifier. 56 | * 57 | * @return string 58 | */ 59 | public static function getPath($key) 60 | { 61 | $path = SettingFolder::getPath(); 62 | $path = $path . '/' . $key . '.json'; 63 | 64 | return $path; 65 | } 66 | 67 | /** 68 | * Returns the list or setting files in the aws-upload folder. 69 | * 70 | * @param string $key The setting file identifier. 71 | * 72 | * @return Settings 73 | */ 74 | public static function getObject($key) 75 | { 76 | $path = static::getPath($key); 77 | $settings = new Settings($path); 78 | 79 | return $settings; 80 | } 81 | 82 | /** 83 | * Method used to get the projects' key available. 84 | * 85 | * @return array 86 | */ 87 | public static function getKeys() 88 | { 89 | $files = SettingFile::getList(); 90 | 91 | $keys = array(); 92 | foreach ($files as $fileName) { 93 | $key = str_replace('.json', '', $fileName); 94 | 95 | if (!in_array($key, $keys)) { 96 | $keys[] = $key; 97 | } 98 | } 99 | 100 | return $keys; 101 | } 102 | 103 | /** 104 | * Method used to get the projects available. 105 | * 106 | * @return array 107 | */ 108 | public static function getProjs() 109 | { 110 | $files = SettingFile::getList(); 111 | 112 | $projs = array(); 113 | foreach ($files as $key) { 114 | list($proj, $env, $ext) = explode(".", $key); 115 | 116 | if (!in_array($proj, $projs)) { 117 | $projs[] = $proj; 118 | } 119 | } 120 | 121 | return $projs; 122 | } 123 | 124 | /** 125 | * Method used to print the environments available for a project. 126 | * 127 | * @param string $projFilter The project for which we want the envs. 128 | * 129 | * @return array | string 130 | */ 131 | public static function getEnvs($projFilter) 132 | { 133 | $files = SettingFile::getList(); 134 | $store = array(); 135 | foreach ($files as $filename) { 136 | list($proj, $env, $ext) = explode(".", $filename); 137 | 138 | if (!isset($store[$proj])) { 139 | $store[$proj] = array(); 140 | } 141 | 142 | $store[$proj][] = $env; 143 | } 144 | 145 | $envs = array(); 146 | $envsRaw = isset($store[$projFilter]) ? $store[$projFilter] : array(); 147 | foreach ($envsRaw as $env) { 148 | if (!in_array($env, $envs)) { 149 | $envs[] = $env; 150 | } 151 | } 152 | 153 | return $envs; 154 | } 155 | 156 | /** 157 | * Method used to create a setting file. 158 | * 159 | * @param string $key The project identifier proj.envs . 160 | * 161 | * @return void 162 | */ 163 | public static function create($key) 164 | { 165 | $template = "{\n" . 166 | ' "pem": "/home/ssh/key.pem",' . "\n" . 167 | ' "local": "/home/project/*",' . "\n" . 168 | ' "remote": "ubuntu@xxx.xxx.xxx.xxx:/var/www/project",' . "\n" . 169 | ' "exclude": [' . "\n" . 170 | ' ".env",' . "\n" . 171 | ' ".git/",' . "\n" . 172 | ' "node_modules"' . "\n" . 173 | ' ]' . "\n" . 174 | "}\n"; 175 | $path = SettingFolder::getPath(); 176 | 177 | file_put_contents($path . '/' . $key . '.json', $template); 178 | } 179 | 180 | /** 181 | * Method used to edit a setting file. 182 | * 183 | * @param string $key The project identifier proj.envs . 184 | * 185 | * @return void 186 | */ 187 | public static function edit($key) 188 | { 189 | $path = SettingFolder::getPath(); 190 | $editor = System::getEditor(); 191 | 192 | system($editor . ' ' . $path . '/' . $key . '.json < `tty` > `tty`'); 193 | } 194 | 195 | /** 196 | * Method to copy a setting file 197 | * 198 | * @param string $oldKey The key for the exisiting setting. 199 | * @param string $newKey The key for the new setting. 200 | * 201 | * @return void 202 | */ 203 | public static function copy($oldKey, $newKey) 204 | { 205 | $path = SettingFolder::getPath(); 206 | $source = $path . '/' . $oldKey . '.json'; 207 | $dest = $path . '/' . $newKey . '.json'; 208 | 209 | File::copy($source, $dest); 210 | } 211 | 212 | /** 213 | * Method to delete a setting file 214 | * 215 | * @param string $key The key for the exisiting setting. 216 | * 217 | * @return void 218 | */ 219 | public static function delete($key) 220 | { 221 | $path = SettingFolder::getPath(); 222 | $src = $path . '/' . $key . '.json'; 223 | 224 | File::delete($src); 225 | } 226 | 227 | /** 228 | * Method to check if the setting file it does exist. 229 | * 230 | * @param string $key The setting file name without the extenion. 231 | * 232 | * @return bool 233 | */ 234 | public static function exists($key) 235 | { 236 | $path = SettingFolder::getPath(); 237 | 238 | return file_exists($path . '/' . $key . '.json'); 239 | } 240 | 241 | 242 | /** 243 | * Method to check if a give key is valid. 244 | * 245 | * The rules are: 246 | * - only one . 247 | * 248 | * @param string $key The setting file name without the extenion. 249 | * 250 | * @return bool 251 | */ 252 | public static function isValidKey($key) 253 | { 254 | $parts = explode('.', $key); 255 | $isValid = (count($parts) === 2); 256 | 257 | return $isValid; 258 | } 259 | 260 | /** 261 | * Method to extract the project and the environment from an array 262 | * 263 | * This method is to cover two cases: 264 | * - aws-upload proj env // double notation 265 | * - aws-upload proj.env // key notation 266 | * 267 | * @param array $items It contains all the extra args. 268 | * 269 | * @return array The array will contain 2 elements in any case. 270 | */ 271 | public static function extractProjEnv(array $items) 272 | { 273 | $proj = 'no-project-given'; 274 | $env = 'no-environment-given'; 275 | 276 | // reorder items in array 277 | // [ 4 => 'a', 6 => 'b' ] ~> [ 0 => 'a', 1 => 'b' ] 278 | if (is_array($items)) { 279 | $items = array_values($items); 280 | } 281 | 282 | if (count($items) === 1) { 283 | if (strpos($items[0], '.') !== false) { 284 | $items = explode('.', $items[0]); 285 | } 286 | } 287 | 288 | if (count($items) === 2) { 289 | $proj = $items[0]; 290 | $env = $items[1]; 291 | } 292 | 293 | return array($proj, $env); 294 | } 295 | } 296 | -------------------------------------------------------------------------------- /src/AwsUpload/Setting/SettingFolder.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\Setting; 14 | 15 | class SettingFolder 16 | { 17 | /** 18 | * Returns the location of the user directory from the environment. 19 | * @throws RuntimeException If the environment value does not exists 20 | * 21 | * @return string 22 | */ 23 | public static function getUserDir() 24 | { 25 | $userEnv = defined('PHP_WINDOWS_VERSION_MAJOR') ? 'APPDATA' : 'HOME'; 26 | $userDir = getenv($userEnv); 27 | 28 | if (!$userDir) { 29 | $msg = 'The ' . $userEnv . ' or AWSUPLOAD_HOME environment variable ' . 30 | 'must be set for aws-upload to run correctly'; 31 | throw new \RuntimeException($msg); 32 | } 33 | 34 | return rtrim(strtr($userDir, '\\', '/'), '/'); 35 | } 36 | 37 | /** 38 | * Returns the system-dependent aws-upload home location, which may not exist. 39 | * 40 | * @return string 41 | */ 42 | public static function getHomeDir() 43 | { 44 | $home = getenv('AWSUPLOAD_HOME'); 45 | 46 | if (!$home) { 47 | $userDir = self::getUserDir(); 48 | 49 | if (defined('PHP_WINDOWS_VERSION_MAJOR')) { 50 | $home = $userDir . '/Aws-upload'; 51 | } else { 52 | $home = $userDir . '/.aws-upload'; 53 | } 54 | } 55 | 56 | return $home; 57 | } 58 | 59 | /** 60 | * Returns the aws-upload home directory, creating it if required. 61 | * @throws RuntimeException If the directory cannot be created 62 | * 63 | * @return string 64 | */ 65 | public static function getPath() 66 | { 67 | $home = self::getHomeDir(); 68 | 69 | if (!is_dir($home)) { 70 | if (!mkdir($home, 0777, true)) { 71 | $msg = 'Unable to create aws-upload home directory "%s"'; 72 | throw new \RuntimeException(sprintf($msg, $home)); 73 | } 74 | } 75 | 76 | return $home; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/AwsUpload/System/File.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\System; 14 | 15 | class File 16 | { 17 | 18 | /** 19 | * Copy a file. 20 | * 21 | * @param string $src The current file path. 22 | * @param string $dst The new file path. 23 | * 24 | * @return bool 25 | */ 26 | public static function copy($src, $dst) 27 | { 28 | return copy($src, $dst); 29 | } 30 | 31 | /** 32 | * Move a file. 33 | * 34 | * @param string $src The current file path. 35 | * @param string $dst The new file path. 36 | * 37 | * @return bool 38 | */ 39 | public static function move($src, $dst) 40 | { 41 | return rename($src, $dst); 42 | } 43 | 44 | /** 45 | * Delete a file. 46 | * 47 | * @param string $src The file path. 48 | * 49 | * @return bool 50 | */ 51 | public static function delete($src) 52 | { 53 | return unlink($src); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/AwsUpload/System/Git.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\System; 14 | 15 | class Git 16 | { 17 | /** 18 | * Define if git is installed. 19 | * 20 | * @return bool 21 | */ 22 | public static function isInstalled() 23 | { 24 | $hasGit = exec('hash git 2>&1'); 25 | return (strlen($hasGit) === 0); 26 | } 27 | 28 | /** 29 | * In case if not in the system. 30 | * 31 | * @return string 32 | */ 33 | public static function errorMsg() 34 | { 35 | $msg = "\n It seems that git is not installed.\n" . 36 | " Please run (or equivalent for your system):\n\n" . 37 | " sudo apt-get install git\n"; 38 | return $msg; 39 | } 40 | 41 | /** 42 | * Clone a repo to a directory. 43 | * 44 | * @param string $repo The repo url. 45 | * @param string $repo_dir The folder path. 46 | * 47 | * @return string 48 | */ 49 | public static function clone($repo, $repo_dir) 50 | { 51 | $git_cmd = 'git clone ' . $repo . ' ' . $repo_dir; 52 | $cmd = self::silentGit($git_cmd); 53 | 54 | return exec($cmd); 55 | } 56 | 57 | /** 58 | * Pull a repo in a directory. 59 | * 60 | * @param string $repo_dir The repo folder path. 61 | * 62 | * @return string 63 | */ 64 | public static function pull($repo_dir) 65 | { 66 | $git_cmd = 'git pull origin master'; 67 | $cmd = self::silentGit($git_cmd); 68 | $cmd = self::goAndComeback($repo_dir, $cmd); 69 | 70 | return exec($cmd); 71 | } 72 | 73 | /** 74 | * Checkout a a tag version. 75 | * 76 | * @param string $repo_dir The repo folder path. 77 | * @param string $tag The tag to checkout. 78 | * 79 | * @return string 80 | */ 81 | public static function checkoutTag($repo_dir, $tag) 82 | { 83 | $git_cmd = 'git checkout ' . $tag; 84 | $cmd = self::silentGit($git_cmd); 85 | $cmd = self::goAndComeback($repo_dir, $cmd); 86 | 87 | return exec($cmd); 88 | } 89 | 90 | /** 91 | * Create command to comeback to current folder. 92 | * 93 | * @param string $repo_dir The repo folder path. 94 | * @param string $cmd The command to run. 95 | * 96 | * @return string 97 | */ 98 | public static function goAndComeback($repo_dir, $cmd) 99 | { 100 | $cmd = 'cd ' . $repo_dir . ' && ' . $cmd . ' && cd - '; 101 | return $cmd; 102 | } 103 | 104 | /** 105 | * Run a git command without output. 106 | * 107 | * @param string $git_cmd The git command. 108 | * 109 | * @return string 110 | */ 111 | public static function silentGit($git_cmd) 112 | { 113 | $cmd = 'env ' . $git_cmd . ' > /dev/null 2>&1'; 114 | return $cmd; 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /src/AwsUpload/System/OhMyZsh.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\System; 14 | 15 | use AwsUpload\Setting\SettingFolder; 16 | 17 | class OhMyZsh 18 | { 19 | /** 20 | * Define if oh-my-zsh is installed. 21 | * 22 | * @return bool 23 | */ 24 | public static function isInstalled() 25 | { 26 | $dir = self::getPath(); 27 | $file = self::getPath() . '/oh-my-zsh.sh'; 28 | 29 | return is_dir($dir) && is_file($file); 30 | } 31 | 32 | /** 33 | * In case if not in the system. 34 | * 35 | * @return string 36 | */ 37 | public static function errorMsg() 38 | { 39 | $text = "\n It seems that oh-my-zsh is not installed.\n" . 40 | " Please run (or equivalent for your system):\n\n" . 41 | " sh -c \"$(wget https://raw.githubusercontent.com/" . 42 | "robbyrussell/oh-my-zsh/master/tools/install.sh -O -)\"\n"; 43 | 44 | return $text; 45 | } 46 | 47 | /** 48 | * Get oh-my-zsh path. 49 | * 50 | * @return string 51 | */ 52 | public static function getPath() 53 | { 54 | $omzDir = exec('echo $ZSH'); 55 | $omzDir = (strlen($omzDir) > 0) ? $omzDir : '~/.oh-my-zsh'; 56 | 57 | return $omzDir; 58 | } 59 | 60 | /** 61 | * Check if plugin is downloaded. 62 | * 63 | * @return bool 64 | */ 65 | public static function hasPluginFiles() 66 | { 67 | $dir = self::getPath() . '/plugins/aws-upload/'; 68 | return is_dir($dir); 69 | } 70 | 71 | /** 72 | * Check if plugin is active. 73 | * 74 | * @return bool 75 | */ 76 | public static function isPluginActive() 77 | { 78 | $isActive = exec("grep aws-upload ~/.zshrc"); 79 | 80 | return (strlen($isActive) > 0); 81 | } 82 | 83 | /** 84 | * Activate the plugin. 85 | * 86 | * @return void 87 | */ 88 | public static function activate() 89 | { 90 | $zshrc = new Zshrc(); 91 | $zshrc->enablePlugin('aws-upload'); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/AwsUpload/System/Rsync.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\System; 14 | 15 | use AwsUpload\System\RsyncCommands; 16 | 17 | class Rsync 18 | { 19 | /** 20 | * It contains the text version of the command to run. 21 | * 22 | * @var string 23 | */ 24 | public $cmd; 25 | 26 | /** 27 | * It contains the settings object 28 | * 29 | * Eg: 30 | * { pem , exclude, remote, local } 31 | * 32 | * @var object 33 | */ 34 | public $settings; 35 | 36 | /** 37 | * It contains the action to perform 38 | * 39 | * @see AwsUpload\System\RsyncCommands 40 | * @var string 41 | */ 42 | public $action; 43 | 44 | public $is_verbose = false; 45 | 46 | /** 47 | * Method to initiate the rsync settings object 48 | * 49 | * The setting object is a object version of one of the files in the 50 | * aws-upload folder. 51 | * 52 | * @see SettingFile::getObjcet($key) 53 | * 54 | * @param \AwsUpload\Model\Settings $settings The object version of one of the 55 | * files in the aws-upload dir. 56 | */ 57 | public function __construct(\AwsUpload\Model\Settings $settings) 58 | { 59 | $this->settings = $settings; 60 | } 61 | 62 | public function setVerbose($verbose) 63 | { 64 | $this->is_verbose = (bool) $verbose; 65 | } 66 | 67 | public function setAction($action) 68 | { 69 | $this->action = $action; 70 | } 71 | 72 | /** 73 | * Method to build the rsync command from the settings object 74 | * 75 | * @return string The rsync command. 76 | */ 77 | public function getCmd() 78 | { 79 | $cmd = ""; 80 | 81 | if (RsyncCommands::UPLOAD === $this->action) { 82 | $cmd = $this->getUploadCommand(); 83 | } 84 | 85 | if (RsyncCommands::DIFF === $this->action) { 86 | $cmd = $this->getDiffCommand(); 87 | } 88 | 89 | return $cmd; 90 | } 91 | 92 | /** 93 | * @return string 94 | */ 95 | public function getUploadCommand() 96 | { 97 | $cmd = "rsync "; 98 | $cmd .= $this->getVerboseFlags(); 99 | $cmd .= $this->getSshDetails(); 100 | $cmd .= $this->getExclude(); 101 | $cmd .= $this->getLocal(); 102 | $cmd .= $this->getRemote(); 103 | 104 | return $cmd; 105 | } 106 | 107 | /** 108 | * @return string 109 | */ 110 | public function getDiffCommand() 111 | { 112 | $cmd = "rsync --dry-run "; 113 | 114 | $cmd .= $this->getVerboseFlags(); 115 | $cmd .= $this->getSshDetails(); 116 | $cmd .= $this->getExclude(); 117 | $cmd .= $this->getOnlyPathLocal(); 118 | $cmd .= $this->getRemote(); 119 | 120 | return $cmd; 121 | } 122 | 123 | /** 124 | * @return string 125 | */ 126 | public function getVerboseFlags() 127 | { 128 | return ($this->is_verbose) ? " -v --stats --progress " : ""; 129 | } 130 | 131 | /** 132 | * @return string 133 | */ 134 | public function getSshDetails() 135 | { 136 | return "-ravze \"ssh -i " . $this->settings->pem . "\" "; 137 | } 138 | 139 | /** 140 | * @return string 141 | */ 142 | public function getExclude() 143 | { 144 | $settings = $this->settings; 145 | 146 | $cmd = ""; 147 | if (!isset($settings->exclude) || !is_array($settings->exclude)) { 148 | return $cmd; 149 | } 150 | 151 | foreach ($settings->exclude as $elem) { 152 | $cmd .= " --exclude " . escapeshellarg($elem) . " "; 153 | } 154 | 155 | $cmd .= " --exclude .DS_Store "; 156 | 157 | return $cmd; 158 | } 159 | 160 | /** 161 | * @return string 162 | */ 163 | public function getLocal() 164 | { 165 | return $this->settings->local . " "; 166 | } 167 | 168 | /** 169 | * @return string 170 | */ 171 | public function getOnlyPathLocal() 172 | { 173 | $local = trim($this->settings->local); 174 | 175 | if (strpos($local, '*') === strlen($local) - 1) { 176 | $local = substr($local, 0, -1); 177 | } 178 | 179 | return escapeshellarg($local) . " "; 180 | } 181 | 182 | /** 183 | * @return string 184 | */ 185 | public function getRemote() 186 | { 187 | return escapeshellarg($this->settings->remote) . " "; 188 | } 189 | 190 | /** 191 | * Method to run the rsync command 192 | * 193 | * @return void 194 | */ 195 | public function run() 196 | { 197 | $escaped_command = $this->getCmd(); 198 | system($escaped_command); 199 | } 200 | 201 | /** 202 | * Define if rsync is installed. 203 | * 204 | * @return bool 205 | */ 206 | public function isInstalled() 207 | { 208 | $has = exec('hash rsync 2>&1'); 209 | return (strlen($has) === 0); 210 | } 211 | } 212 | -------------------------------------------------------------------------------- /src/AwsUpload/System/RsyncCommands.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\System; 14 | 15 | interface RsyncCommands 16 | { 17 | const UPLOAD = 'UPLOAD'; 18 | const DIFF = 'DIFF'; 19 | } 20 | -------------------------------------------------------------------------------- /src/AwsUpload/System/System.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\System; 14 | 15 | class System 16 | { 17 | private static function getValue($array, $key) 18 | { 19 | return isset($array[$key]) ? $array[$key] : ''; 20 | } 21 | 22 | /** 23 | * Method to get a default editor. 24 | * 25 | * @return string 26 | */ 27 | public static function getEditor() 28 | { 29 | $editor = self::getValue($_ENV, 'EDITOR'); 30 | 31 | if (empty($editor)) { 32 | $editor = self::getValue($_SERVER, 'EDITOR'); 33 | } 34 | 35 | if (empty($editor)) { 36 | $editor = 'vim'; 37 | } 38 | 39 | return $editor; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/AwsUpload/System/Zsh.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\System; 14 | 15 | class Zsh 16 | { 17 | /** 18 | * Define if zsh is installed. 19 | * 20 | * @return bool 21 | */ 22 | public static function isInstalled() 23 | { 24 | $count = (int) exec('grep /zsh$ /etc/shells | wc -l'); 25 | 26 | return ($count >= 1); 27 | } 28 | 29 | /** 30 | * In case if not in the system. 31 | * 32 | * @return string 33 | */ 34 | public static function errorMsg() 35 | { 36 | $text = "\n It seems that zsh is not installed.\n" . 37 | " Please run (or equivalent for your system):\n\n" . 38 | " sudo apt-get install zsh\n" . 39 | " sudo chsh zsh\n"; 40 | 41 | return $text; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/AwsUpload/System/Zshrc.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright 2017 Marco Buttini 10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License 11 | */ 12 | 13 | namespace AwsUpload\System; 14 | 15 | use AwsUpload\Setting\SettingFolder; 16 | 17 | class Zshrc 18 | { 19 | /** 20 | * @var string 21 | */ 22 | public $zshrc; 23 | 24 | 25 | public function __construct() 26 | { 27 | $this->zshrc = SettingFolder::getHomeDir() . '/../.zshrc'; 28 | } 29 | 30 | /** 31 | * Activate the plugin. 32 | * 33 | * @return bool 34 | */ 35 | public function enablePlugin($plugin_name) 36 | { 37 | $zshrc_body = $this->getZshrcContent(); 38 | $zshrc_body = $this->writeInZshrcPluginVariable($zshrc_body, $plugin_name); 39 | 40 | return $this->updateZshrcContent($zshrc_body); 41 | } 42 | 43 | /** 44 | * @param string $line 45 | * @return bool 46 | */ 47 | private function isValidPluginLine($line, $plugin_name) 48 | { 49 | $trimLine = trim($line); 50 | $isCommentLine = ($trimLine[0] === '#'); 51 | $hasPluginsSyntax = (strpos($line, 'plugins=(') !== false); 52 | $hasEndParentesys = (strpos($line, ')') !== false); 53 | $pluginIsNotYetActive = (strpos($line, $plugin_name) === false); 54 | 55 | return (! $isCommentLine && 56 | $hasPluginsSyntax && 57 | $hasEndParentesys && 58 | $pluginIsNotYetActive); 59 | } 60 | 61 | /** 62 | * @param string[] $zshrc_body 63 | * @return bool 64 | */ 65 | private function hasOnePluginLine($zshrc_body) 66 | { 67 | $hasPluginLine = false; 68 | foreach ($zshrc_body as $key => $line) { 69 | $trimLine = trim($line); 70 | 71 | if (empty($trimLine)) { 72 | continue; 73 | } 74 | 75 | $isCommentLine = ($trimLine[0] === '#'); 76 | $hasPluginsSyntax = (strpos($line, 'plugins=(') !== false); 77 | $hasEndParentesys = (strpos($line, ')') !== false); 78 | 79 | $hasPluginLine = (! $isCommentLine && 80 | $hasPluginsSyntax && 81 | $hasEndParentesys) ? true : $hasPluginLine; 82 | } 83 | 84 | return $hasPluginLine; 85 | } 86 | 87 | /** 88 | * @param string[] $zshrc_body 89 | * @param string $plugin_name 90 | * @return string[] $zshrc_body 91 | */ 92 | private function writeInZshrcPluginVariable($zshrc_body, $plugin_name) 93 | { 94 | $zshrc_body = $this->attemptCaseBasicInsert($zshrc_body, $plugin_name); 95 | $zshrc_body = $this->attemptCaseNoPlugin($zshrc_body, $plugin_name); 96 | 97 | return $zshrc_body; 98 | } 99 | 100 | private function attemptCaseBasicInsert($zshrc_body, $plugin_name) 101 | { 102 | // attempt basic insert in one line 103 | foreach ($zshrc_body as $key => $line) { 104 | $trimLine = trim($line); 105 | 106 | if (empty($trimLine)) { 107 | continue; 108 | } 109 | 110 | if ($this->isValidPluginLine($line, $plugin_name)) { 111 | $zshrc_body[$key] = str_replace(')', ' ' . $plugin_name . ')', $line); 112 | } 113 | } 114 | 115 | return $zshrc_body; 116 | } 117 | 118 | private function attemptCaseNoPlugin($zshrc_body, $plugin_name) 119 | { 120 | // case no plugin line at all 121 | if (!$this->hasOnePluginLine($zshrc_body)) { 122 | $zshrc_body[] = "\n" . 'plugins=(' . $plugin_name . ')'; 123 | } 124 | 125 | return $zshrc_body; 126 | } 127 | 128 | private function getZshrcContent() 129 | { 130 | return file($this->zshrc); 131 | } 132 | 133 | private function updateZshrcContent($zshrc_body) 134 | { 135 | return file_put_contents($this->zshrc, implode('', $zshrc_body)); 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /tests/AwsUpload/AwsUploadTest.php: -------------------------------------------------------------------------------- 1 | getCmdName(); 16 | $this->assertEquals('AwsUpload\Command\EnvsCommand', $cmd); 17 | 18 | self::clearArgv(); 19 | self::pushToArgv(array('asd.php', 'envs', 'asd')); 20 | 21 | $aws = new AwsUpload(); 22 | $cmd = $aws->getCmdName(); 23 | $this->assertEquals('AwsUpload\Command\EnvsCommand', $cmd); 24 | 25 | } 26 | 27 | public function test_getCmdName_projs_withQuiet() 28 | { 29 | self::clearArgv(); 30 | self::pushToArgv(array('aws-upload', '-p', '-q', 'asd')); 31 | 32 | $aws = new AwsUpload(); 33 | $cmd = $aws->getCmdName(); 34 | $this->assertEquals('AwsUpload\Command\ProjsCommand', $cmd); 35 | $this->assertEquals(true, $aws->args->quiet); 36 | 37 | self::clearArgv(); 38 | self::pushToArgv(array('aws-upload', 'projs', '-q', 'asd')); 39 | 40 | $aws = new AwsUpload(); 41 | $cmd = $aws->getCmdName(); 42 | $this->assertEquals('AwsUpload\Command\ProjsCommand', $cmd); 43 | $this->assertEquals(true, $aws->args->quiet); 44 | } 45 | 46 | public function test_getCmdName_import() 47 | { 48 | self::clearArgv(); 49 | self::pushToArgv(array('aws-upload', '-i', './dir/blog.test.json')); 50 | 51 | $aws = new AwsUpload(); 52 | $cmd = $aws->getCmdName(); 53 | $this->assertEquals('AwsUpload\Command\ImportCommand', $cmd); 54 | 55 | self::clearArgv(); 56 | self::pushToArgv(array('aws-upload', 'import', './dir/blog.test.json')); 57 | 58 | $aws = new AwsUpload(); 59 | $cmd = $aws->getCmdName(); 60 | $this->assertEquals('AwsUpload\Command\ImportCommand', $cmd); 61 | } 62 | 63 | public function test_getCmdName_export() 64 | { 65 | self::clearArgv(); 66 | self::pushToArgv(array('aws-upload', '-ex', 'test.env', './dir/blog.test.json')); 67 | 68 | $aws = new AwsUpload(); 69 | $cmd = $aws->getCmdName(); 70 | $this->assertEquals('AwsUpload\Command\ExportCommand', $cmd); 71 | 72 | self::clearArgv(); 73 | self::pushToArgv(array('aws-upload', 'export', 'test.env', './dir/blog.test.json')); 74 | 75 | $aws = new AwsUpload(); 76 | $cmd = $aws->getCmdName(); 77 | $this->assertEquals('AwsUpload\Command\ExportCommand', $cmd); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /tests/AwsUpload/BaseTestCase.php: -------------------------------------------------------------------------------- 1 | main_aws_home = __DIR__ . '/../../aws_upload/'; 42 | $this->aws_home = $this->main_aws_home . $uid; 43 | putenv("AWSUPLOAD_HOME=" . $this->aws_home); 44 | 45 | $this->main_external = __DIR__ . '/../../external/'; 46 | $this->external = $this->main_external . $uid; 47 | 48 | $filesystem = new Filesystem(); 49 | $filesystem->mkdir($this->aws_home); 50 | $filesystem->mkdir($this->external); 51 | } 52 | 53 | /** 54 | * {@inheritdoc} 55 | */ 56 | public function tearDown() 57 | { 58 | unset($_ENV['AWSUPLOAD_HOME']); 59 | 60 | $filesystem = new Filesystem(); 61 | $filesystem->remove($this->main_aws_home); 62 | $filesystem->remove($this->main_external); 63 | } 64 | 65 | /** 66 | * Clear the $_SERVER['argv'] array 67 | */ 68 | public static function clearArgv() 69 | { 70 | $_SERVER['argv'] = array(); 71 | $_SERVER['argc'] = 0; 72 | } 73 | 74 | /** 75 | * Add one or more element(s) at the end of the $_SERVER['argv'] array 76 | * 77 | * @param string[] $args Value to add to the argv array. 78 | */ 79 | public static function pushToArgv($args) 80 | { 81 | if (is_string($args)) { 82 | $args = explode(' ', $args); 83 | } 84 | 85 | foreach ($args as $arg) { 86 | array_push($_SERVER['argv'], $arg); 87 | } 88 | 89 | $_SERVER['argc'] += count($args); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /tests/AwsUpload/Command/CheckTest.php: -------------------------------------------------------------------------------- 1 | dumpFile($this->aws_home . '/file.pem', ''); 18 | $filesystem->mkdir($this->aws_home . '/local'); 19 | $filesystem->dumpFile($this->aws_home . '/project-2.dev.json', '{ "pem" : "' . $this->aws_home . '/file.pem", "local" : "' . $this->aws_home . '/local"}'); 20 | 21 | 22 | $pem_perms = decoct(fileperms($this->aws_home . '/file.pem') & 0777); 23 | 24 | $report = array( 25 | "path" => $this->aws_home . '/project-2.dev.json', 26 | "is_valid_json" => true, 27 | "pem" => $this->aws_home . '/file.pem', 28 | "pem_exists" => true, 29 | "is_400" => false, 30 | "pem_perms" => $pem_perms, 31 | "local" => $this->aws_home . '/local', 32 | "local_exists" => true, 33 | "error_json" => '' 34 | ); 35 | 36 | $msg = CheckMessage::report($report); 37 | $msg = Output::color($msg); 38 | $this->expectOutputString($msg . "\n"); 39 | 40 | self::clearArgv(); 41 | self::pushToArgv(array('asd.php', 'check', 'project-2.dev')); 42 | 43 | $aws = new AwsUpload(); 44 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 45 | 46 | $cmd = new \AwsUpload\Command\CheckCommand($aws); 47 | $cmd->run(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /tests/AwsUpload/Command/CopyTest.php: -------------------------------------------------------------------------------- 1 | expectOutputString($msg . "\n"); 23 | 24 | self::clearArgv(); 25 | self::pushToArgv(array('asd.php', 'copy')); 26 | 27 | $aws = new AwsUpload(); 28 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 29 | 30 | $cmd = new \AwsUpload\Command\CopyCommand($aws); 31 | $cmd->run(); 32 | } 33 | 34 | // test isValidArgs 35 | public function test_noValidKey_expected_NoArgsMsg_oneParam() 36 | { 37 | $msg = CopyMessage::noArgs(); 38 | $msg = Output::color($msg); 39 | $this->expectOutputString($msg . "\n"); 40 | 41 | self::clearArgv(); 42 | self::pushToArgv(array('asd.php', 'copy', 'aaa')); 43 | 44 | $aws = new AwsUpload(); 45 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 46 | 47 | $cmd = new \AwsUpload\Command\CopyCommand($aws); 48 | $cmd->run(); 49 | } 50 | 51 | // test isValidKey 52 | public function test_noValidKey_expectedNoValidKey_first() 53 | { 54 | $msg = ErrorMessage::noValidKey('bbbb'); 55 | $msg = Output::color($msg); 56 | $this->expectOutputString($msg . "\n"); 57 | 58 | self::clearArgv(); 59 | self::pushToArgv(array('asd.php', 'copy', 'aaa', 'bbbb')); 60 | 61 | $aws = new AwsUpload(); 62 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 63 | 64 | $cmd = new \AwsUpload\Command\CopyCommand($aws); 65 | $cmd->run(); 66 | } 67 | 68 | // test isValidKey 69 | public function test_noValidKey_expectedNoValidKey_second() 70 | { 71 | $msg = ErrorMessage::noValidKey('aaa'); 72 | $msg = Output::color($msg); 73 | $this->expectOutputString($msg . "\n"); 74 | 75 | self::clearArgv(); 76 | self::pushToArgv(array('asd.php', 'copy', 'aaa', 'bbb.bbb')); 77 | 78 | $aws = new AwsUpload(); 79 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 80 | 81 | $cmd = new \AwsUpload\Command\CopyCommand($aws); 82 | $cmd->run(); 83 | } 84 | 85 | // "file_not_exists" => !Check::fileExists($source), 86 | public function test_validKeyNoExists_expectedNoFileFound() 87 | { 88 | $filesystem = new Filesystem(); 89 | $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', '{}'); 90 | 91 | $msg = ErrorMessage::noFileFound('project-2.dev'); 92 | $msg = Output::color($msg); 93 | $this->expectOutputString($msg . "\n"); 94 | 95 | self::clearArgv(); 96 | self::pushToArgv(array('asd.php', 'copy', 'project-2.dev', 'project-3.dev')); 97 | 98 | $aws = new AwsUpload(); 99 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 100 | 101 | $cmd = new \AwsUpload\Command\CopyCommand($aws); 102 | $cmd->run(); 103 | } 104 | 105 | // "file_exists" => Check::fileExists($dest), 106 | public function test_validKeyNoExists_expected_DestAlreadyExists() 107 | { 108 | $filesystem = new Filesystem(); 109 | $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', '{}'); 110 | $filesystem->dumpFile($this->aws_home . '/project-2.dev.json', '{}'); 111 | 112 | $msg = ErrorMessage::keyAlreadyExists('project-2.dev'); 113 | $msg = Output::color($msg); 114 | $this->expectOutputString($msg . "\n"); 115 | 116 | self::clearArgv(); 117 | self::pushToArgv(array('asd.php', 'copy', 'project-1.dev', 'project-2.dev')); 118 | 119 | $aws = new AwsUpload(); 120 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 121 | 122 | $cmd = new \AwsUpload\Command\CopyCommand($aws); 123 | $cmd->run(); 124 | } 125 | 126 | public function test_copyFile() 127 | { 128 | $filesystem = new Filesystem(); 129 | $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', '{"pem": "", "local":"", "remote":"", "exclude":[""]}'); 130 | 131 | 132 | $msg = NewMessage::success('project-2.dev'); 133 | $msg = Output::color($msg); 134 | $this->expectOutputString($msg . "\n"); 135 | 136 | self::clearArgv(); 137 | self::pushToArgv(array('asd.php', 'copy', 'project-1.dev', 'project-2.dev')); 138 | 139 | 140 | $aws = new AwsUpload(); 141 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 142 | 143 | $cmd = new \AwsUpload\Command\CopyCommand($aws); 144 | $cmd->run(); 145 | 146 | $settings = SettingFile::getObject('project-2.dev'); 147 | 148 | $this->assertEquals('', $settings->pem); 149 | $this->assertEquals('', $settings->local); 150 | $this->assertEquals('', $settings->remote); 151 | $this->assertEquals(array(''), $settings->exclude); 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /tests/AwsUpload/Command/DeleteTest.php: -------------------------------------------------------------------------------- 1 | expectOutputString($msg . "\n"); 20 | 21 | self::clearArgv(); 22 | self::pushToArgv(array('asd.php', 'delete')); 23 | 24 | $aws = new AwsUpload(); 25 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 26 | 27 | $cmd = new \AwsUpload\Command\DeleteCommand($aws); 28 | $cmd->run(); 29 | } 30 | 31 | public function test_noValidKey_expectedNoValidKey() 32 | { 33 | $this->expectOutputString("It seems that the key \e[33maaa\e[0m is not valid:\n\n" 34 | . "Please try to use this format:\n" 35 | . " - [project].[environmet]\n\n" 36 | . "Examples of valid key to create a new setting file:\n" 37 | . " - \e[32mmy-site.staging\e[0m\n" 38 | . " - \e[32mmy-site.dev\e[0m\n" 39 | . " - \e[32mmy-site.prod\e[0m\n\n" 40 | . "Tips on choosing the key name:\n" 41 | . " - for [project] and [environmet] try to be: short, sweet, to the point\n" 42 | . " - use only one 'dot' . in the name\n" 43 | . "\n\n"); 44 | 45 | self::clearArgv(); 46 | self::pushToArgv(array('asd.php', 'delete', 'aaa')); 47 | 48 | $aws = new AwsUpload(); 49 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 50 | 51 | $cmd = new \AwsUpload\Command\DeleteCommand($aws); 52 | $cmd->run(); 53 | } 54 | 55 | public function test_validKeyNoExists_expectedNoFileFound() 56 | { 57 | $filesystem = new Filesystem(); 58 | $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', '{}'); 59 | 60 | $msg = ErrorMessage::noFileFound('project-2.dev'); 61 | $msg = Output::color($msg); 62 | $this->expectOutputString($msg . "\n"); 63 | 64 | self::clearArgv(); 65 | self::pushToArgv(array('asd.php', 'delete', 'project-2.dev')); 66 | 67 | $aws = new AwsUpload(); 68 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 69 | 70 | $cmd = new \AwsUpload\Command\DeleteCommand($aws); 71 | $cmd->run(); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /tests/AwsUpload/Command/EditTest.php: -------------------------------------------------------------------------------- 1 | expectOutputString($msg . "\n"); 20 | 21 | self::clearArgv(); 22 | self::pushToArgv(array('asd.php', 'edit')); 23 | 24 | $aws = new AwsUpload(); 25 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 26 | 27 | $cmd = new \AwsUpload\Command\EditCommand($aws); 28 | $cmd->run(); 29 | } 30 | 31 | public function test_noValidKey_expectedNoValidKey() 32 | { 33 | $this->expectOutputString("It seems that the key \e[33maaa\e[0m is not valid:\n\n" 34 | . "Please try to use this format:\n" 35 | . " - [project].[environmet]\n\n" 36 | . "Examples of valid key to create a new setting file:\n" 37 | . " - \e[32mmy-site.staging\e[0m\n" 38 | . " - \e[32mmy-site.dev\e[0m\n" 39 | . " - \e[32mmy-site.prod\e[0m\n\n" 40 | . "Tips on choosing the key name:\n" 41 | . " - for [project] and [environmet] try to be: short, sweet, to the point\n" 42 | . " - use only one 'dot' . in the name\n" 43 | . "\n\n"); 44 | 45 | self::clearArgv(); 46 | self::pushToArgv(array('asd.php', 'edit', 'aaa')); 47 | 48 | $aws = new AwsUpload(); 49 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 50 | 51 | $cmd = new \AwsUpload\Command\EditCommand($aws); 52 | $cmd->run(); 53 | } 54 | 55 | public function test_validKeyNoExists_expectedNoFileFound() 56 | { 57 | $filesystem = new Filesystem(); 58 | $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', '{}'); 59 | 60 | $msg = ErrorMessage::noFileFound('project-2.dev'); 61 | $msg = Output::color($msg); 62 | $this->expectOutputString($msg . "\n"); 63 | 64 | self::clearArgv(); 65 | self::pushToArgv(array('asd.php', 'edit', 'project-2.dev')); 66 | 67 | $aws = new AwsUpload(); 68 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 69 | 70 | $cmd = new \AwsUpload\Command\EditCommand($aws); 71 | $cmd->run(); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /tests/AwsUpload/Command/EnvsTest.php: -------------------------------------------------------------------------------- 1 | expectOutputString($msg . "\n"); 20 | 21 | self::clearArgv(); 22 | self::pushToArgv(array('asd.php', '-e', 'proj-3')); 23 | 24 | $aws = new AwsUpload(); 25 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 26 | 27 | $cmd = new \AwsUpload\Command\EnvsCommand($aws); 28 | $cmd->run(); 29 | } 30 | 31 | public function test_moreFilesSameProj_expectedProposeAlternative() 32 | { 33 | $filesystem = new Filesystem(); 34 | $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', '{}'); 35 | $filesystem->dumpFile($this->aws_home . '/project-2.prod.json', '{}'); 36 | $filesystem->dumpFile($this->aws_home . '/project-1.staging.json', '{}'); 37 | 38 | self::clearArgv(); 39 | self::pushToArgv(array('asd.php', '-e', 'proj-3')); 40 | 41 | $error = EnvsMessage::errorNoEnvsProj('proj-3') . "\n"; 42 | $error = Output::color($error); 43 | $this->expectOutputString($error); 44 | 45 | $aws = new AwsUpload(); 46 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 47 | 48 | $cmd = new \AwsUpload\Command\EnvsCommand($aws); 49 | $cmd->run(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /tests/AwsUpload/Command/ExportTest.php: -------------------------------------------------------------------------------- 1 | expectOutputString($msg . "\n"); 22 | 23 | self::clearArgv(); 24 | self::pushToArgv(array('aws-upload', 'export')); 25 | 26 | $aws = new AwsUpload(); 27 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 28 | 29 | $cmd = new \AwsUpload\Command\ExportCommand($aws); 30 | $cmd->run(); 31 | } 32 | 33 | // test isValidArgs 34 | public function test_noValidKey_expected_NoArgsMsg_oneParam() 35 | { 36 | $msg = ErrorMessage::noValidKey('aaa'); 37 | $msg = Output::color($msg); 38 | $this->expectOutputString($msg . "\n"); 39 | 40 | $filesystem = new Filesystem(); 41 | $filesystem->dumpFile($this->aws_home . '/blog.dev.json', '{}'); 42 | 43 | self::clearArgv(); 44 | self::pushToArgv(array('asd.php', 'export', 'aaa')); 45 | 46 | $aws = new AwsUpload(); 47 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 48 | 49 | $cmd = new \AwsUpload\Command\ExportCommand($aws); 50 | $cmd->run(); 51 | } 52 | 53 | // "file_exists" => SettingFile::fileExists($dest), 54 | public function test_validKeyNoExists_expected_DestAlreadyExists() 55 | { 56 | $filesystem = new Filesystem(); 57 | $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', '{}'); 58 | $filesystem->dumpFile($this->external . '/project-1.dev.json', '{}'); 59 | 60 | $msg = 'file alreay present'; 61 | $msg = Output::color($msg); 62 | $this->expectOutputString($msg . "\n"); 63 | 64 | self::clearArgv(); 65 | self::pushToArgv(array('asd.php', 'export', 'project-1.dev', $this->external . '/')); 66 | 67 | $aws = new AwsUpload(); 68 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 69 | 70 | $cmd = new \AwsUpload\Command\ExportCommand($aws); 71 | $cmd->run(); 72 | } 73 | 74 | public function test_exportFile() 75 | { 76 | $filesystem = new Filesystem(); 77 | $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', 78 | '{"pem": "", "local":"", "remote":"", "exclude":[""]}'); 79 | 80 | $msg = ExportMessage::success('project-1.dev'); 81 | $msg = Output::color($msg); 82 | $this->expectOutputString($msg . "\n"); 83 | 84 | self::clearArgv(); 85 | self::pushToArgv(array('asd.php', 'export', 'project-1.dev', $this->external)); 86 | 87 | $aws = new AwsUpload(); 88 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 89 | 90 | $cmd = new \AwsUpload\Command\ExportCommand($aws); 91 | $cmd->run(); 92 | 93 | $settings = new Settings($this->external . '/project-1.dev.json'); 94 | 95 | $this->assertEquals('', $settings->pem); 96 | $this->assertEquals('', $settings->local); 97 | $this->assertEquals('', $settings->remote); 98 | $this->assertEquals(array(''), $settings->exclude); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /tests/AwsUpload/Command/ImportTest.php: -------------------------------------------------------------------------------- 1 | expectOutputString($msg . "\n"); 22 | 23 | self::clearArgv(); 24 | self::pushToArgv(array('aws-upload', 'import')); 25 | 26 | $aws = new AwsUpload(); 27 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 28 | 29 | $cmd = new \AwsUpload\Command\ImportCommand($aws); 30 | $cmd->run(); 31 | } 32 | 33 | // test isValidArgs 34 | public function test_noValidKey_expected_NoArgsMsg_oneParam() 35 | { 36 | $msg = ErrorMessage::noValidKey('aaa'); 37 | $msg = Output::color($msg); 38 | $this->expectOutputString($msg . "\n"); 39 | 40 | $filesystem = new Filesystem(); 41 | $filesystem->dumpFile($this->external . '/aaa.json', '{}'); 42 | 43 | self::clearArgv(); 44 | self::pushToArgv(array('asd.php', 'import', $this->external . '/aaa.json')); 45 | 46 | $aws = new AwsUpload(); 47 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 48 | 49 | $cmd = new \AwsUpload\Command\ImportCommand($aws); 50 | $cmd->run(); 51 | } 52 | 53 | public function test_validKeyNoExists_expected_DestAlreadyExists() 54 | { 55 | $filesystem = new Filesystem(); 56 | $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', '{}'); 57 | $filesystem->dumpFile($this->external . '/project-1.dev.json', '{}'); 58 | 59 | $msg = ErrorMessage::keyAlreadyExists('project-1.dev'); 60 | $msg = Output::color($msg); 61 | $this->expectOutputString($msg . "\n"); 62 | 63 | self::clearArgv(); 64 | self::pushToArgv(array('asd.php', 'import', $this->external . '/project-1.dev.json')); 65 | 66 | $aws = new AwsUpload(); 67 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 68 | 69 | $cmd = new \AwsUpload\Command\ImportCommand($aws); 70 | $cmd->run(); 71 | } 72 | 73 | public function test_importFile() 74 | { 75 | $filesystem = new Filesystem(); 76 | $filesystem->dumpFile($this->external . '/project-1.dev.json', '{"pem": "", "local":"", "remote":"", "exclude":[""]}'); 77 | 78 | 79 | $msg = ImportMessage::success('project-1.dev'); 80 | $msg = Output::color($msg); 81 | $this->expectOutputString($msg . "\n"); 82 | 83 | self::clearArgv(); 84 | self::pushToArgv(array('asd.php', 'import', $this->external . '/project-1.dev.json')); 85 | 86 | $aws = new AwsUpload(); 87 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 88 | 89 | $cmd = new \AwsUpload\Command\ImportCommand($aws); 90 | $cmd->run(); 91 | 92 | $settings = SettingFile::getObject('project-1.dev'); 93 | 94 | $this->assertEquals('', $settings->pem); 95 | $this->assertEquals('', $settings->local); 96 | $this->assertEquals('', $settings->remote); 97 | $this->assertEquals(array(''), $settings->exclude); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /tests/AwsUpload/Command/KeysTest.php: -------------------------------------------------------------------------------- 1 | expectOutputString($msg . "\n"); 21 | 22 | $aws = new AwsUpload(); 23 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 24 | 25 | $cmd = new \AwsUpload\Command\KeysCommand($aws); 26 | $cmd->run(); 27 | } 28 | 29 | public function test_oneFile_expectedProjName() 30 | { 31 | $this->expectOutputString("project-1.dev\n\n"); 32 | 33 | $filesystem = new Filesystem(); 34 | $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', '{}'); 35 | 36 | $aws = new AwsUpload(); 37 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 38 | 39 | $cmd = new \AwsUpload\Command\KeysCommand($aws); 40 | $cmd->run(); 41 | } 42 | 43 | public function test_moreFilesSameProj_expectedProjName() 44 | { 45 | $this->expectOutputString("project-1.dev project-1.prod project-1.staging\n\n"); 46 | 47 | $filesystem = new Filesystem(); 48 | $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', '{}'); 49 | $filesystem->dumpFile($this->aws_home . '/project-1.prod.json', '{}'); 50 | $filesystem->dumpFile($this->aws_home . '/project-1.staging.json', '{}'); 51 | 52 | $aws = new AwsUpload(); 53 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 54 | 55 | $cmd = new \AwsUpload\Command\KeysCommand($aws); 56 | $cmd->run(); 57 | } 58 | 59 | public function test_moreFilesDiffProj_expectedProjsName() 60 | { 61 | $this->expectOutputString("project-1.prod project-1.staging project-2.dev\n\n"); 62 | 63 | $filesystem = new Filesystem(); 64 | $filesystem->dumpFile($this->aws_home . '/project-2.dev.json', '{}'); 65 | $filesystem->dumpFile($this->aws_home . '/project-1.prod.json', '{}'); 66 | $filesystem->dumpFile($this->aws_home . '/project-1.staging.json', '{}'); 67 | 68 | $aws = new AwsUpload(); 69 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 70 | 71 | $cmd = new \AwsUpload\Command\KeysCommand($aws); 72 | $cmd->run(); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /tests/AwsUpload/Command/NewTest.php: -------------------------------------------------------------------------------- 1 | expectOutputString($msg . "\n"); 20 | 21 | self::clearArgv(); 22 | self::pushToArgv(array('asd.php', 'new')); 23 | 24 | $aws = new AwsUpload(); 25 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 26 | 27 | $cmd = new \AwsUpload\Command\NewCommand($aws); 28 | $cmd->run(); 29 | } 30 | 31 | public function test_noValidKey_expectedNoValidKey() 32 | { 33 | $this->expectOutputString("It seems that the key \e[33maaa\e[0m is not valid:\n\n" 34 | . "Please try to use this format:\n" 35 | . " - [project].[environmet]\n\n" 36 | . "Examples of valid key to create a new setting file:\n" 37 | . " - \e[32mmy-site.staging\e[0m\n" 38 | . " - \e[32mmy-site.dev\e[0m\n" 39 | . " - \e[32mmy-site.prod\e[0m\n\n" 40 | . "Tips on choosing the key name:\n" 41 | . " - for [project] and [environmet] try to be: short, sweet, to the point\n" 42 | . " - use only one 'dot' . in the name\n" 43 | . "\n" . "\n"); 44 | 45 | self::clearArgv(); 46 | self::pushToArgv(array('asd.php', 'new', 'aaa')); 47 | 48 | $aws = new AwsUpload(); 49 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 50 | 51 | $cmd = new \AwsUpload\Command\NewCommand($aws); 52 | $cmd->run(); 53 | } 54 | 55 | public function test_keyExists_expectedKeyAlreadyExists() 56 | { 57 | $filesystem = new Filesystem(); 58 | $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', '{}'); 59 | 60 | $msg = ErrorMessage::keyAlreadyExists('project-1.dev'); 61 | $msg = Output::color($msg); 62 | $this->expectOutputString($msg . "\n"); 63 | 64 | self::clearArgv(); 65 | self::pushToArgv(array('asd.php', 'new', 'project-1.dev')); 66 | 67 | $aws = new AwsUpload(); 68 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 69 | 70 | $cmd = new \AwsUpload\Command\NewCommand($aws); 71 | $cmd->run(); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /tests/AwsUpload/Command/ProjsTest.php: -------------------------------------------------------------------------------- 1 | expectOutputString($msg . "\n"); 21 | 22 | $aws = new AwsUpload(); 23 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 24 | 25 | $cmd = new \AwsUpload\Command\ProjsCommand($aws); 26 | $cmd->run(); 27 | } 28 | 29 | public function test_oneFile_expectedProjName() 30 | { 31 | $this->expectOutputString("project-1\n\n"); 32 | 33 | $filesystem = new Filesystem(); 34 | $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', '{}'); 35 | 36 | $aws = new AwsUpload(); 37 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 38 | 39 | $cmd = new \AwsUpload\Command\ProjsCommand($aws); 40 | $cmd->run(); 41 | } 42 | 43 | public function test_moreFilesSameProj_expectedProjName() 44 | { 45 | $this->expectOutputString("project-1\n\n"); 46 | 47 | $filesystem = new Filesystem(); 48 | $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', '{}'); 49 | $filesystem->dumpFile($this->aws_home . '/project-1.prod.json', '{}'); 50 | $filesystem->dumpFile($this->aws_home . '/project-1.staging.json', '{}'); 51 | 52 | $aws = new AwsUpload(); 53 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 54 | 55 | $cmd = new \AwsUpload\Command\ProjsCommand($aws); 56 | $cmd->run(); 57 | } 58 | 59 | public function test_moreFilesDiffProj_expectedProjsName() 60 | { 61 | $this->expectOutputString("project-1 project-2\n\n"); 62 | 63 | $filesystem = new Filesystem(); 64 | $filesystem->dumpFile($this->aws_home . '/project-2.dev.json', '{}'); 65 | $filesystem->dumpFile($this->aws_home . '/project-1.prod.json', '{}'); 66 | $filesystem->dumpFile($this->aws_home . '/project-1.staging.json', '{}'); 67 | 68 | $aws = new AwsUpload(); 69 | $aws->setOutput(new \AwsUpload\Io\OutputEcho()); 70 | 71 | $cmd = new \AwsUpload\Command\ProjsCommand($aws); 72 | $cmd->run(); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /tests/AwsUpload/Io/ArgsTest.php: -------------------------------------------------------------------------------- 1 | addFlags(array( 15 | 'projs' => array('p', 'projs'), 16 | 'envs' => array('e', 'envs'), 17 | 'quiet' => array('quiet', 'q'), 18 | 'version' => array('version', 'v'), 19 | )); 20 | $args->parse(); 21 | 22 | $this->assertEquals($args->envs, true); 23 | $this->assertEquals($args->quiet, true); 24 | $this->assertEquals($args->version, true); 25 | $this->assertEquals($args->proj, false); 26 | } 27 | 28 | public function test_cmds() 29 | { 30 | $args = new Args(array('e', 'P')); 31 | $args->addCmds(array( 32 | 'projs' => array('p', 'projs'), 33 | 'envs' => array('e', 'envs'), 34 | 'version' => array('version', 'v'), 35 | )); 36 | $args->parse(); 37 | 38 | $this->assertEquals($args->envs, true); 39 | $this->assertEquals($args->getParams('envs'), array('P')); 40 | $this->assertEquals($args->getFirst('envs'), 'P'); 41 | } 42 | 43 | public function test_cmd_with_flags() 44 | { 45 | $args = new Args(array('e', '-q', 'P')); 46 | $args->addCmds(array( 47 | 'projs' => array('p', 'projs'), 48 | 'envs' => array('e', 'envs'), 49 | 'version' => array('version', 'v'), 50 | )); 51 | $args->parse(); 52 | 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /tests/AwsUpload/Io/OutputTest.php: -------------------------------------------------------------------------------- 1 | red"); 14 | $this->assertEquals($redText, "\e[31mred\e[0m"); 15 | 16 | $greenText = Output::color("green"); 17 | $this->assertEquals($greenText, "\e[32mgreen\e[0m"); 18 | 19 | $yellowText = Output::color("yellow"); 20 | $this->assertEquals($yellowText, "\e[33myellow\e[0m"); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tests/AwsUpload/Setting/SettingFileTest.php: -------------------------------------------------------------------------------- 1 | assertCount(0, $list); 17 | } 18 | 19 | public function test_getList_oneFile_true() 20 | { 21 | $filesystem = new Filesystem(); 22 | $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', '{}'); 23 | 24 | $list = SettingFile::getList(); 25 | 26 | $this->assertCount(1, $list); 27 | } 28 | 29 | public function test_getList_moreFiles_true() 30 | { 31 | $filesystem = new Filesystem(); 32 | $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', '{}'); 33 | $filesystem->dumpFile($this->aws_home . '/project-1.prod.json', '{}'); 34 | $filesystem->dumpFile($this->aws_home . '/project-1.staging.json', '{}'); 35 | 36 | $list = SettingFile::getList(); 37 | 38 | $this->assertCount(3, $list); 39 | } 40 | 41 | public function test_getKeys_oneFile_true() 42 | { 43 | $filesystem = new Filesystem(); 44 | $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', '{}'); 45 | 46 | $list = SettingFile::getKeys(); 47 | 48 | $this->assertCount(1, $list); 49 | } 50 | 51 | public function test_getKeys_moreFiles_true() 52 | { 53 | $filesystem = new Filesystem(); 54 | $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', '{}'); 55 | $filesystem->dumpFile($this->aws_home . '/project-1.prod.json', '{}'); 56 | $filesystem->dumpFile($this->aws_home . '/project-1.staging.json', '{}'); 57 | 58 | $list = SettingFile::getKeys(); 59 | 60 | $this->assertCount(3, $list); 61 | } 62 | 63 | public function test_getObject_oneFile_true() 64 | { 65 | $filesystem = new Filesystem(); 66 | $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', '{"pem": "", "local":"", "remote":"", "exclude":[""]}'); 67 | 68 | $settings = SettingFile::getObject('project-1.dev'); 69 | 70 | $this->assertEquals('', $settings->pem); 71 | $this->assertEquals('', $settings->local); 72 | $this->assertEquals('', $settings->remote); 73 | $this->assertEquals(array(''), $settings->exclude); 74 | } 75 | 76 | public function test_getProjs_noProjects_true() 77 | { 78 | $projs = SettingFile::getProjs(); 79 | 80 | $this->assertEquals([], $projs); 81 | } 82 | 83 | public function testGetProjsOneFile() 84 | { 85 | $filesystem = new Filesystem(); 86 | $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', '{}'); 87 | 88 | $projs = SettingFile::getProjs(); 89 | $envs = SettingFile::getEnvs('project-1'); 90 | 91 | $this->assertEquals(['project-1'], $projs); 92 | $this->assertEquals(['dev'], $envs); 93 | } 94 | 95 | public function testGetProjsMoreFilesSameProj() 96 | { 97 | $filesystem = new Filesystem(); 98 | $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', '{}'); 99 | $filesystem->dumpFile($this->aws_home . '/project-1.prod.json', '{}'); 100 | $filesystem->dumpFile($this->aws_home . '/project-1.staging.json', '{}'); 101 | $filesystem->dumpFile($this->aws_home . '/old/project-2.staging.json', '{}'); 102 | 103 | $projs = SettingFile::getProjs(); 104 | $envs = SettingFile::getEnvs('project-1'); 105 | 106 | $this->assertEquals(['project-1'], $projs); 107 | $this->assertEquals(['dev', 'prod', 'staging'], $envs); 108 | } 109 | 110 | public function testGetProjsMoreFilesDiffProj() 111 | { 112 | $filesystem = new Filesystem(); 113 | $filesystem->dumpFile($this->aws_home . '/project-2.dev.json', '{}'); 114 | $filesystem->dumpFile($this->aws_home . '/project-1.prod.json', '{}'); 115 | $filesystem->dumpFile($this->aws_home . '/project-1.staging.json', '{}'); 116 | 117 | $projs = SettingFile::getProjs(); 118 | $envs = SettingFile::getEnvs('project-2'); 119 | 120 | $this->assertEquals(['project-1', 'project-2'], $projs); 121 | $this->assertEquals(['dev'], $envs); 122 | } 123 | 124 | public function test_noArgs() 125 | { 126 | $filesystem = new Filesystem(); 127 | $filesystem->dumpFile($this->aws_home . '/project-2.dev.json', '{}'); 128 | $filesystem->dumpFile($this->aws_home . '/project-1.prod.json', '{}'); 129 | $filesystem->dumpFile($this->aws_home . '/project-1.staging.json', '{}'); 130 | 131 | $envs = SettingFile::getEnvs(''); 132 | 133 | $this->assertEquals([], $envs); 134 | } 135 | 136 | public function test_extractProjEnv() 137 | { 138 | list($proj, $env) = SettingFile::extractProjEnv(array()); 139 | $this->assertEquals('no-project-given', $proj); 140 | $this->assertEquals('no-environment-given', $env); 141 | 142 | list($proj, $env) = SettingFile::extractProjEnv(array('a')); 143 | $this->assertEquals('no-project-given', $proj); 144 | $this->assertEquals('no-environment-given', $env); 145 | 146 | list($proj, $env) = SettingFile::extractProjEnv(array('a', 'b')); 147 | $this->assertEquals('a', $proj); 148 | $this->assertEquals('b', $env); 149 | 150 | list($proj, $env) = SettingFile::extractProjEnv(array('a.b')); 151 | $this->assertEquals('a', $proj); 152 | $this->assertEquals('b', $env); 153 | 154 | list($proj, $env) = SettingFile::extractProjEnv(array(2 => 'a', 4 => 'b')); 155 | $this->assertEquals('a', $proj); 156 | $this->assertEquals('b', $env); 157 | } 158 | 159 | /** 160 | * fileExists 161 | */ 162 | 163 | public function test_fileExists_noFile_false() 164 | { 165 | $exist = SettingFile::exists('proj.dev'); 166 | 167 | $this->assertFalse($exist); 168 | } 169 | 170 | public function test_fileExists_yesFile_true() 171 | { 172 | $filesystem = new Filesystem(); 173 | $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', '{}'); 174 | 175 | $exist = SettingFile::exists('proj.dev'); 176 | $this->assertFalse($exist); 177 | 178 | $exist = SettingFile::exists('project-1.dev'); 179 | $this->assertTrue($exist); 180 | } 181 | 182 | /** 183 | * isValidKey 184 | */ 185 | 186 | public function test_isValidKey_noDot_false() 187 | { 188 | $valid = SettingFile::isValidKey('proj'); 189 | 190 | $this->assertFalse($valid); 191 | } 192 | 193 | public function test_isValidKey_oneDot_true() 194 | { 195 | $valid = SettingFile::isValidKey('proj.env'); 196 | 197 | $this->assertTrue($valid); 198 | } 199 | 200 | public function test_isValidKey_moreDots_false() 201 | { 202 | $valid = SettingFile::isValidKey('proj.env.biz'); 203 | 204 | $this->assertFalse($valid); 205 | } 206 | } 207 | -------------------------------------------------------------------------------- /tests/AwsUpload/Setting/SettingFolderTest.php: -------------------------------------------------------------------------------- 1 | assertEquals($this->aws_home, $home); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tests/AwsUpload/System/OhMyZshTest.php: -------------------------------------------------------------------------------- 1 | aws_home . '/../.zshrc'; 14 | $zshrc_body = 'aaaa'; 15 | 16 | $filesystem = new Filesystem(); 17 | $filesystem->dumpFile($zshrc_path, $zshrc_body); 18 | 19 | $pre_content = file_get_contents($zshrc_path); 20 | $this->assertEquals($pre_content, 'aaaa'); 21 | 22 | OhMyZsh::activate(); 23 | 24 | $post_content = file_get_contents($zshrc_path); 25 | $this->assertEquals($post_content, 'aaaa' . "\n" . 'plugins=(aws-upload)'); 26 | } 27 | 28 | public function test_activate_plugin_one_line() 29 | { 30 | $zshrc_path = $this->aws_home . '/../.zshrc'; 31 | $zshrc_body = 'plugins=(git)'; 32 | 33 | $filesystem = new Filesystem(); 34 | $filesystem->dumpFile($zshrc_path, $zshrc_body); 35 | 36 | $pre_content = file_get_contents($zshrc_path); 37 | $this->assertEquals($pre_content, 'plugins=(git)'); 38 | 39 | OhMyZsh::activate(); 40 | 41 | $post_content = file_get_contents($zshrc_path); 42 | $this->assertEquals('plugins=(git aws-upload)', $post_content); 43 | } 44 | 45 | public function test_activate_plugin_one_line_has_plugin_already() 46 | { 47 | $zshrc_path = $this->aws_home . '/../.zshrc'; 48 | $zshrc_body = 'plugins=(git aws-upload)'; 49 | 50 | $filesystem = new Filesystem(); 51 | $filesystem->dumpFile($zshrc_path, $zshrc_body); 52 | 53 | $pre_content = file_get_contents($zshrc_path); 54 | $this->assertEquals($pre_content, 'plugins=(git aws-upload)'); 55 | 56 | OhMyZsh::activate(); 57 | 58 | $post_content = file_get_contents($zshrc_path); 59 | $this->assertEquals('plugins=(git aws-upload)', $post_content); 60 | } 61 | 62 | public function test_activate_plugin_do_not_touch_comments_line() 63 | { 64 | $zshrc_path = $this->aws_home . '/../.zshrc'; 65 | $zshrc_body = ' # plugins=(git)'; 66 | 67 | $filesystem = new Filesystem(); 68 | $filesystem->dumpFile($zshrc_path, $zshrc_body); 69 | 70 | $pre_content = file_get_contents($zshrc_path); 71 | $this->assertEquals($pre_content, ' # plugins=(git)'); 72 | 73 | OhMyZsh::activate(); 74 | 75 | $post_content = file_get_contents($zshrc_path); 76 | $this->assertEquals(' # plugins=(git)' . "\n" . 'plugins=(aws-upload)', 77 | $post_content); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /tests/AwsUpload/System/RsyncTest.php: -------------------------------------------------------------------------------- 1 | dumpFile($this->aws_home . '/project-1.dev.json', $json); 28 | $settings = SettingFile::getObject('project-1.dev'); 29 | 30 | $rsync = new Rsync($settings); 31 | $rsync->setAction(RsyncCommands::UPLOAD); 32 | $this->assertEquals($rsync->getCmd(), $cmd); 33 | } 34 | 35 | public function test_getExclude() 36 | { 37 | $json = '{ 38 | "pem": "/Users/jhon.doe/Documents/certificates/site.pem", 39 | "local":"/Users/jhon.doe/Documents/w/html/", 40 | "remote":"ec2-user@ec2-xx-xx-xx-xx.ap-southeast-2.compute.amazonaws.com:/var/www/html/site", 41 | "exclude":[".env", ".git/"] 42 | }'; 43 | 44 | $filesystem = new Filesystem(); 45 | $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', $json); 46 | $settings = SettingFile::getObject('project-1.dev'); 47 | 48 | $rsync = new Rsync($settings); 49 | $this->assertEquals($rsync->getExclude(), 50 | " --exclude '.env' --exclude '.git/' --exclude .DS_Store "); 51 | } 52 | 53 | public function test_buildCmd_diff() 54 | { 55 | $cmd = 'rsync --dry-run -ravze "ssh -i /Users/jhon.doe/Documents/certificates/site.pem" --exclude \'.env\' ' . 56 | '--exclude \'.git/\' --exclude .DS_Store \'/Users/jhon.doe/Documents/w/html/\' ' . 57 | '\'ec2-user@ec2-xx-xx-xx-xx.ap-southeast-2.compute.amazonaws.com:/var/www/html/site\' '; 58 | 59 | $json = '{ 60 | "pem": "/Users/jhon.doe/Documents/certificates/site.pem", 61 | "local":"/Users/jhon.doe/Documents/w/html/", 62 | "remote":"ec2-user@ec2-xx-xx-xx-xx.ap-southeast-2.compute.amazonaws.com:/var/www/html/site", 63 | "exclude":[".env", ".git/"] 64 | }'; 65 | 66 | $filesystem = new Filesystem(); 67 | $filesystem->dumpFile($this->aws_home . '/project-1.dev.json', $json); 68 | $settings = SettingFile::getObject('project-1.dev'); 69 | 70 | $rsync = new Rsync($settings); 71 | $rsync->setAction(RsyncCommands::DIFF); 72 | $this->assertEquals($rsync->getCmd(), $cmd); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /tests/AwsUpload/System/SystemTest.php: -------------------------------------------------------------------------------- 1 | assertEquals($editor, 'env_case'); 17 | } 18 | 19 | public function test_getEditor_SERVER_case() 20 | { 21 | unset($_ENV['EDITOR']); 22 | $_SERVER['EDITOR'] = 'server_case'; 23 | $editor = System::getEditor(); 24 | 25 | $this->assertEquals($editor, 'server_case'); 26 | } 27 | 28 | public function test_getEditor_default_case() 29 | { 30 | unset($_ENV['EDITOR']); 31 | unset($_SERVER['EDITOR']); 32 | $editor = System::getEditor(); 33 | 34 | $this->assertEquals($editor, 'vim'); 35 | } 36 | } 37 | --------------------------------------------------------------------------------