Skip to content

Instantly share code, notes, and snippets.

@koichirok
Created April 14, 2023 08:02
Show Gist options
  • Save koichirok/d62d1c072fe2b9b7cdf14c32317c7b04 to your computer and use it in GitHub Desktop.
Save koichirok/d62d1c072fe2b9b7cdf14c32317c7b04 to your computer and use it in GitHub Desktop.
A shell script to wrap `podman-compose config` command to make paths in output absolute
#!/bin/sh
#
# podman-compose wrapper to make relative paths in output of `config` command absolute.
# Currently only paths in build.context and volumes are converted.
#
# path to the podman-compose executable to wrap
PODMAN_COMPOSE="/usr/bin/podman-compose"
COMMAND=""
COMPOSE_FILES=""
# parse command line arguments to determine which command is specified
parse_args () {
while [ $# -gt 0 ]
do
case "$1" in
-h|--help) COMMAND="help";;
-f)
COMPOSE_FILES="${COMPOSE_FILES}$2;"
shift
;;
help|version|wait|systemd|pull|push|build|up|down|ps|run|exec|start|stop|restart|logs|config|port|pause|unpause|kill)
COMMAND="$1";;
esac
if [ "$COMMAND" ]; then
break
fi
shift
done
}
# function to test "$1" starts with "$2" or not
starts_with () {
string="$1"
char="$2"
test "${string#"$char"}" != "${string}"
}
parse_args "$@"
if [ "$COMMAND" != config ]; then
exec "$PODMAN_COMPOSE" "$@"
fi
if [ "$COMPOSE_FILES" ]; then
# Use the location of the first file as the base directory, because docker-compose behaves this way.
BASE_FILE="${COMPOSE_FILES%%;*}"
if ! starts_with "$BASE_FILE" /; then
BASE_FILE="$PWD/$BASE_FILE"
BASE_FILE="$(realpath "$BASE_FILE")"
fi
BASE_DIR="$(dirname "$BASE_FILE")"
else
BASE_DIR="$PWD"
fi
# echo '"." is '"$BASE_DIR" >&2
OUTPUT="$("$PODMAN_COMPOSE" "$@")"
RC=$?
if [ $RC -ne 0 ] && [ "$OUTPUT" ]; then
echo "$OUTPUT"
exit $RC
fi
IFS="
"
echo "$OUTPUT" | while read -r line
do
case $line in
*" context:"*)
path="$BASE_DIR/$(echo "$line" | sed 's/^.*: *//')"
echo "context directory is $path" >&2
line="${line%%:*}: $(realpath "$path")"
;;
*" volumes:") in_volumes=1;;
*" - "*)
if [ "$in_volumes" -eq 1 ]; then
volume="${line#*- }"
local_path="${volume%%:*}"
other="${volume#*:}"
if [ "${local_path#.}" != "$local_path" ]; then
local_path=$(realpath "$BASE_DIR/$local_path")
line="${line%% - *} - $local_path:$other"
fi
fi
;;
*) in_volumes=0;;
esac
echo "$line"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment