From f9ae6b7bf2a4c0ee59c10ea441cd44912f3aa389 Mon Sep 17 00:00:00 2001 From: Xaq Rothman Date: Wed, 22 Mar 2023 12:59:13 -0400 Subject: [PATCH 1/2] Improved error checking --- wp-cli-sync.php | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/wp-cli-sync.php b/wp-cli-sync.php index 8685eda..9e2c515 100644 --- a/wp-cli-sync.php +++ b/wp-cli-sync.php @@ -69,9 +69,6 @@ function lb_cr() { $ssh_hostname = $_ENV['REMOTE_SSH_HOSTNAME']; $ssh_username = $_ENV['REMOTE_SSH_USERNAME']; $rem_proj_loc = $_ENV['REMOTE_PROJECT_DIR']; - $ssh_port = $_ENV['REMOTE_PORT'] ?? false; - $ssh_port_command = $ssh_port ? ' -p ' . $ssh_port . ' ' : ''; - $rsync_port_command = $ssh_port ? ' -e "ssh -p 18765" ' : ''; // Welcome task_message('Running .env file and connection checks...', 'WP-CLI Sync', 97); @@ -80,11 +77,22 @@ function lb_cr() { * BEGIN VAR / CONNECTION CHECKS */ - // Exit if some vars missing if (empty($ssh_hostname) || empty($ssh_username) || empty($rem_proj_loc)) { + $message = 'Missing vars in .env: '; + $missing = []; + foreach([ + 'REMOTE_SSH_HOSTNAME' => $ssh_hostname, + 'REMOTE_SSH_USERNAME' => $ssh_username, + 'REMOTE_PROJECT_DIR' => $rem_proj_loc, + ] as $env_name => $env_value) { + if (empty($env_value)) { + $missing[] = $env_name; + } + } + $message .= implode(', ', $missing); // Exit Messages - task_message('some/all local sync vars are not set in .env file', 'Error', 31, false); + task_message($message, 'Error', 31, false); // Line Break + Color Reset + Exit lb_cr(); @@ -93,7 +101,7 @@ function lb_cr() { } // Check if Remote location formatted correctly - if(($rem_proj_loc[0] != '/') && ($rem_proj_loc[0] != '~')) { + if(preg_match('/^~?\//', $rem_proj_loc)) { // Exit Messages task_message('Incorrect formatting of the REMOTE_PROJECT_DIR variable', 'Error', 31, false); @@ -103,22 +111,12 @@ function lb_cr() { lb_cr(); exit(); - } elseif($rem_proj_loc[0] == '~') { - - if($rem_proj_loc[1] != '/') { - - // Exit Messages - task_message('Incorrect formatting of the REMOTE_PROJECT_DIR variable', 'Error', 31, false); - task_message('Ensure that the path begins with either / or ~/', 'Hint', 33); - - // Line Break + Color Reset + Exit - lb_cr(); - exit(); - - } - } + $ssh_port = $_ENV['REMOTE_PORT'] ?? false; + $ssh_port_command = $ssh_port ? ' -p ' . $ssh_port . ' ' : ''; + $rsync_port_command = $ssh_port ? ' -e "ssh -p 18765" ' : ''; + // Check if SSH connection works $command = 'ssh ' . $ssh_port_command . ' -q '.$ssh_username.'@'.$ssh_hostname.' exit; echo $?'; $remote_server_status = exec($command); From 0a784c68452d1df6594b9b11667df5f308809823 Mon Sep 17 00:00:00 2001 From: Xaq Rothman Date: Wed, 22 Mar 2023 13:09:15 -0400 Subject: [PATCH 2/2] Forgot a negation. --- wp-cli-sync.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-cli-sync.php b/wp-cli-sync.php index 9e2c515..5dbaacd 100644 --- a/wp-cli-sync.php +++ b/wp-cli-sync.php @@ -101,7 +101,7 @@ function lb_cr() { } // Check if Remote location formatted correctly - if(preg_match('/^~?\//', $rem_proj_loc)) { + if(!preg_match('/^~?\//', $rem_proj_loc)) { // Exit Messages task_message('Incorrect formatting of the REMOTE_PROJECT_DIR variable', 'Error', 31, false);