Skip to content

Commit

Permalink
chore: improve available tool detection (#951)
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksii Kurinnyi <[email protected]>
  • Loading branch information
akurinnoy authored Oct 16, 2023
1 parent 9fd8677 commit 6811fbf
Showing 1 changed file with 34 additions and 24 deletions.
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

0 comments on commit 6811fbf

Please sign in to comment.