mirror of
https://github.com/shadoll/just-commons.git
synced 2025-12-20 03:26:43 +00:00
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:
39
README.md
39
README.md
@@ -27,9 +27,10 @@ import 'just-commons/container.just' # Container operations
|
|||||||
import 'just-commons/registry.just' # Registry authentication
|
import 'just-commons/registry.just' # Registry authentication
|
||||||
import 'just-commons/images.just' # Image operations
|
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/postgres.just' # PostgreSQL operations
|
||||||
import 'just-commons/mysql.just' # MySQL operations
|
import 'just-commons/mysql.just' # MySQL operations
|
||||||
|
import 'just-commons/volumes.just' # Volume management
|
||||||
```
|
```
|
||||||
|
|
||||||
## Files
|
## Files
|
||||||
@@ -44,6 +45,9 @@ import 'just-commons/mysql.just' # MySQL operations
|
|||||||
- `postgres.just` - PostgreSQL operations (postgres-sql, postgres-check, postgres-list-databases, etc.)
|
- `postgres.just` - PostgreSQL operations (postgres-sql, postgres-check, postgres-list-databases, etc.)
|
||||||
- `mysql.just` - MySQL operations (mysql-sql, mysql-check, mysql-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
|
### Command Structure
|
||||||
- **Container commands**: No prefix (start, stop, logs, shell, exec, status, restart)
|
- **Container commands**: No prefix (start, stop, logs, shell, exec, status, restart)
|
||||||
- **All other commands**: Prefixed by file type (image-*, registry-*, postgres-*, mysql-*)
|
- **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-sql "SELECT version();"
|
||||||
just postgres-check
|
just postgres-check
|
||||||
just mysql-sql "SELECT VERSION();"
|
just mysql-sql "SELECT VERSION();"
|
||||||
|
|
||||||
|
# Volume operations (volumes- prefix)
|
||||||
|
just volumes-list "myproject_*"
|
||||||
|
just volumes-clean-all
|
||||||
|
just volumes-remove "old_volume"
|
||||||
```
|
```
|
||||||
|
|
||||||
## Requirements
|
## 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
|
- Docker or Podman
|
||||||
- Git
|
- Git
|
||||||
|
|||||||
23
mysql.just
23
mysql.just
@@ -128,6 +128,7 @@ mysql-create-database database service="mysql" compose-file="":
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Drop MySQL database
|
# Drop MySQL database
|
||||||
|
[confirm]
|
||||||
mysql-drop-database database service="mysql" compose-file="":
|
mysql-drop-database database service="mysql" compose-file="":
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
@@ -149,18 +150,12 @@ mysql-drop-database database service="mysql" compose-file="":
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
echo -e "{{YELLOW}}WARNING: This will permanently delete database: $database{{NC}}"
|
echo -e "{{YELLOW}}WARNING: This will permanently delete database: $database{{NC}}"
|
||||||
read -p "Are you sure? (y/N): " -n 1 -r
|
echo -e "{{BLUE}}Dropping MySQL database: $database{{NC}}"
|
||||||
echo
|
|
||||||
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
|
||||||
echo -e "{{BLUE}}Dropping MySQL database: $database{{NC}}"
|
|
||||||
|
|
||||||
if [ -n "$file_arg" ]; then
|
if [ -n "$file_arg" ]; then
|
||||||
just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD} -e 'DROP DATABASE \`$database\`;'" "$file_arg"
|
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
|
|
||||||
else
|
else
|
||||||
echo "Operation cancelled"
|
just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD} -e 'DROP DATABASE \`$database\`;'"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Create MySQL user
|
# Create MySQL user
|
||||||
@@ -247,6 +242,7 @@ mysql-shell service="mysql" compose-file="":
|
|||||||
|
|
||||||
# Restore MySQL database from backup file
|
# Restore MySQL database from backup file
|
||||||
[group('database')]
|
[group('database')]
|
||||||
|
[confirm]
|
||||||
mysql-restore backup_file database service="mysql" compose-file="" backup_path="./backups":
|
mysql-restore backup_file database service="mysql" compose-file="" backup_path="./backups":
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -euo pipefail
|
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 -e "{{YELLOW}}Make sure you have a backup of current data if needed{{NC}}"
|
||||||
echo ""
|
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}}"
|
echo -e "{{BLUE}}Restoring database '$database' from $backup_file...{{NC}}"
|
||||||
|
|
||||||
# Restore database from backup file
|
# Restore database from backup file
|
||||||
|
|||||||
@@ -131,6 +131,8 @@ postgres-create-database database service="postgres" compose-file="":
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Drop PostgreSQL database
|
# Drop PostgreSQL database
|
||||||
|
[group('database')]
|
||||||
|
[confirm]
|
||||||
postgres-drop-database database service="postgres" compose-file="":
|
postgres-drop-database database service="postgres" compose-file="":
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
@@ -152,18 +154,12 @@ postgres-drop-database database service="postgres" compose-file="":
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
echo -e "{{YELLOW}}WARNING: This will permanently delete database: $database{{NC}}"
|
echo -e "{{YELLOW}}WARNING: This will permanently delete database: $database{{NC}}"
|
||||||
read -p "Are you sure? (y/N): " -n 1 -r
|
echo -e "{{BLUE}}Dropping PostgreSQL database: $database{{NC}}"
|
||||||
echo
|
|
||||||
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
|
||||||
echo -e "{{BLUE}}Dropping PostgreSQL database: $database{{NC}}"
|
|
||||||
|
|
||||||
if [ -n "$file_arg" ]; then
|
if [ -n "$file_arg" ]; then
|
||||||
just exec "$service" "dropdb -U postgres \"$database\"" "$file_arg"
|
just exec "$service" "dropdb -U postgres \"$database\"" "$file_arg"
|
||||||
else
|
|
||||||
just exec "$service" "dropdb -U postgres \"$database\""
|
|
||||||
fi
|
|
||||||
else
|
else
|
||||||
echo "Operation cancelled"
|
just exec "$service" "dropdb -U postgres \"$database\""
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# PostgreSQL interactive shell
|
# PostgreSQL interactive shell
|
||||||
@@ -190,6 +186,7 @@ postgres-shell service="postgres" compose-file="":
|
|||||||
|
|
||||||
# Restore PostgreSQL database from backup file
|
# Restore PostgreSQL database from backup file
|
||||||
[group('database')]
|
[group('database')]
|
||||||
|
[confirm]
|
||||||
postgres-restore backup_file service="postgres" compose-file="" backup_path="./backups":
|
postgres-restore backup_file service="postgres" compose-file="" backup_path="./backups":
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -euo pipefail
|
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 -e "{{YELLOW}}Make sure you have a backup of current data if needed{{NC}}"
|
||||||
echo ""
|
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}}"
|
echo -e "{{BLUE}}Restoring database from $backup_file...{{NC}}"
|
||||||
|
|
||||||
# Copy backup file to container and restore
|
# Copy backup file to container and restore
|
||||||
|
|||||||
22
volumes.just
22
volumes.just
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
# Clean all volumes used by compose file (DESTRUCTIVE!)
|
# Clean all volumes used by compose file (DESTRUCTIVE!)
|
||||||
[group('volumes')]
|
[group('volumes')]
|
||||||
|
[confirm]
|
||||||
volumes-clean-all compose-file="":
|
volumes-clean-all compose-file="":
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -euo pipefail
|
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"
|
$compose_cmd $file_arg config --volumes 2>/dev/null || echo "Cannot list volumes from compose file"
|
||||||
echo ""
|
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}}"
|
echo -e "{{BLUE}}Stopping services...{{NC}}"
|
||||||
$compose_cmd $file_arg down
|
$compose_cmd $file_arg down
|
||||||
|
|
||||||
@@ -40,6 +34,7 @@ volumes-clean-all compose-file="":
|
|||||||
|
|
||||||
# Remove specific volume by name (DESTRUCTIVE!)
|
# Remove specific volume by name (DESTRUCTIVE!)
|
||||||
[group('volumes')]
|
[group('volumes')]
|
||||||
|
[confirm]
|
||||||
volumes-remove volume_name:
|
volumes-remove volume_name:
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
@@ -62,12 +57,6 @@ volumes-remove volume_name:
|
|||||||
|
|
||||||
echo -e "{{RED}}⚠️ WARNING: This will DELETE VOLUME DATA!{{NC}}"
|
echo -e "{{RED}}⚠️ WARNING: This will DELETE VOLUME DATA!{{NC}}"
|
||||||
echo -e "{{YELLOW}}This will remove volume: $volume_name{{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}}"
|
echo -e "{{BLUE}}Removing volume: $volume_name{{NC}}"
|
||||||
$runtime volume rm "$volume_name" 2>/dev/null || true
|
$runtime volume rm "$volume_name" 2>/dev/null || true
|
||||||
@@ -76,6 +65,7 @@ volumes-remove volume_name:
|
|||||||
|
|
||||||
# Remove volumes matching pattern (DESTRUCTIVE!)
|
# Remove volumes matching pattern (DESTRUCTIVE!)
|
||||||
[group('volumes')]
|
[group('volumes')]
|
||||||
|
[confirm]
|
||||||
volumes-remove-pattern pattern:
|
volumes-remove-pattern pattern:
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
@@ -102,12 +92,6 @@ volumes-remove-pattern pattern:
|
|||||||
echo -e "{{YELLOW}}Volumes matching pattern '$pattern':{{NC}}"
|
echo -e "{{YELLOW}}Volumes matching pattern '$pattern':{{NC}}"
|
||||||
echo "$matching_volumes"
|
echo "$matching_volumes"
|
||||||
echo ""
|
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 -e "{{BLUE}}Removing matching volumes...{{NC}}"
|
||||||
echo "$matching_volumes" | while IFS= read -r volume; do
|
echo "$matching_volumes" | while IFS= read -r volume; do
|
||||||
|
|||||||
Reference in New Issue
Block a user