-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: improve available tool detection (#951)
Signed-off-by: Oleksii Kurinnyi <[email protected]>
- Loading branch information
Showing
1 changed file
with
34 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |