Refactor: Add project argument support to service management and volume cleanup commands

This commit is contained in:
sha
2026-02-05 16:45:59 +02:00
parent 59f66a89b6
commit 9cceaa46a8
2 changed files with 104 additions and 36 deletions
+94 -31
View File
@@ -48,13 +48,14 @@ default:
# Start service (or all services if no service specified) # Start service (or all services if no service specified)
[no-cd] [no-cd]
start service="" compose-file="": start service="" compose-file="" project="":
#!/usr/bin/env bash #!/usr/bin/env bash
set -euo pipefail set -euo pipefail
compose_cmd=$(just _detect_compose) compose_cmd=$(just _detect_compose)
service="{{service}}" service="{{service}}"
compose_file="{{compose-file}}" compose_file="{{compose-file}}"
project="{{project}}"
# Auto-discover compose file if not provided # Auto-discover compose file if not provided
if [ -z "$compose_file" ]; then if [ -z "$compose_file" ]; then
@@ -67,31 +68,38 @@ start service="" compose-file="":
file_arg="-f $compose_file" file_arg="-f $compose_file"
fi fi
# Build project name argument
project_arg=""
if [ -n "$project" ]; then
project_arg="-p $project"
fi
if [ -n "$service" ]; then if [ -n "$service" ]; then
echo -e "{{BLUE}}Starting service: $service{{NORMAL}}" echo -e "{{BLUE}}Starting service: $service{{NORMAL}}"
if ! $compose_cmd $file_arg up -d "$service" 2>&1; then if ! $compose_cmd $project_arg $file_arg up -d "$service" 2>&1; then
echo -e "{{RED}}✗ Service '$service' not found{{NORMAL}}" echo -e "{{RED}}✗ Service '$service' not found{{NORMAL}}"
echo "" echo ""
echo -e "{{YELLOW}}Available services:{{NORMAL}}" echo -e "{{YELLOW}}Available services:{{NORMAL}}"
$compose_cmd $file_arg config --services 2>/dev/null || echo "Could not list services" $compose_cmd $project_arg $file_arg config --services 2>/dev/null || echo "Could not list services"
exit 1 exit 1
fi fi
echo -e "{{GREEN}}✓ Service $service started{{NORMAL}}" echo -e "{{GREEN}}✓ Service $service started{{NORMAL}}"
else else
echo -e "{{BLUE}}Starting all services...{{NORMAL}}" echo -e "{{BLUE}}Starting all services...{{NORMAL}}"
$compose_cmd $file_arg up -d $compose_cmd $project_arg $file_arg up -d
echo -e "{{GREEN}}✓ All services started{{NORMAL}}" echo -e "{{GREEN}}✓ All services started{{NORMAL}}"
fi fi
# Stop service (or all services if no service specified) # Stop service (or all services if no service specified)
[no-cd] [no-cd]
stop service="" compose-file="": stop service="" compose-file="" project="":
#!/usr/bin/env bash #!/usr/bin/env bash
set -euo pipefail set -euo pipefail
compose_cmd=$(just _detect_compose) compose_cmd=$(just _detect_compose)
service="{{service}}" service="{{service}}"
compose_file="{{compose-file}}" compose_file="{{compose-file}}"
project="{{project}}"
# Auto-discover compose file if not provided # Auto-discover compose file if not provided
if [ -z "$compose_file" ]; then if [ -z "$compose_file" ]; then
@@ -104,31 +112,38 @@ stop service="" compose-file="":
file_arg="-f $compose_file" file_arg="-f $compose_file"
fi fi
# Build project name argument
project_arg=""
if [ -n "$project" ]; then
project_arg="-p $project"
fi
if [ -n "$service" ]; then if [ -n "$service" ]; then
echo -e "{{BLUE}}Stopping service: $service{{NORMAL}}" echo -e "{{BLUE}}Stopping service: $service{{NORMAL}}"
if ! $compose_cmd $file_arg stop "$service" 2>&1; then if ! $compose_cmd $project_arg $file_arg stop "$service" 2>&1; then
echo -e "{{RED}}✗ Service '$service' not found{{NORMAL}}" echo -e "{{RED}}✗ Service '$service' not found{{NORMAL}}"
echo "" echo ""
echo -e "{{YELLOW}}Available services:{{NORMAL}}" echo -e "{{YELLOW}}Available services:{{NORMAL}}"
$compose_cmd $file_arg config --services 2>/dev/null || echo "Could not list services" $compose_cmd $project_arg $file_arg config --services 2>/dev/null || echo "Could not list services"
exit 1 exit 1
fi fi
echo -e "{{GREEN}}✓ Service $service stopped{{NORMAL}}" echo -e "{{GREEN}}✓ Service $service stopped{{NORMAL}}"
else else
echo -e "{{BLUE}}Stopping all services...{{NORMAL}}" echo -e "{{BLUE}}Stopping all services...{{NORMAL}}"
$compose_cmd $file_arg down $compose_cmd $project_arg $file_arg down
echo -e "{{GREEN}}✓ All services stopped{{NORMAL}}" echo -e "{{GREEN}}✓ All services stopped{{NORMAL}}"
fi fi
# Restart service (or all services if no service specified) # Restart service (or all services if no service specified)
[no-cd] [no-cd]
restart service="" compose-file="": restart service="" compose-file="" project="":
#!/usr/bin/env bash #!/usr/bin/env bash
set -euo pipefail set -euo pipefail
compose_cmd=$(just _detect_compose) compose_cmd=$(just _detect_compose)
service="{{service}}" service="{{service}}"
compose_file="{{compose-file}}" compose_file="{{compose-file}}"
project="{{project}}"
# Auto-discover compose file if not provided # Auto-discover compose file if not provided
if [ -z "$compose_file" ]; then if [ -z "$compose_file" ]; then
@@ -141,40 +156,47 @@ restart service="" compose-file="":
file_arg="-f $compose_file" file_arg="-f $compose_file"
fi fi
# Build project name argument
project_arg=""
if [ -n "$project" ]; then
project_arg="-p $project"
fi
if [ -n "$service" ]; then if [ -n "$service" ]; then
echo -e "{{BLUE}}Restarting service: $service{{NORMAL}}" echo -e "{{BLUE}}Restarting service: $service{{NORMAL}}"
# Try to restart first # Try to restart first
$compose_cmd $file_arg restart "$service" 2>/dev/null || true $compose_cmd $project_arg $file_arg restart "$service" 2>/dev/null || true
# Check if service is actually running after restart attempt # Check if service is actually running after restart attempt
if $compose_cmd $file_arg ps "$service" 2>/dev/null | grep -q "Up"; then if $compose_cmd $project_arg $file_arg ps "$service" 2>/dev/null | grep -q "Up"; then
echo -e "{{GREEN}}✓ Service $service restarted{{NORMAL}}" echo -e "{{GREEN}}✓ Service $service restarted{{NORMAL}}"
else else
echo -e "{{YELLOW}}Service $service not running, starting instead...{{NORMAL}}" echo -e "{{YELLOW}}Service $service not running, starting instead...{{NORMAL}}"
if ! $compose_cmd $file_arg up -d "$service" 2>&1; then if ! $compose_cmd $project_arg $file_arg up -d "$service" 2>&1; then
echo -e "{{RED}}✗ Service '$service' not found{{NORMAL}}" echo -e "{{RED}}✗ Service '$service' not found{{NORMAL}}"
echo "" echo ""
echo -e "{{YELLOW}}Available services:{{NORMAL}}" echo -e "{{YELLOW}}Available services:{{NORMAL}}"
$compose_cmd $file_arg config --services 2>/dev/null || echo "Could not list services" $compose_cmd $project_arg $file_arg config --services 2>/dev/null || echo "Could not list services"
exit 1 exit 1
fi fi
echo -e "{{GREEN}}✓ Service $service started{{NORMAL}}" echo -e "{{GREEN}}✓ Service $service started{{NORMAL}}"
fi fi
else else
just container stop "$service" "$compose_file" just container stop "$service" "$compose_file" "$project"
just container start "$service" "$compose_file" just container start "$service" "$compose_file" "$project"
fi fi
# Clean up service (or all services if no service specified) and remove orphans # Clean up service (or all services if no service specified) and remove orphans
[no-cd] [no-cd]
clean service="" compose-file="": clean service="" compose-file="" project="":
#!/usr/bin/env bash #!/usr/bin/env bash
set -euo pipefail set -euo pipefail
compose_cmd=$(just _detect_compose) compose_cmd=$(just _detect_compose)
service="{{service}}" service="{{service}}"
compose_file="{{compose-file}}" compose_file="{{compose-file}}"
project="{{project}}"
# Auto-discover compose file if not provided # Auto-discover compose file if not provided
if [ -z "$compose_file" ]; then if [ -z "$compose_file" ]; then
@@ -187,31 +209,38 @@ clean service="" compose-file="":
file_arg="-f $compose_file" file_arg="-f $compose_file"
fi fi
# Build project name argument
project_arg=""
if [ -n "$project" ]; then
project_arg="-p $project"
fi
if [ -n "$service" ]; then if [ -n "$service" ]; then
echo -e "{{BLUE}}Cleaning up service: $service{{NORMAL}}" echo -e "{{BLUE}}Cleaning up service: $service{{NORMAL}}"
if ! $compose_cmd $file_arg down "$service" --remove-orphans 2>&1; then if ! $compose_cmd $project_arg $file_arg down "$service" --remove-orphans 2>&1; then
echo -e "{{RED}}✗ Service '$service' not found{{NORMAL}}" echo -e "{{RED}}✗ Service '$service' not found{{NORMAL}}"
echo "" echo ""
echo -e "{{YELLOW}}Available services:{{NORMAL}}" echo -e "{{YELLOW}}Available services:{{NORMAL}}"
$compose_cmd $file_arg config --services 2>/dev/null || echo "Could not list services" $compose_cmd $project_arg $file_arg config --services 2>/dev/null || echo "Could not list services"
exit 1 exit 1
fi fi
echo -e "{{GREEN}}✓ Service $service cleaned up{{NORMAL}}" echo -e "{{GREEN}}✓ Service $service cleaned up{{NORMAL}}"
else else
echo -e "{{BLUE}}Cleaning up all services and orphans...{{NORMAL}}" echo -e "{{BLUE}}Cleaning up all services and orphans...{{NORMAL}}"
$compose_cmd $file_arg down --remove-orphans $compose_cmd $project_arg $file_arg down --remove-orphans
echo -e "{{GREEN}}✓ All services and orphans cleaned up{{NORMAL}}" echo -e "{{GREEN}}✓ All services and orphans cleaned up{{NORMAL}}"
fi fi
# Show service status (specific service or all) # Show service status (specific service or all)
[no-cd] [no-cd]
status service="" compose-file="": status service="" compose-file="" project="":
#!/usr/bin/env bash #!/usr/bin/env bash
set -euo pipefail set -euo pipefail
compose_cmd=$(just _detect_compose) compose_cmd=$(just _detect_compose)
service="{{service}}" service="{{service}}"
compose_file="{{compose-file}}" compose_file="{{compose-file}}"
project="{{project}}"
# Auto-discover compose file if not provided # Auto-discover compose file if not provided
if [ -z "$compose_file" ]; then if [ -z "$compose_file" ]; then
@@ -224,24 +253,31 @@ status service="" compose-file="":
file_arg="-f $compose_file" file_arg="-f $compose_file"
fi fi
# Build project name argument
project_arg=""
if [ -n "$project" ]; then
project_arg="-p $project"
fi
if [ -n "$service" ]; then if [ -n "$service" ]; then
echo -e "{{BLUE}}Status for: $service{{NORMAL}}" echo -e "{{BLUE}}Status for: $service{{NORMAL}}"
# Filter output for specific service # Filter output for specific service
$compose_cmd $file_arg ps | grep -E "(NAME|$service)" || echo "Service $service not found" $compose_cmd $project_arg $file_arg ps | grep -E "(NAME|$service)" || echo "Service $service not found"
else else
echo -e "{{BLUE}}Service Status:{{NORMAL}}" echo -e "{{BLUE}}Service Status:{{NORMAL}}"
$compose_cmd $file_arg ps $compose_cmd $project_arg $file_arg ps
fi fi
# View logs for specific service or all services # View logs for specific service or all services
[no-cd] [no-cd]
logs service="" compose-file="" follow="true": logs service="" compose-file="" follow="true" project="":
#!/usr/bin/env bash #!/usr/bin/env bash
set -euo pipefail set -euo pipefail
compose_cmd=$(just _detect_compose) compose_cmd=$(just _detect_compose)
service="{{service}}" service="{{service}}"
compose_file="{{compose-file}}" compose_file="{{compose-file}}"
project="{{project}}"
# Auto-discover compose file if not provided # Auto-discover compose file if not provided
if [ -z "$compose_file" ]; then if [ -z "$compose_file" ]; then
@@ -254,6 +290,12 @@ logs service="" compose-file="" follow="true":
file_arg="-f $compose_file" file_arg="-f $compose_file"
fi fi
# Build project name argument
project_arg=""
if [ -n "$project" ]; then
project_arg="-p $project"
fi
follow_flag="" follow_flag=""
if [ "{{follow}}" = "true" ]; then if [ "{{follow}}" = "true" ]; then
follow_flag="-f" follow_flag="-f"
@@ -261,21 +303,22 @@ logs service="" compose-file="" follow="true":
if [ -n "$service" ]; then if [ -n "$service" ]; then
echo -e "{{BLUE}}Showing logs for: $service{{NORMAL}}" echo -e "{{BLUE}}Showing logs for: $service{{NORMAL}}"
$compose_cmd $file_arg logs $follow_flag "$service" $compose_cmd $project_arg $file_arg logs $follow_flag "$service"
else else
echo -e "{{BLUE}}Showing logs for all services{{NORMAL}}" echo -e "{{BLUE}}Showing logs for all services{{NORMAL}}"
$compose_cmd $file_arg logs $follow_flag $compose_cmd $project_arg $file_arg logs $follow_flag
fi fi
# Open shell in specific container # Open shell in specific container
[no-cd] [no-cd]
shell service compose-file="": shell service compose-file="" project="":
#!/usr/bin/env bash #!/usr/bin/env bash
set -euo pipefail set -euo pipefail
compose_cmd=$(just _detect_compose) compose_cmd=$(just _detect_compose)
service="{{service}}" service="{{service}}"
compose_file="{{compose-file}}" compose_file="{{compose-file}}"
project="{{project}}"
# Auto-discover compose file if not provided # Auto-discover compose file if not provided
if [ -z "$compose_file" ]; then if [ -z "$compose_file" ]; then
@@ -288,12 +331,18 @@ shell service compose-file="":
file_arg="-f $compose_file" file_arg="-f $compose_file"
fi fi
# Build project name argument
project_arg=""
if [ -n "$project" ]; then
project_arg="-p $project"
fi
echo -e "{{BLUE}}Opening shell in: $service{{NORMAL}}" echo -e "{{BLUE}}Opening shell in: $service{{NORMAL}}"
$compose_cmd $file_arg exec "$service" /bin/bash $compose_cmd $project_arg $file_arg exec "$service" /bin/bash
# Execute command in specific service container # Execute command in specific service container
[no-cd] [no-cd]
exec service cmd compose-file="": exec service cmd compose-file="" project="":
#!/usr/bin/env bash #!/usr/bin/env bash
set -euo pipefail set -euo pipefail
@@ -301,6 +350,7 @@ exec service cmd compose-file="":
service="{{service}}" service="{{service}}"
cmd="{{cmd}}" cmd="{{cmd}}"
compose_file="{{compose-file}}" compose_file="{{compose-file}}"
project="{{project}}"
# Auto-discover compose file if not provided # Auto-discover compose file if not provided
if [ -z "$compose_file" ]; then if [ -z "$compose_file" ]; then
@@ -313,12 +363,18 @@ exec service cmd compose-file="":
file_arg="-f $compose_file" file_arg="-f $compose_file"
fi fi
# Build project name argument
project_arg=""
if [ -n "$project" ]; then
project_arg="-p $project"
fi
echo -e "{{BLUE}}Executing in $service: $cmd{{NORMAL}}" echo -e "{{BLUE}}Executing in $service: $cmd{{NORMAL}}"
$compose_cmd $file_arg exec "$service" bash -c "$cmd" $compose_cmd $project_arg $file_arg exec "$service" bash -c "$cmd"
# Execute command in specific service container with piped input support # Execute command in specific service container with piped input support
[no-cd] [no-cd]
exec-pipe service cmd compose-file="": exec-pipe service cmd compose-file="" project="":
#!/usr/bin/env bash #!/usr/bin/env bash
set -euo pipefail set -euo pipefail
@@ -326,6 +382,7 @@ exec-pipe service cmd compose-file="":
service="{{service}}" service="{{service}}"
cmd="{{cmd}}" cmd="{{cmd}}"
compose_file="{{compose-file}}" compose_file="{{compose-file}}"
project="{{project}}"
# Auto-discover compose file if not provided # Auto-discover compose file if not provided
if [ -z "$compose_file" ]; then if [ -z "$compose_file" ]; then
@@ -338,5 +395,11 @@ exec-pipe service cmd compose-file="":
file_arg="-f $compose_file" file_arg="-f $compose_file"
fi fi
# Build project name argument
project_arg=""
if [ -n "$project" ]; then
project_arg="-p $project"
fi
echo -e "{{BLUE}}Executing in $service (with piped input): $cmd{{NORMAL}}" echo -e "{{BLUE}}Executing in $service (with piped input): $cmd{{NORMAL}}"
$compose_cmd $file_arg exec -T "$service" bash -c "$cmd" $compose_cmd $project_arg $file_arg exec -T "$service" bash -c "$cmd"
+10 -5
View File
@@ -37,7 +37,7 @@ default:
# Clean all volumes used by compose file (DESTRUCTIVE!) # Clean all volumes used by compose file (DESTRUCTIVE!)
[confirm] [confirm]
clean-all compose-file="": clean-all compose-file="" project="":
#!/usr/bin/env bash #!/usr/bin/env bash
set -euo pipefail set -euo pipefail
@@ -50,19 +50,24 @@ clean-all compose-file="":
file_arg="-f $compose_file" file_arg="-f $compose_file"
fi fi
project_arg=""
if [ -n "{{project}}" ]; then
project_arg="-p {{project}}"
fi
echo -e "{{RED}}⚠️ WARNING: This will DELETE ALL DATA!{{NORMAL}}" echo -e "{{RED}}⚠️ WARNING: This will DELETE ALL DATA!{{NORMAL}}"
echo -e "{{YELLOW}}This will remove all volumes defined in the compose file{{NORMAL}}" echo -e "{{YELLOW}}This will remove all volumes defined in the compose file{{NORMAL}}"
# Show which volumes would be removed # Show which volumes would be removed
echo -e "{{BLUE}}Volumes that will be removed:{{NORMAL}}" echo -e "{{BLUE}}Volumes that will be removed:{{NORMAL}}"
$compose_cmd $file_arg config --volumes 2>/dev/null || echo "Cannot list volumes from compose file" $compose_cmd $project_arg $file_arg config --volumes 2>/dev/null || echo "Cannot list volumes from compose file"
echo "" echo ""
echo -e "{{BLUE}}Stopping services...{{NORMAL}}" echo -e "{{BLUE}}Stopping services...{{NORMAL}}"
$compose_cmd $file_arg down $compose_cmd $project_arg $file_arg down
echo -e "{{BLUE}}Removing volumes...{{NORMAL}}" echo -e "{{BLUE}}Removing volumes...{{NORMAL}}"
$compose_cmd $file_arg down -v $compose_cmd $project_arg $file_arg down -v
echo -e "{{GREEN}}✓ All volumes removed{{NORMAL}}" echo -e "{{GREEN}}✓ All volumes removed{{NORMAL}}"
@@ -149,4 +154,4 @@ list pattern="":
else else
echo -e "{{BLUE}}All volumes:{{NORMAL}}" echo -e "{{BLUE}}All volumes:{{NORMAL}}"
$runtime volume ls $runtime volume ls
fi fi