From 1745a15acfb1d1c35a79dfaf831f852237e5b34e Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Sun, 23 Jul 2023 21:51:40 +0300 Subject: [PATCH] fix(scripts): install-cheat-tldr now updates files --- scripts/install-cheat-tldr.sh | 13 +++++++------ scripts/shared.sh | 26 ++++++++++++++++++++------ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/scripts/install-cheat-tldr.sh b/scripts/install-cheat-tldr.sh index 2d2968f..88d9446 100755 --- a/scripts/install-cheat-tldr.sh +++ b/scripts/install-cheat-tldr.sh @@ -51,14 +51,15 @@ for d in "$TLDR_TEMP_DIR"/pages/*; do TLDR_FILE="$SECTION_DIR/${FILENAME}" # echo "-> dest: $TLDR_FILE" - replacable "$FILE" "$TLDR_FILE" - override=$? - if [ "$override" -ne 1 ]; then - cp "$FILE" "$TLDR_FILE" && msg_run "Updated: $TLDR_FILE" + # Update the original file for making the replacable value comparable + if [ -f "$FILE" ] && [ '---' != "$(head -1 < "$FILE")" ]; then + echo -e "---\n$TLDR_SYNTAX\n$TLDR_TAGS\n$TLDR_SOURCE\n---\n$(cat "$FILE")" > "$FILE" fi - if [ -f "$TLDR_FILE" ] && [ '---' != "$(head -1 < "$TLDR_FILE")" ]; then - echo -e "---\n$TLDR_SYNTAX\n$TLDR_TAGS\n$TLDR_SOURCE\n---\n$(cat "$TLDR_FILE")" > "$TLDR_FILE" + replacable "$FILE" "$TLDR_FILE" + override=$? + if [ "$override" -ne 0 ]; then + cp "$FILE" "$TLDR_FILE" && msg_run "Updated: $TLDR_FILE" fi done diff --git a/scripts/shared.sh b/scripts/shared.sh index e40e199..4696cc8 100755 --- a/scripts/shared.sh +++ b/scripts/shared.sh @@ -2,7 +2,10 @@ # # Shared bash functions and helpers # that can be sourced to other scripts. -# + +# Helper env variables. Use like this: VERBOSE=1 ./script.sh +: "${VERBOSE:=0}" + # -- Colors -- # CLR_RED="\033[1;31m" @@ -249,23 +252,34 @@ function replacable() FILE1="$1" FILE2="$2" - [[ ! -r "$FILE1" ]] && { msg_err "File 1 ($FILE1) does not exist" && return 1; } - [[ ! -r "$FILE2" ]] && { msg_err "File 2 ($FILE2) does not exist, replacable" && return 1; } + [[ ! -r "$FILE1" ]] && { + [[ $VERBOSE -eq 1 ]] && msg_err "File 1 ($FILE1) does not exist" + return 0; + } + [[ ! -r "$FILE2" ]] && { + [[ $VERBOSE -eq 1 ]] && msg_err "File 2 ($FILE2) does not exist, replacable" + return 1; + } FILE1_HASH=$(get_sha256sum "$FILE1") FILE2_HASH=$(get_sha256sum "$FILE2") [[ $FILE1_HASH = "" ]] && { - msg_err "Could not get hash for file 1 ($FILE1)" && return 1; + [[ $VERBOSE -eq 1 ]] && msg_err "Could not get hash for file 1 ($FILE1)" + return 0; } [[ $FILE2_HASH = "" ]] && { - msg_err "Could not get hash for file 2 ($FILE2), replacable" && return 1; + [[ $VERBOSE -eq 1 ]] && msg_err "Could not get hash for file 2 ($FILE2), replacable" + return 1; } [[ "$FILE1_HASH" == "$FILE2_HASH" ]] && { - msg_ok "Files match, not replacable" && return 0; + [[ $VERBOSE -eq 1 ]] && msg_ok "Files match, not replacable: $FILE1" + return 0; } + [[ $VERBOSE -eq 1 ]] && msg_warn "Files do not match ($FILE1_HASH != $FILE2_HASH), replacable" + return 1; }