Skip to content

Commit

Permalink
docker_build: add throbber
Browse files Browse the repository at this point in the history
As the cqfd --init command is going, in the future, to be ran
automatically, work has been done to make it less instrusive.

Rather than showing all of the Docker log, it just shows a throbber
with the current step status, and shows the full log only if the
docker build command failed.
  • Loading branch information
Thomas Ballasi committed Oct 30, 2023
1 parent c7db455 commit 25dcdd5
Showing 1 changed file with 56 additions and 3 deletions.
59 changes: 56 additions & 3 deletions cqfd
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,64 @@ die() {
exit 1
}

# loading_throbber() - Loading throbber process for Docker build initialization
loading_throbber() {
pid=$1
delay=0.1
spinstr='-\|/'

while kill -0 "$pid" 2>/dev/null
do
temp=${spinstr#?}
state=$(awk '$1 == "Step" { print $2 }' "$TEMP_OUTPUT" | tail -n 1)
spinstr=$temp${spinstr%"$temp"}
# Getting the last line of the output of the long command
if [ "$state" ]; then
printf '\r[%c] %s: Initialization of the environment (%s)' "$spinstr" "$PROGNAME" "$state"
line_length=$((46 + ${#state}))
else
printf '\r[%c] %s: Initialization of the environment' "$spinstr" "$PROGNAME"
fi
sleep $delay
done
}

# docker_build() - Initialize build container
docker_build() {
export BUILDKIT_PROGRESS=plain
TEMP_OUTPUT=$(mktemp -u)
line_length=46

if [ -z "$project_build_context" ]; then
docker build ${quiet:+-q} $CQFD_EXTRA_BUILD_ARGS -t "$docker_img_name" "$(dirname "$dockerfile")"
docker build ${quiet:+-q} $CQFD_EXTRA_BUILD_ARGS -t "$docker_img_name" "$(dirname "$dockerfile")" > "$TEMP_OUTPUT" 2>&1 &
else
docker build ${quiet:+-q} $CQFD_EXTRA_BUILD_ARGS -t "$docker_img_name" "${project_build_context}" -f "$dockerfile"
docker build ${quiet:+-q} $CQFD_EXTRA_BUILD_ARGS -t "$docker_img_name" "${project_build_context}" -f "$dockerfile" > "$TEMP_OUTPUT" 2>&1 &
fi
container_init_command_pid=$!

if [ "$quiet" ]; then
# If quiet mode is enabled, we don't display the throbber
# and we wait for the docker build to finish
wait $container_init_command_pid
cat "$TEMP_OUTPUT"
rm "$TEMP_OUTPUT"
return 0
fi

# Start the throbber
loading_throbber $container_init_command_pid

# Wait for the docker build to finish
if ! wait $container_init_command_pid; then
printf "\r%${line_length}s\r"
cat "$TEMP_OUTPUT" 1>&2
rm "$TEMP_OUTPUT"
die 'Initialization of environment failed'
fi

# Clean up the output file and the console
printf "\r%${line_length}s\r"
rm "$TEMP_OUTPUT"
}

# docker_run() - run command in configured container
Expand Down Expand Up @@ -465,7 +516,9 @@ while [ $# -gt 0 ]; do
"--init")
config_load $flavor
docker_build
exit $?
ret=$?
[ "$quiet" ] || echo "$PROGNAME: Initialization of environment successful"
exit $ret
;;
"--flavors")
config_load
Expand Down

0 comments on commit 25dcdd5

Please sign in to comment.