Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve available tool detection #951

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 34 additions & 24 deletions scripts/container_tool.sh
Original file line number Diff line number Diff line change
@@ -1,40 +1,50 @@
#!/bin/bash

# check if command exists
check_command() {
if command -v "$1" &> /dev/null; then
return 0 # not found
else
return 1 # found
fi
# Function to check if a command is available
command_exists() {
command -v "$1" >/dev/null 2>&1
}

# Check for Podman
if command_exists "podman"; then
# Check if Podman machine is running
if podman info &>/dev/null; then
container_engine="podman"
fi
fi

# Check for Docker
if command_exists "docker"; then
# Check if Docker daemon is running
if docker info &>/dev/null; then
container_engine="docker"
fi
fi

# If neither Podman nor Docker is found or running
if [ -z "$container_engine" ]; then
echo "Neither Podman nor Docker is installed or running."
exit 1
fi

# Run command using Docker or Podman whichever is available
container_tool() {
local command=$1
shift

if check_command "podman"; then
CONTAINER_TOOL="podman"
elif check_command "docker"; then
CONTAINER_TOOL="docker"
else
echo "Error: Docker or Podman not found on the system."
exit 1
fi

"$CONTAINER_TOOL" "$command" "$@"
echo "Container engine: $container_engine"
"$container_engine" "$command" "$@"
}

# Main script
case "$1" in
build|run|push)
container_tool "$@"
;;
*)
echo "Uknown command. Use: build, run or push."
exit 1
;;
build | run | push)
container_tool "$@"
;;
*)
echo "Unknown command. Use: build, run, or push."
exit 1
;;
esac

exit 0
Loading