From 959c8abf71f4af2e6e453838340a092a430c7828 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Tue, 18 Aug 2026 11:21:49 +0545 Subject: [PATCH] Add confirmation --- .distignore | 18 ++++++++ .gitattributes | 10 ++++ CHANGELOG.md | 8 ++++ LICENSE | 21 +++++++++ features/database-command-basic.feature | 61 +++++++++++++++++++++---- features/database-command-reset.feature | 30 ++++++------ src/DatabaseCommand.php | 7 ++- 7 files changed, 130 insertions(+), 25 deletions(-) create mode 100644 .distignore create mode 100644 .gitattributes create mode 100644 CHANGELOG.md create mode 100644 LICENSE diff --git a/.distignore b/.distignore new file mode 100644 index 0000000..2f9c78d --- /dev/null +++ b/.distignore @@ -0,0 +1,18 @@ +.DS_Store +.git +.gitignore +.gitlab-ci.yml +.editorconfig +.travis.yml +behat.yml +circle.yml +phpcs.xml.dist +phpunit.xml.dist +bin/ +features/ +utils/ +*.zip +*.tar.gz +*.swp +*.txt +*.log diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..ac8a7c5 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,10 @@ +/.claude export-ignore +/.editorconfig export-ignore +/.gherkin-lintrc export-ignore +/.github export-ignore +/.gitignore export-ignore +/behat.yml export-ignore +/features export-ignore +/phpcs.xml.dist export-ignore +/phpstan.neon.dist export-ignore +/wp-cli.yml export-ignore diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..08cd496 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,8 @@ +# Changelog + +## 2.0.0 - 2026-08-18 +- Added: `--yes` flag to skip the confirmation prompt +- Changed: require WP-CLI v3.0 or greater + +## 1.0.0 - 2016-05-06 +- Initial release diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b792f88 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (C) 2026 Nilambar Sharma + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/features/database-command-basic.feature b/features/database-command-basic.feature index 7e4729d..e1602e6 100644 --- a/features/database-command-basic.feature +++ b/features/database-command-basic.feature @@ -51,7 +51,7 @@ Feature: Basic tests When I run `wp user create firstadmin firstadmin@gmail.com --role=administrator` And I run `wp user create secondadmin secondadmin@gmail.com --role=administrator` - And I run `wp database reset --author=secondadmin` + And I run `wp database reset --author=secondadmin --yes` And I run `wp user list --role=administrator --format=count` Then STDOUT should be: """ @@ -62,7 +62,7 @@ Feature: Basic tests Given a WP install When I run `wp user create admin.name admin.name@gmail.com --role=administrator` - And I run `wp database reset --author=admin.name` + And I run `wp database reset --author=admin.name --yes` Then STDOUT should contain: """ Success: Database is reset successfully. @@ -80,7 +80,7 @@ Feature: Basic tests """ When I run `wp user create installfailadmin installfailadmin@gmail.com --role=administrator` - And I try `wp database reset --author=installfailadmin` + And I try `wp database reset --author=installfailadmin --yes` Then STDERR should contain: """ Error: Reset failed @@ -92,7 +92,7 @@ Feature: Basic tests When I run `wp role create customadmin "Custom Admin"` And I run `wp user create customadminuser customadminuser@gmail.com --role=customadmin` And I run `wp eval 'get_role( "customadmin" )->add_cap( "manage_options" );'` - And I run `wp database reset --author=customadminuser` + And I run `wp database reset --author=customadminuser --yes` Then STDOUT should contain: """ Success: Database is reset successfully. @@ -102,8 +102,8 @@ Feature: Basic tests Given a WP install When I run `wp user create repeatadmin repeatadmin@gmail.com --role=administrator` - And I run `wp database reset --author=repeatadmin` - And I run `wp database reset --author=repeatadmin` + And I run `wp database reset --author=repeatadmin --yes` + And I run `wp database reset --author=repeatadmin --yes` Then STDOUT should contain: """ Success: Database is reset successfully. @@ -132,7 +132,7 @@ Feature: Basic tests Given a WP install When I run `wp user create testadmin testadmin@gmail.com --role=administrator` - And I run `wp database reset --author=testadmin` + And I run `wp database reset --author=testadmin --yes` Then STDOUT should contain: """ Success: Database is reset successfully. @@ -151,7 +151,7 @@ Feature: Basic tests Given a WP install When I run `wp user create spacedadmin spacedadmin@gmail.com --role=administrator` - And I run `wp database reset --author=" spacedadmin "` + And I run `wp database reset --author=" spacedadmin " --yes` Then STDOUT should contain: """ Success: Database is reset successfully. @@ -162,7 +162,7 @@ Feature: Basic tests Given a WP install When I run `wp user create caseadmin caseadmin@gmail.com --role=administrator` - And I run `wp database reset --author=CASEADMIN` + And I run `wp database reset --author=CASEADMIN --yes` Then STDOUT should contain: """ Success: Database is reset successfully. @@ -219,3 +219,46 @@ Feature: Basic tests """ --author """ + + Scenario: Test help shows yes parameter + Given an empty directory + + When I try `PAGER= wp help database reset` + Then STDOUT should contain: + """ + --yes + """ + + Scenario: Test reset prompts for confirmation when --yes is not passed + Given a WP install + + When I run `wp user create noyesadmin noyesadmin@gmail.com --role=administrator` + And I run `wp database reset --author=noyesadmin` + Then STDOUT should contain: + """ + Are you sure you want to reset the database? [y/n] + """ + And STDOUT should not contain: + """ + Success: Database is reset successfully. + """ + + When I run `wp user get noyesadmin --field=login` + Then STDOUT should be: + """ + noyesadmin + """ + + Scenario: Test reset proceeds without prompting when --yes is passed + Given a WP install + + When I run `wp user create yesadmin yesadmin@gmail.com --role=administrator` + And I run `wp database reset --author=yesadmin --yes` + Then STDOUT should not contain: + """ + [y/n] + """ + And STDOUT should contain: + """ + Success: Database is reset successfully. + """ diff --git a/features/database-command-reset.feature b/features/database-command-reset.feature index c022747..57d5cb6 100644 --- a/features/database-command-reset.feature +++ b/features/database-command-reset.feature @@ -4,7 +4,7 @@ Feature: Test reset behaviour Given a WP install When I run `wp user create testadmin2 testadmin2@gmail.com --role=administrator` - And I run `wp database reset --author=testadmin2` + And I run `wp database reset --author=testadmin2 --yes` And I run `wp user list --role=administrator --format=count` Then STDOUT should be: """ @@ -12,7 +12,7 @@ Feature: Test reset behaviour """ When I run `wp user create testadmin3 testadmin3@gmail.com --role=administrator` - And I run `wp database reset --author=testadmin3` + And I run `wp database reset --author=testadmin3 --yes` And I run `wp user get testadmin3 --field=email` Then STDOUT should be: """ @@ -22,7 +22,7 @@ Feature: Test reset behaviour When I run `wp user create testadmin4 testadmin4@gmail.com --role=administrator --user_pass=testpass@1234` And I run `wp user get testadmin4 --field=user_pass` And save STDOUT as {USER_PASS} - And I run `wp database reset --author=testadmin4` + And I run `wp database reset --author=testadmin4 --yes` And I run `wp user get testadmin4 --field=user_pass` Then STDOUT should be: """ @@ -31,7 +31,7 @@ Feature: Test reset behaviour When I run `wp user create testadmin5 testadmin5@gmail.com --role=administrator` And I run `wp post generate --count=10` - And I run `wp database reset --author=testadmin5` + And I run `wp database reset --author=testadmin5 --yes` And I run `wp post list --format=count` Then STDOUT should be: """ @@ -41,7 +41,7 @@ Feature: Test reset behaviour When I run `wp user create testadmin6 testadmin6@gmail.com --role=administrator` And I run `wp option get blogname` And save STDOUT as {BLOG_NAME} - And I run `wp database reset --author=testadmin6` + And I run `wp database reset --author=testadmin6 --yes` And I run `wp option get blogname` Then STDOUT should be: """ @@ -50,7 +50,7 @@ Feature: Test reset behaviour When I run `wp user create testadmin7 testadmin7@gmail.com --role=administrator` And I run `wp user generate --count=10` - And I run `wp database reset --author=testadmin7` + And I run `wp database reset --author=testadmin7 --yes` And I run `wp user list --format=count` Then STDOUT should be: """ @@ -59,7 +59,7 @@ Feature: Test reset behaviour When I run `wp user create testadmin8 testadmin8@gmail.com --role=administrator` And I run `wp term generate category --count=10` - And I run `wp database reset --author=testadmin8` + And I run `wp database reset --author=testadmin8 --yes` And I run `wp term list category --format=count` Then STDOUT should be: """ @@ -70,7 +70,7 @@ Feature: Test reset behaviour And save STDOUT as {SAMPLE_POST_ID} And I run `wp comment generate --post_id={SAMPLE_POST_ID}` And I run `wp user create testadmin9 testadmin9@gmail.com --role=administrator` - And I run `wp database reset --author=testadmin9` + And I run `wp database reset --author=testadmin9 --yes` And I run `wp comment list --format=count` Then STDOUT should be: """ @@ -80,7 +80,7 @@ Feature: Test reset behaviour When I run `wp user create testadmin10 testadmin10@gmail.com --role=administrator` And I run `wp option get siteurl` And save STDOUT as {SITE_URL} - And I run `wp database reset --author=testadmin10` + And I run `wp database reset --author=testadmin10 --yes` And I run `wp option get siteurl` Then STDOUT should be: """ @@ -92,7 +92,7 @@ Feature: Test reset behaviour When I run `wp user create testadmin_bp testadmin_bp@gmail.com --role=administrator` And I run `wp option set blog_public 0` - And I run `wp database reset --author=testadmin_bp` + And I run `wp database reset --author=testadmin_bp --yes` And I run `wp option get blog_public` Then STDOUT should be: """ @@ -105,7 +105,7 @@ Feature: Test reset behaviour When I run `wp user create testadmin_home testadmin_home@gmail.com --role=administrator` And I run `wp option set home https://example-home.test` And I run `wp option set siteurl https://example-site.test` - And I run `wp database reset --author=testadmin_home` + And I run `wp database reset --author=testadmin_home --yes` And I run `wp option get home` Then STDOUT should be: """ @@ -124,7 +124,7 @@ Feature: Test reset behaviour When I run `wp db query "CREATE TABLE wp_custom_test_reset (id INT PRIMARY KEY)"` And I run `wp db query "CREATE TABLE custom_noprefix_reset (id INT PRIMARY KEY)"` And I run `wp user create testadmin_ct testadmin_ct@gmail.com --role=administrator` - And I run `wp database reset --author=testadmin_ct` + And I run `wp database reset --author=testadmin_ct --yes` And I run `wp db query "SHOW TABLES LIKE 'wp_custom_test_reset'" --skip-column-names` Then STDOUT should not contain: """ @@ -140,9 +140,9 @@ Feature: Test reset behaviour Given a WP install When I run `wp user create adminA adminA@gmail.com --role=administrator` - And I run `wp database reset --author=adminA` + And I run `wp database reset --author=adminA --yes` And I run `wp user create adminB adminB@gmail.com --role=administrator` - And I run `wp database reset --author=adminB` + And I run `wp database reset --author=adminB --yes` And I try `wp user get adminA --field=login` Then STDERR should contain: """ @@ -156,7 +156,7 @@ Feature: Test reset behaviour When I run `wp user create testadmin_meta testadmin_meta@gmail.com --role=administrator` And I run `wp eval 'update_user_meta( get_user_by( "login", "testadmin_meta" )->ID, "default_password_nag", true );'` And I run `wp db query "UPDATE wp_users SET user_activation_key='testkey123' WHERE user_login='testadmin_meta'"` - And I run `wp database reset --author=testadmin_meta` + And I run `wp database reset --author=testadmin_meta --yes` And I run `wp db query "SELECT user_activation_key FROM wp_users WHERE user_login='testadmin_meta'" --skip-column-names` Then STDOUT should not contain: """ diff --git a/src/DatabaseCommand.php b/src/DatabaseCommand.php index f472ed1..8ba8ad2 100644 --- a/src/DatabaseCommand.php +++ b/src/DatabaseCommand.php @@ -14,11 +14,14 @@ class DatabaseCommand extends WP_CLI_Command { * * --author= * : Administrator user you want to keep after reset. + * + * [--yes] + * : Answer yes to the confirmation message. * * ## EXAMPLES * * # Reset database and keep `admin` user. - * $ wp database reset --author=admin + * $ wp database reset --author=admin --yes * * @since 1.0.0 * @@ -61,6 +64,8 @@ public function reset( $args, $assoc_args ) { WP_CLI::error( 'User is not administrator.' ); } + WP_CLI::confirm( 'Are you sure you want to reset the database?', $assoc_args ); + $this->reset_callback( $author_obj ); }