From 06a6aded5a80473e8f38ea40013047b22b8be211 Mon Sep 17 00:00:00 2001 From: alban Date: Tue, 19 May 2020 20:04:11 +0200 Subject: [PATCH] [fix] fixes critical bugs due to hazardous rewriting --- changelog.sh | 75 ++++++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/changelog.sh b/changelog.sh index e69ab87..a17ad0c 100755 --- a/changelog.sh +++ b/changelog.sh @@ -2,21 +2,20 @@ #Manages an /etc/changelog -echo ${BASH_SOURCE[@]} APP_PATH=$( cd $(dirname ${BASH_SOURCE[0]}) && pwd ) APP_NAME=$( basename ${BASH_SOURCE[0]}) -[[ -w /etc/changelog ]] || { - echo -e "\nYou cannot write to $CHANGELOG... This doesn't look serious!\n" - read -e -i y -n 1 -p "Run as sudo [Yn]? : " - [[ "Y" == ${REPLY^} ]] && sudo -E "$APP_PATH/$APP_NAME" && exit 0 -} - - CHANGELOG=/etc/changelog # %CONFIG% This comment is used to build custom scripts [[ -f "$APP_PATH/config.sh" ]] && source "$APP_PATH/config.sh" +# Sanity check on changelog file +[[ -f "$CHANGELOG" ]] && [[ ! -w "$CHANGELOG" ]] && { + echo -e "\nYou cannot write to $CHANGELOG... This doesn't look serious!\n" + read -e -i y -n 1 -p "Run as sudo [Yn]? : " + [[ "Y" == ${REPLY^} ]] && sudo -E "$APP_PATH/$APP_NAME" && exit 0 +} + # functions msg(){ echo -e "$@"; } panic(){ msg "${@} Exiting."; exit 1;} @@ -30,19 +29,19 @@ change(){ echo -e "\nTime for some details." for (( ; ; )); do - echo "Select number or x to quit" - for (( i=0; i<${#PLUGIN_CONTENT_MENU[@]}; i++)) ; do - echo "$i) ${PLUGIN_CONTENT_MENU[$i]}" - done - echo "x) No more details." - read -e -n 1 -p "Your choice? :" REPLY - if [[ "x" == ${REPLY,,} ]]; then - break - elif [[ -z "${PLUGIN_CONTENT[$REPLY]}" ]] ; then - echo "Unknown Key." - else - ${PLUGIN_CONTENT[$REPLY]} $TMP - fi + echo "Select number or x to quit" + for (( i=0; i<${#PLUGIN_CONTENT_MENU[@]}; i++)) ; do + echo "$i) ${PLUGIN_CONTENT_MENU[$i]}" + done + echo "x) No more details." + read -e -n 1 -p "Your choice? :" REPLY + if [[ "x" == ${REPLY,,} ]]; then + break + elif [[ -z "${PLUGIN_CONTENT[$REPLY]}" ]] ; then + echo "Unknown Key." + else + ${PLUGIN_CONTENT[$REPLY]} $TMP + fi done read -e -i "n" -p "Another section? [yN]:" END [[ "${END^^}" == "N" ]] && break @@ -52,6 +51,15 @@ change(){ [[ "N" == ${RETURN^^} ]] && return 1 } +template(){ +FILE="$1" +cat << HEREDOC + +$(date "+%Y-%m-%d %H:%M") $CHANGELOG_USERNAME +$( cat "$FILE" |sed 's/^/ /' ) +HEREDOC +} + hookContentComment(){ [[ -z "$1" ]] && echo "Enter a simple comment. Ex: systemctl disable nginx" && return TMP="$1" @@ -61,19 +69,12 @@ hookContentComment(){ hookOutputFile(){ [[ -z "$1" ]] && echo "Writing a local changelog entry to $CHANGELOG" && return - local t=$(mktemp) - TMP="$1" - cat << HEREDOC >$t - - -$(date "+%Y-%m-%d %H:%M") $CHANGELOG_USERNAME - -$( echo -e "$@"|sed 's/^/ /' ) -HEREDOC - - [[ -f $CHANGELOG ]] && cat /etc/changelog >> $t - cat "$t" > $CHANGELOG - rm -f "$t" + local TMP=$(mktemp) + SOURCE="$1" + template "$SOURCE" > "$TMP" + [[ -f "$CHANGELOG" ]] && cat /etc/changelog >> "$TMP" + cat "$TMP" > $CHANGELOG + rm -f "$TMP" } @@ -107,7 +108,7 @@ echo -e "\nWelcome to Serious Changelogs Inc. Do you have something for me?\n" [[ " <>" == "$CHANGELOG_USERNAME" ]] && panic "srs.ly?" # check changelog exists -[[ ! -f $CHANGELOG ]] && template "* Changelog: Added $CHANGELOG file\nIt was not serious. Fixed!\n\n" +[[ ! -f "$CHANGELOG" ]] && template "* Changelog: Added $CHANGELOG file\nIt was not serious. Fixed!\n\n" > "$CHANGELOG" # parse command change "$ENTRY_FILE" @@ -115,8 +116,8 @@ change "$ENTRY_FILE" [[ $? -eq 0 ]] && exit for (( i=0; i<${#PLUGIN_OUTPUT_MENU[@]}; i++)) ; do - echo "${PLUGIN_OUTPUT_MENU[$i]}" - ${PLUGIN_OUTPUT[$i]} $ENTRY_FILE + echo "${PLUGIN_OUTPUT_MENU[$i]}" + ${PLUGIN_OUTPUT[$i]} $ENTRY_FILE done rm "$ENTRY_FILE"