From 25dcdd5ef7a26065df9d8755933cc55b506cad5a Mon Sep 17 00:00:00 2001 From: Thomas Ballasi Date: Fri, 27 Oct 2023 19:23:24 -0400 Subject: [PATCH] docker_build: add throbber 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. --- cqfd | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/cqfd b/cqfd index 5e32406..de84a3b 100755 --- a/cqfd +++ b/cqfd @@ -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 @@ -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