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.
This commit is contained in:
sHa
2025-09-27 01:44:12 +03:00
parent 5fd0e0370c
commit c359858ee9
4 changed files with 53 additions and 55 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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