mirror of
https://github.com/shadoll/just-commons.git
synced 2025-12-20 04:27:43 +00:00
Refactor: Enhance command help and add clean functionality for service management
This commit is contained in:
@@ -26,6 +26,7 @@ default:
|
|||||||
echo -e " {{CYAN}}{{BOLD}}start{{NORMAL}} {{DARK_GREY}}[service] [compose-file]\033[0m - Start service(s)"
|
echo -e " {{CYAN}}{{BOLD}}start{{NORMAL}} {{DARK_GREY}}[service] [compose-file]\033[0m - Start service(s)"
|
||||||
echo -e " {{CYAN}}{{BOLD}}stop{{NORMAL}} {{DARK_GREY}}[service] [compose-file]\033[0m - Stop service(s)"
|
echo -e " {{CYAN}}{{BOLD}}stop{{NORMAL}} {{DARK_GREY}}[service] [compose-file]\033[0m - Stop service(s)"
|
||||||
echo -e " {{CYAN}}{{BOLD}}restart{{NORMAL}} {{DARK_GREY}}[service] [compose-file]\033[0m - Restart service(s)"
|
echo -e " {{CYAN}}{{BOLD}}restart{{NORMAL}} {{DARK_GREY}}[service] [compose-file]\033[0m - Restart service(s)"
|
||||||
|
echo -e " {{CYAN}}{{BOLD}}clean{{NORMAL}} {{DARK_GREY}}[service] [compose-file]\033[0m - Clean up service(s) and orphans"
|
||||||
echo -e " {{CYAN}}{{BOLD}}status{{NORMAL}} {{DARK_GREY}}[service] [compose-file]\033[0m - Show status"
|
echo -e " {{CYAN}}{{BOLD}}status{{NORMAL}} {{DARK_GREY}}[service] [compose-file]\033[0m - Show status"
|
||||||
echo -e " {{CYAN}}{{BOLD}}logs{{NORMAL}} {{DARK_GREY}}[service] [compose-file]\033[0m - View logs"
|
echo -e " {{CYAN}}{{BOLD}}logs{{NORMAL}} {{DARK_GREY}}[service] [compose-file]\033[0m - View logs"
|
||||||
echo -e " {{CYAN}}{{BOLD}}shell{{NORMAL}} {{YELLOW}}<service>{{NORMAL}} {{DARK_GREY}}[compose-file]\033[0m - Open shell"
|
echo -e " {{CYAN}}{{BOLD}}shell{{NORMAL}} {{YELLOW}}<service>{{NORMAL}} {{DARK_GREY}}[compose-file]\033[0m - Open shell"
|
||||||
@@ -155,6 +156,43 @@ restart service="" compose-file="":
|
|||||||
just container start "$service" "$compose_file"
|
just container start "$service" "$compose_file"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Clean up service (or all services if no service specified) and remove orphans
|
||||||
|
[no-cd]
|
||||||
|
clean service="" compose-file="":
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
compose_cmd=$(just _detect_compose)
|
||||||
|
service="{{service}}"
|
||||||
|
compose_file="{{compose-file}}"
|
||||||
|
|
||||||
|
# Auto-discover compose file if not provided
|
||||||
|
if [ -z "$compose_file" ]; then
|
||||||
|
compose_file=$(just _discover_compose_file)
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Build compose file argument
|
||||||
|
file_arg=""
|
||||||
|
if [ -n "$compose_file" ]; then
|
||||||
|
file_arg="-f $compose_file"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$service" ]; then
|
||||||
|
echo -e "{{BLUE}}Cleaning up service: $service{{NORMAL}}"
|
||||||
|
if ! $compose_cmd $file_arg down "$service" --remove-orphans 2>&1; then
|
||||||
|
echo -e "{{RED}}✗ Service '$service' not found{{NORMAL}}"
|
||||||
|
echo ""
|
||||||
|
echo -e "{{YELLOW}}Available services:{{NORMAL}}"
|
||||||
|
$compose_cmd $file_arg config --services 2>/dev/null || echo "Could not list services"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo -e "{{GREEN}}✓ Service $service cleaned up{{NORMAL}}"
|
||||||
|
else
|
||||||
|
echo -e "{{BLUE}}Cleaning up all services and orphans...{{NORMAL}}"
|
||||||
|
$compose_cmd $file_arg down --remove-orphans
|
||||||
|
echo -e "{{GREEN}}✓ All services and orphans cleaned up{{NORMAL}}"
|
||||||
|
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="":
|
||||||
|
|||||||
135
images/mod.just
135
images/mod.just
@@ -24,31 +24,79 @@ default:
|
|||||||
echo -e " {{DARK_GREY}}[tag]{{NORMAL}} - Image tag (if empty, generates from git commit or timestamp)"
|
echo -e " {{DARK_GREY}}[tag]{{NORMAL}} - Image tag (if empty, generates from git commit or timestamp)"
|
||||||
echo ""
|
echo ""
|
||||||
echo "{{BOLD}}Commands:{{NORMAL}}"
|
echo "{{BOLD}}Commands:{{NORMAL}}"
|
||||||
echo -e " {{CYAN}}{{BOLD}}build{{NORMAL}} {{DARK_GREY}}[project] [tag]\033[0m - Build image with multi-architecture support"
|
echo -e " {{CYAN}}{{BOLD}}build{{NORMAL}} {{DARK_GREY}}[service] [compose-file] [tag] [no-cache]\033[0m - Build service(s) using docker-compose or image with multi-architecture support"
|
||||||
echo -e " {{CYAN}}{{BOLD}}push{{NORMAL}} {{DARK_GREY}}[project] [tag]\033[0m - Push image to registry"
|
echo -e " {{CYAN}}{{BOLD}}push{{NORMAL}} {{DARK_GREY}}[project] [tag]\033[0m - Push image to registry"
|
||||||
echo -e " {{CYAN}}{{BOLD}}pull{{NORMAL}} {{DARK_GREY}}[project] [tag]\033[0m - Pull image from registry"
|
echo -e " {{CYAN}}{{BOLD}}pull{{NORMAL}} {{DARK_GREY}}[project] [tag]\033[0m - Pull image from registry"
|
||||||
echo -e " {{CYAN}}{{BOLD}}tag{{NORMAL}} {{YELLOW}}<source_tag> <new_tag>{{NORMAL}} - Tag existing image with new tag"
|
echo -e " {{CYAN}}{{BOLD}}tag{{NORMAL}} {{YELLOW}}<source_tag> <new_tag>{{NORMAL}} - Tag existing image with new tag"
|
||||||
echo -e " {{CYAN}}{{BOLD}}info{{NORMAL}} {{DARK_GREY}}[project] [tag]\033[0m - Show image information"
|
echo -e " {{CYAN}}{{BOLD}}info{{NORMAL}} {{DARK_GREY}}[service] [compose-file] [tag]\033[0m - Show container service or image information"
|
||||||
echo -e " {{CYAN}}{{BOLD}}list{{NORMAL}} {{DARK_GREY}}[project]\033[0m - List all project images"
|
echo -e " {{CYAN}}{{BOLD}}list{{NORMAL}} {{DARK_GREY}}[project]\033[0m - List all project images"
|
||||||
echo -e " {{CYAN}}{{BOLD}}clean{{NORMAL}} {{DARK_GREY}}[project]\033[0m - Remove project images (with confirmation)"
|
echo -e " {{CYAN}}{{BOLD}}clean{{NORMAL}} {{DARK_GREY}}[project]\033[0m - Remove project images (with confirmation)"
|
||||||
echo -e " {{CYAN}}{{BOLD}}build-all{{NORMAL}} - Build all projects with Containerfiles"
|
echo -e " {{CYAN}}{{BOLD}}build-all{{NORMAL}} - Build all projects with Containerfiles"
|
||||||
echo ""
|
echo ""
|
||||||
echo "{{BOLD}}Examples:{{NORMAL}}"
|
echo "{{BOLD}}Examples:{{NORMAL}}"
|
||||||
echo " just images build # Build current project with auto-generated tag"
|
echo " just images build # Build all services or current project image"
|
||||||
echo " just images build myapp v1.0.0 # Build specific project with version tag"
|
echo " just images build suaimi-dev # Build specific service or project with auto tag"
|
||||||
|
echo " just images build suaimi-dev container/compose.yml # Build service with specific compose file"
|
||||||
|
echo " just images build myapp v1.0.0 # Build specific project image with version tag"
|
||||||
|
echo " just images build suaimi-dev \"\" \"\" true # Build service with no-cache"
|
||||||
echo " just images push # Push current project latest build"
|
echo " just images push # Push current project latest build"
|
||||||
echo " just images push latest # Push current project as latest"
|
echo " just images push latest # Push current project as latest"
|
||||||
echo " just images list # List all images for current project"
|
echo " just images list # List all images for current project"
|
||||||
echo ""
|
echo " just images info # Show image information for current project"
|
||||||
|
echo " just images info suaimi-dev # Show container service information"
|
||||||
|
echo " just images info suaimi-dev container/compose.yml # Show service info with specific compose file"
|
||||||
|
|
||||||
# Build project image with multi-architecture support
|
# Build service(s) using docker-compose or image with multi-architecture support
|
||||||
[no-cd]
|
[no-cd]
|
||||||
build project="" tag="":
|
build service="" compose-file="" tag="" no-cache="":
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
project="{{project}}"
|
service="{{service}}"
|
||||||
|
compose_file="{{compose-file}}"
|
||||||
tag="{{tag}}"
|
tag="{{tag}}"
|
||||||
|
no_cache="{{no-cache}}"
|
||||||
|
|
||||||
|
# Check if this is container building (service/compose-file provided) or image building
|
||||||
|
if [ -n "$service" ] || [ -n "$compose_file" ]; then
|
||||||
|
# Container building mode
|
||||||
|
compose_cmd=$(just _detect_compose)
|
||||||
|
|
||||||
|
# Auto-discover compose file if not provided
|
||||||
|
if [ -z "$compose_file" ]; then
|
||||||
|
compose_file=$(just _discover_compose_file)
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Build compose file argument
|
||||||
|
file_arg=""
|
||||||
|
if [ -n "$compose_file" ]; then
|
||||||
|
file_arg="-f $compose_file"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Build no-cache argument
|
||||||
|
cache_arg=""
|
||||||
|
if [ "$no_cache" = "true" ]; then
|
||||||
|
cache_arg="--no-cache"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$service" ]; then
|
||||||
|
echo -e "{{BLUE}}Building service: $service{{NORMAL}}"
|
||||||
|
if [ -n "$cache_arg" ]; then
|
||||||
|
echo -e "{{YELLOW}}Using no-cache mode{{NORMAL}}"
|
||||||
|
fi
|
||||||
|
$compose_cmd $file_arg build $cache_arg "$service"
|
||||||
|
echo -e "{{GREEN}}✓ Service $service built{{NORMAL}}"
|
||||||
|
else
|
||||||
|
echo -e "{{BLUE}}Building all services...{{NORMAL}}"
|
||||||
|
if [ -n "$cache_arg" ]; then
|
||||||
|
echo -e "{{YELLOW}}Using no-cache mode{{NORMAL}}"
|
||||||
|
fi
|
||||||
|
$compose_cmd $file_arg build $cache_arg
|
||||||
|
echo -e "{{GREEN}}✓ All services built{{NORMAL}}"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
# Image building mode (original functionality)
|
||||||
|
project="$service" # service parameter used as project for backward compatibility
|
||||||
|
|
||||||
# Auto-discover project if not specified
|
# Auto-discover project if not specified
|
||||||
if [ -z "$project" ]; then
|
if [ -z "$project" ]; then
|
||||||
@@ -97,11 +145,18 @@ build project="" tag="":
|
|||||||
temp_tag="${image_name}-temp-${timestamp}-${platform//\//-}"
|
temp_tag="${image_name}-temp-${timestamp}-${platform//\//-}"
|
||||||
temp_tags+=("$temp_tag")
|
temp_tags+=("$temp_tag")
|
||||||
|
|
||||||
|
build_args=""
|
||||||
|
if [ "$no_cache" = "true" ]; then
|
||||||
|
build_args="--no-cache"
|
||||||
|
echo -e "{{YELLOW}}Using no-cache mode{{NORMAL}}"
|
||||||
|
fi
|
||||||
|
|
||||||
$runtime buildx build \
|
$runtime buildx build \
|
||||||
--platform "$platform" \
|
--platform "$platform" \
|
||||||
-f "$containerfile" \
|
-f "$containerfile" \
|
||||||
-t "$temp_tag" \
|
-t "$temp_tag" \
|
||||||
--load \
|
--load \
|
||||||
|
$build_args \
|
||||||
"$build_context"
|
"$build_context"
|
||||||
done
|
done
|
||||||
|
|
||||||
@@ -123,6 +178,7 @@ build project="" tag="":
|
|||||||
# Keep platform images for manifest - don't clean up temp tags
|
# Keep platform images for manifest - don't clean up temp tags
|
||||||
|
|
||||||
echo -e "{{GREEN}}✓ Successfully built: $image_name{{NORMAL}}"
|
echo -e "{{GREEN}}✓ Successfully built: $image_name{{NORMAL}}"
|
||||||
|
fi
|
||||||
|
|
||||||
# Push project image to registry with smart latest handling
|
# Push project image to registry with smart latest handling
|
||||||
[no-cd]
|
[no-cd]
|
||||||
@@ -316,15 +372,71 @@ tag source_tag new_tag:
|
|||||||
|
|
||||||
echo -e "{{GREEN}}✓ Successfully tagged: $new_image{{NORMAL}}"
|
echo -e "{{GREEN}}✓ Successfully tagged: $new_image{{NORMAL}}"
|
||||||
|
|
||||||
# Show image information
|
# Show image information or container service information
|
||||||
[no-cd]
|
[no-cd]
|
||||||
info project="" tag="":
|
info service="" compose-file="" tag="":
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
project="{{project}}"
|
service="{{service}}"
|
||||||
|
compose_file="{{compose-file}}"
|
||||||
tag="{{tag}}"
|
tag="{{tag}}"
|
||||||
|
|
||||||
|
# Check if this is container info (service/compose-file provided) or image info
|
||||||
|
if [ -n "$service" ] || [ -n "$compose_file" ]; then
|
||||||
|
# Container info mode
|
||||||
|
compose_cmd=$(just _detect_compose)
|
||||||
|
|
||||||
|
# Auto-discover compose file if not provided
|
||||||
|
if [ -z "$compose_file" ]; then
|
||||||
|
compose_file=$(just _discover_compose_file)
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Build compose file argument
|
||||||
|
file_arg=""
|
||||||
|
if [ -n "$compose_file" ]; then
|
||||||
|
file_arg="-f $compose_file"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo -e "{{BLUE}}Container Service Information{{NORMAL}}"
|
||||||
|
echo -e "{{YELLOW}}Compose file:{{NORMAL}} $compose_file"
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
if [ -n "$service" ]; then
|
||||||
|
echo -e "{{BLUE}}Service details for: $service{{NORMAL}}"
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
# Get service configuration
|
||||||
|
service_config=$($compose_cmd $file_arg config --services | grep "^$service$" || true)
|
||||||
|
if [ -z "$service_config" ]; then
|
||||||
|
echo -e "{{RED}}Service '$service' not found in compose file{{NORMAL}}"
|
||||||
|
echo -e "{{YELLOW}}Available services:{{NORMAL}}"
|
||||||
|
$compose_cmd $file_arg config --services
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Show service configuration
|
||||||
|
echo -e "{{CYAN}}Configuration:{{NORMAL}}"
|
||||||
|
$compose_cmd $file_arg config | sed -n "/^ $service:/,/^ [^ ]/p" | sed '$d' | sed 's/^/ /'
|
||||||
|
else
|
||||||
|
echo -e "{{BLUE}}All services in compose file:{{NORMAL}}"
|
||||||
|
echo ""
|
||||||
|
services=$($compose_cmd $file_arg config --services)
|
||||||
|
if [ -z "$services" ]; then
|
||||||
|
echo -e "{{YELLOW}}No services found in compose file{{NORMAL}}"
|
||||||
|
else
|
||||||
|
echo "$services" | while IFS= read -r svc; do
|
||||||
|
echo -e "{{CYAN}}• $svc{{NORMAL}}"
|
||||||
|
done
|
||||||
|
echo ""
|
||||||
|
echo -e "{{YELLOW}}Use: just images info <service_name> to see detailed configuration{{NORMAL}}"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
# Image info mode (original functionality)
|
||||||
|
project="$service" # service parameter used as project for backward compatibility
|
||||||
|
|
||||||
# Auto-discover project if not specified
|
# Auto-discover project if not specified
|
||||||
if [ -z "$project" ]; then
|
if [ -z "$project" ]; then
|
||||||
echo -e "{{BLUE}}🔍 Auto-discovering project...{{NORMAL}}"
|
echo -e "{{BLUE}}🔍 Auto-discovering project...{{NORMAL}}"
|
||||||
@@ -359,6 +471,7 @@ info project="" tag="":
|
|||||||
echo -e "{{BLUE}}All project images:{{NORMAL}}"
|
echo -e "{{BLUE}}All project images:{{NORMAL}}"
|
||||||
$runtime images | grep "$image_name" 2>/dev/null || echo "No images found for project: $project"
|
$runtime images | grep "$image_name" 2>/dev/null || echo "No images found for project: $project"
|
||||||
fi
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
# List all project images
|
# List all project images
|
||||||
[no-cd]
|
[no-cd]
|
||||||
|
|||||||
Reference in New Issue
Block a user