changelog/changelog.sh

121 lines
3.1 KiB
Bash
Executable File

#!/bin/bash
#Manages an /etc/changelog
# %CONFIG% This comment is used to build custom scripts
CHANGELOG=/etc/changelog
[[ -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 $0
exit 0
}
# functions
msg(){ echo -e "$@"; }
panic(){ msg "${@} Exiting."; exit 1;}
ENTRY_FILE=$(mktemp)
change(){
TMP="$1"
while true ; do
echo -e "\nType the nature of the change.\nExample: > mysql: do not start on boot\n"
read -e -p "> " SUB
echo -e "* $SUB" >> $TMP
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
done
read -e -i "n" -p "Another section? [yN]:" END
[[ "${END^^}" == "N" ]] && break
done
echo -e "\nHere is the content you are about to add.\n\n$(cat $TMP)\n"
read -e -p "OK? [Yn]: " -i y RETURN
[[ "N" == ${RETURN^^} ]] && return 1
}
hookContentComment(){
[[ -z "$1" ]] && echo "Enter a simple comment. Ex: systemctl disable nginx" && return
TMP="$1"
read -e -p "Your comment: " CONTENT
echo "$CONTENT" >> $TMP
}
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"
}
# Build the hook content menu
PLUGIN_CONTENT+=("hookContentComment")
declare -a PLUGIN_CONTENT_MENU
# Build the hook content menu
PLUGIN_OUTPUT+=("hookOutputFile")
declare -a PLUGIN_OUTPUT_MENU
# %PLUGIN% This comment is used to build custom scripts
[[ -d "plugins-enabled" ]] && [[ -n "plugins-enabled/*" ]] && source plugins-enabled/*
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
# Exec
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
[[ ! -f $CHANGELOG ]] && template "* Changelog: Added $CHANGELOG file\nIt was not serious. Fixed!\n\n"
# parse command
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
done
rm "$ENTRY_FILE"