2020-05-12 08:20:41 +00:00
|
|
|
#!/bin/bash
|
|
|
|
#Manages an /etc/changelog
|
|
|
|
|
2020-06-03 19:50:40 +00:00
|
|
|
# Immediately attempt to read from named pipe
|
|
|
|
# And store the piped content if provided
|
|
|
|
[[ -p /proc/self/fd/0 ]] && export STDIN=$( cat )
|
|
|
|
# And then force the input to be from terminal
|
|
|
|
exec 0</dev/tty
|
2020-05-17 23:10:06 +00:00
|
|
|
|
2020-06-03 19:50:40 +00:00
|
|
|
# Defining some path variables
|
2020-05-19 08:14:22 +00:00
|
|
|
APP_PATH=$( cd $(dirname ${BASH_SOURCE[0]}) && pwd )
|
|
|
|
APP_NAME=$( basename ${BASH_SOURCE[0]})
|
2020-06-03 19:50:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-05-19 18:04:11 +00:00
|
|
|
# %CONFIG% This comment is used to build custom scripts
|
|
|
|
[[ -f "$APP_PATH/config.sh" ]] && source "$APP_PATH/config.sh"
|
|
|
|
|
|
|
|
# Sanity check on changelog file
|
2020-06-03 19:50:40 +00:00
|
|
|
[[ -n "$CHANGELOG" ]] && CHANGELOG="/etc/Changelog"
|
2020-05-21 18:45:38 +00:00
|
|
|
[[ -f "$CHANGELOG" ]] && [[ ! -w "$CHANGELOG" ]] && RUN_SUDO=1
|
|
|
|
[[ ! -f "$CHANGELOG" ]] && ! touch /etc/changelog &>/dev/null && RUN_SUDO=1
|
|
|
|
[[ $RUN_SUDO -eq 1 ]] && {
|
2020-05-17 23:10:06 +00:00
|
|
|
echo -e "\nYou cannot write to $CHANGELOG... This doesn't look serious!\n"
|
|
|
|
read -e -i y -n 1 -p "Run as sudo [Yn]? : "
|
2020-05-19 08:14:22 +00:00
|
|
|
[[ "Y" == ${REPLY^} ]] && sudo -E "$APP_PATH/$APP_NAME" && exit 0
|
2020-05-17 23:10:06 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 08:20:41 +00:00
|
|
|
# functions
|
|
|
|
msg(){ echo -e "$@"; }
|
|
|
|
panic(){ msg "${@} Exiting."; exit 1;}
|
2020-05-16 00:38:09 +00:00
|
|
|
ENTRY_FILE=$(mktemp)
|
2020-06-03 18:52:22 +00:00
|
|
|
|
|
|
|
# This is the main function
|
2020-05-12 08:20:41 +00:00
|
|
|
change(){
|
2020-05-16 00:38:09 +00:00
|
|
|
TMP="$1"
|
2020-05-12 08:20:41 +00:00
|
|
|
while true ; do
|
|
|
|
echo -e "\nType the nature of the change.\nExample: > mysql: do not start on boot\n"
|
|
|
|
read -e -p "> " SUB
|
2020-05-15 09:46:56 +00:00
|
|
|
echo -e "* $SUB" >> $TMP
|
|
|
|
echo -e "\nTime for some details."
|
|
|
|
|
|
|
|
for (( ; ; )); do
|
2020-05-19 18:04:11 +00:00
|
|
|
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
|
2020-05-15 09:46:56 +00:00
|
|
|
done
|
|
|
|
read -e -i "n" -p "Another section? [yN]:" END
|
2020-05-12 08:20:41 +00:00
|
|
|
[[ "${END^^}" == "N" ]] && break
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2020-05-19 18:04:11 +00:00
|
|
|
template(){
|
|
|
|
FILE="$1"
|
|
|
|
cat << HEREDOC
|
|
|
|
|
|
|
|
$(date "+%Y-%m-%d %H:%M") $CHANGELOG_USERNAME
|
|
|
|
$( cat "$FILE" |sed 's/^/ /' )
|
|
|
|
HEREDOC
|
|
|
|
}
|
|
|
|
|
2020-05-15 09:46:56 +00:00
|
|
|
hookContentComment(){
|
|
|
|
[[ -z "$1" ]] && echo "Enter a simple comment. Ex: systemctl disable nginx" && return
|
|
|
|
TMP="$1"
|
|
|
|
read -e -p "Your comment: " CONTENT
|
|
|
|
echo "$CONTENT" >> $TMP
|
|
|
|
}
|
|
|
|
|
2020-05-16 00:38:09 +00:00
|
|
|
hookOutputFile(){
|
|
|
|
[[ -z "$1" ]] && echo "Writing a local changelog entry to $CHANGELOG" && return
|
2020-05-19 18:04:11 +00:00
|
|
|
local TMP=$(mktemp)
|
|
|
|
SOURCE="$1"
|
|
|
|
template "$SOURCE" > "$TMP"
|
|
|
|
[[ -f "$CHANGELOG" ]] && cat /etc/changelog >> "$TMP"
|
|
|
|
cat "$TMP" > $CHANGELOG
|
|
|
|
rm -f "$TMP"
|
2020-05-16 00:38:09 +00:00
|
|
|
|
|
|
|
}
|
2020-05-15 09:46:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Build the hook content menu
|
2020-05-17 23:10:06 +00:00
|
|
|
PLUGIN_CONTENT+=("hookContentComment")
|
|
|
|
declare -a PLUGIN_CONTENT_MENU
|
2020-05-15 09:46:56 +00:00
|
|
|
|
2020-05-16 00:38:09 +00:00
|
|
|
# Build the hook content menu
|
2020-05-17 23:10:06 +00:00
|
|
|
PLUGIN_OUTPUT+=("hookOutputFile")
|
|
|
|
declare -a PLUGIN_OUTPUT_MENU
|
|
|
|
|
|
|
|
# %PLUGIN% This comment is used to build custom scripts
|
2020-05-21 18:45:38 +00:00
|
|
|
[[ -d "$APP_PATH/plugins-enabled" ]] && [[ -n "$APP_PATH/plugins-enabled/*" ]] && for plugin in "$APP_PATH"/plugins-enabled/*; do source $plugin; done
|
2020-05-16 00:38:09 +00:00
|
|
|
|
2020-05-17 23:10:06 +00:00
|
|
|
for (( i = 0; i < ${#PLUGIN_OUTPUT[@]} ; i++ )); do
|
|
|
|
TXT=$( ${PLUGIN_OUTPUT[$i]} )
|
|
|
|
PLUGIN_OUTPUT_MENU+=("$( ${PLUGIN_OUTPUT[$i]} )")
|
|
|
|
done
|
|
|
|
for (( i = 0; i < ${#PLUGIN_CONTENT[@]} ; i++ )); do
|
|
|
|
TXT=$( ${PLUGIN_CONTENT[$i]} )
|
|
|
|
PLUGIN_CONTENT_MENU+=("$( ${PLUGIN_CONTENT[$i]} )")
|
|
|
|
done
|
2020-05-16 00:38:09 +00:00
|
|
|
|
2020-05-15 09:46:56 +00:00
|
|
|
|
2020-05-17 23:10:06 +00:00
|
|
|
# Exec
|
2020-05-15 09:46:56 +00:00
|
|
|
|
2020-05-12 08:20:41 +00:00
|
|
|
echo -e "\nWelcome to Serious Changelogs Inc. Do you have something for me?\n"
|
|
|
|
# set environment variables
|
|
|
|
[[ -z "$CHANGELOG_USERNAME" ]] && read -e -p "Your name? " NAME&& read -e -p "Your email? " EMAIL && CHANGELOG_USERNAME="${NAME^} <$EMAIL>" && echo "export CHANGELOG_USERNAME=\"$CHANGELOG_USERNAME\""
|
|
|
|
[[ " <>" == "$CHANGELOG_USERNAME" ]] && panic "srs.ly?"
|
|
|
|
|
|
|
|
# check changelog exists
|
2020-05-19 18:04:11 +00:00
|
|
|
[[ ! -f "$CHANGELOG" ]] && template "* Changelog: Added $CHANGELOG file\nIt was not serious. Fixed!\n\n" > "$CHANGELOG"
|
2020-05-12 08:20:41 +00:00
|
|
|
|
2020-06-03 18:52:22 +00:00
|
|
|
# Detect piped content
|
|
|
|
if [[ -n "$STDIN" ]] ; then
|
|
|
|
echo -e "$STDIN" > $ENTRY_FILE
|
|
|
|
|
|
|
|
#OR enter interactive mode
|
|
|
|
else
|
|
|
|
change "$ENTRY_FILE"
|
|
|
|
fi
|
2020-05-16 00:38:09 +00:00
|
|
|
|
2020-06-03 18:52:22 +00:00
|
|
|
# confirm the output
|
|
|
|
echo -e "\nHere is the content you are about to add:\n\n$(cat $ENTRY_FILE)\n"
|
|
|
|
read -e -p "OK? [Yn]: " -i y RETURN
|
|
|
|
[[ "N" == ${RETURN^^} ]] && exit 1
|
2020-05-17 23:10:06 +00:00
|
|
|
|
2020-06-03 18:52:22 +00:00
|
|
|
# Send data to outputs
|
2020-05-17 23:10:06 +00:00
|
|
|
for (( i=0; i<${#PLUGIN_OUTPUT_MENU[@]}; i++)) ; do
|
2020-05-19 18:04:11 +00:00
|
|
|
echo "${PLUGIN_OUTPUT_MENU[$i]}"
|
|
|
|
${PLUGIN_OUTPUT[$i]} $ENTRY_FILE
|
2020-05-16 00:38:09 +00:00
|
|
|
done
|
2020-05-12 08:20:41 +00:00
|
|
|
|
2020-05-16 00:38:09 +00:00
|
|
|
rm "$ENTRY_FILE"
|