From c359858ee9ba5a611514cadefecb92e0db5fb426 Mon Sep 17 00:00:00 2001 From: sHa Date: Sat, 27 Sep 2025 01:44:12 +0300 Subject: [PATCH] Replace manual confirmations with [confirm] attribute in destructive commands - Add [confirm] attribute to all destructive operations: - volumes-clean-all, volumes-remove, volumes-remove-pattern - postgres-drop-database, postgres-restore - mysql-drop-database, mysql-restore - Remove manual read -p confirmations and conditional logic - Simplifies code and uses Just's built-in confirmation system - Ensures consistent confirmation behavior across all destructive operations This provides better UX and cleaner code using Just's native features. --- README.md | 39 +++++++++++++++++++++++++++++++++++++-- mysql.just | 23 ++++++----------------- postgres.just | 24 +++++++----------------- volumes.just | 22 +++------------------- 4 files changed, 53 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index 5b9454e..08cfb16 100644 --- a/README.md +++ b/README.md @@ -27,9 +27,10 @@ import 'just-commons/container.just' # Container operations import 'just-commons/registry.just' # Registry authentication import 'just-commons/images.just' # Image operations -# Import database commands (optional) +# Import database and volume commands (optional) import 'just-commons/postgres.just' # PostgreSQL operations import 'just-commons/mysql.just' # MySQL operations +import 'just-commons/volumes.just' # Volume management ``` ## Files @@ -44,6 +45,9 @@ import 'just-commons/mysql.just' # MySQL operations - `postgres.just` - PostgreSQL operations (postgres-sql, postgres-check, postgres-list-databases, etc.) - `mysql.just` - MySQL operations (mysql-sql, mysql-check, mysql-list-databases, etc.) +### Volume Management (Optional) +- `volumes.just` - Volume operations (volumes-clean-all, volumes-remove, volumes-list, etc.) + ### Command Structure - **Container commands**: No prefix (start, stop, logs, shell, exec, status, restart) - **All other commands**: Prefixed by file type (image-*, registry-*, postgres-*, mysql-*) @@ -68,10 +72,41 @@ just registry-check just postgres-sql "SELECT version();" just postgres-check just mysql-sql "SELECT VERSION();" + +# Volume operations (volumes- prefix) +just volumes-list "myproject_*" +just volumes-clean-all +just volumes-remove "old_volume" ``` ## Requirements -- Just command runner +### Just Command Runner + +This library requires **Just 1.14.0 or later** to support the `group` attribute for organized command display. + +#### Installation on Ubuntu 24.04 LTS + +**Method 1: Snap (Recommended for latest version)** +```bash +sudo snap install just --classic +``` + +**Method 2: Manual Installation from GitHub** +```bash +# Get the latest version +JUST_VERSION=$(curl -s "https://api.github.com/repos/casey/just/releases/latest" | grep -Po '"tag_name": "\K[0-9.]+') + +# Download and install +wget -qO just.tar.gz "https://github.com/casey/just/releases/latest/download/just-${JUST_VERSION}-x86_64-unknown-linux-musl.tar.gz" +tar -xzf just.tar.gz +sudo mv just /usr/local/bin/ +chmod +x /usr/local/bin/just + +# Verify installation +just --version +``` + +### Other Requirements - Docker or Podman - Git diff --git a/mysql.just b/mysql.just index f080021..da99c37 100644 --- a/mysql.just +++ b/mysql.just @@ -128,6 +128,7 @@ mysql-create-database database service="mysql" compose-file="": fi # Drop MySQL database +[confirm] mysql-drop-database database service="mysql" compose-file="": #!/usr/bin/env bash set -euo pipefail @@ -149,18 +150,12 @@ mysql-drop-database database service="mysql" compose-file="": fi echo -e "{{YELLOW}}WARNING: This will permanently delete database: $database{{NC}}" - read -p "Are you sure? (y/N): " -n 1 -r - echo - if [[ $REPLY =~ ^[Yy]$ ]]; then - echo -e "{{BLUE}}Dropping MySQL database: $database{{NC}}" + echo -e "{{BLUE}}Dropping MySQL database: $database{{NC}}" - if [ -n "$file_arg" ]; then - just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD} -e 'DROP DATABASE \`$database\`;'" "$file_arg" - else - just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD} -e 'DROP DATABASE \`$database\`;'" - fi + if [ -n "$file_arg" ]; then + just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD} -e 'DROP DATABASE \`$database\`;'" "$file_arg" else - echo "Operation cancelled" + just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD} -e 'DROP DATABASE \`$database\`;'" fi # Create MySQL user @@ -247,6 +242,7 @@ mysql-shell service="mysql" compose-file="": # Restore MySQL database from backup file [group('database')] +[confirm] mysql-restore backup_file database service="mysql" compose-file="" backup_path="./backups": #!/usr/bin/env bash set -euo pipefail @@ -302,13 +298,6 @@ mysql-restore backup_file database service="mysql" compose-file="" backup_path=" echo -e "{{YELLOW}}Make sure you have a backup of current data if needed{{NC}}" echo "" - read -p "Are you sure you want to restore '$database' from '$backup_file'? Type 'yes' to continue: " confirm - - if [ "$confirm" != "yes" ]; then - echo "Restore cancelled" - exit 1 - fi - echo -e "{{BLUE}}Restoring database '$database' from $backup_file...{{NC}}" # Restore database from backup file diff --git a/postgres.just b/postgres.just index d1b8e4a..4fd038b 100644 --- a/postgres.just +++ b/postgres.just @@ -131,6 +131,8 @@ postgres-create-database database service="postgres" compose-file="": fi # Drop PostgreSQL database +[group('database')] +[confirm] postgres-drop-database database service="postgres" compose-file="": #!/usr/bin/env bash set -euo pipefail @@ -152,18 +154,12 @@ postgres-drop-database database service="postgres" compose-file="": fi echo -e "{{YELLOW}}WARNING: This will permanently delete database: $database{{NC}}" - read -p "Are you sure? (y/N): " -n 1 -r - echo - if [[ $REPLY =~ ^[Yy]$ ]]; then - echo -e "{{BLUE}}Dropping PostgreSQL database: $database{{NC}}" + echo -e "{{BLUE}}Dropping PostgreSQL database: $database{{NC}}" - if [ -n "$file_arg" ]; then - just exec "$service" "dropdb -U postgres \"$database\"" "$file_arg" - else - just exec "$service" "dropdb -U postgres \"$database\"" - fi + if [ -n "$file_arg" ]; then + just exec "$service" "dropdb -U postgres \"$database\"" "$file_arg" else - echo "Operation cancelled" + just exec "$service" "dropdb -U postgres \"$database\"" fi # PostgreSQL interactive shell @@ -190,6 +186,7 @@ postgres-shell service="postgres" compose-file="": # Restore PostgreSQL database from backup file [group('database')] +[confirm] postgres-restore backup_file service="postgres" compose-file="" backup_path="./backups": #!/usr/bin/env bash set -euo pipefail @@ -243,13 +240,6 @@ postgres-restore backup_file service="postgres" compose-file="" backup_path="./b echo -e "{{YELLOW}}Make sure you have a backup of current data if needed{{NC}}" echo "" - read -p "Are you sure you want to restore from '$backup_file'? Type 'yes' to continue: " confirm - - if [ "$confirm" != "yes" ]; then - echo "Restore cancelled" - exit 1 - fi - echo -e "{{BLUE}}Restoring database from $backup_file...{{NC}}" # Copy backup file to container and restore diff --git a/volumes.just b/volumes.just index 9acbe3b..f1af093 100644 --- a/volumes.just +++ b/volumes.just @@ -2,6 +2,7 @@ # Clean all volumes used by compose file (DESTRUCTIVE!) [group('volumes')] +[confirm] volumes-clean-all compose-file="": #!/usr/bin/env bash set -euo pipefail @@ -23,13 +24,6 @@ volumes-clean-all compose-file="": $compose_cmd $file_arg config --volumes 2>/dev/null || echo "Cannot list volumes from compose file" echo "" - read -p "Are you sure? Type 'yes' to continue: " confirm - - if [ "$confirm" != "yes" ]; then - echo "Aborted" - exit 1 - fi - echo -e "{{BLUE}}Stopping services...{{NC}}" $compose_cmd $file_arg down @@ -40,6 +34,7 @@ volumes-clean-all compose-file="": # Remove specific volume by name (DESTRUCTIVE!) [group('volumes')] +[confirm] volumes-remove volume_name: #!/usr/bin/env bash set -euo pipefail @@ -62,12 +57,6 @@ volumes-remove volume_name: echo -e "{{RED}}⚠️ WARNING: This will DELETE VOLUME DATA!{{NC}}" echo -e "{{YELLOW}}This will remove volume: $volume_name{{NC}}" - read -p "Are you sure? Type 'yes' to continue: " confirm - - if [ "$confirm" != "yes" ]; then - echo "Aborted" - exit 1 - fi echo -e "{{BLUE}}Removing volume: $volume_name{{NC}}" $runtime volume rm "$volume_name" 2>/dev/null || true @@ -76,6 +65,7 @@ volumes-remove volume_name: # Remove volumes matching pattern (DESTRUCTIVE!) [group('volumes')] +[confirm] volumes-remove-pattern pattern: #!/usr/bin/env bash set -euo pipefail @@ -102,12 +92,6 @@ volumes-remove-pattern pattern: echo -e "{{YELLOW}}Volumes matching pattern '$pattern':{{NC}}" echo "$matching_volumes" echo "" - read -p "Are you sure? Type 'yes' to continue: " confirm - - if [ "$confirm" != "yes" ]; then - echo "Aborted" - exit 1 - fi echo -e "{{BLUE}}Removing matching volumes...{{NC}}" echo "$matching_volumes" | while IFS= read -r volume; do