From d296e615e9c63985cf6e3b8f33bf1e6e1bbf6c47 Mon Sep 17 00:00:00 2001 From: alban Date: Mon, 18 May 2020 01:10:06 +0200 Subject: [PATCH] [fix] Bit of renaming things --- .gitignore | 2 + changelog => changelog.sh | 73 +++++++++++-------- hooks-output/changelog-server.sh | 15 ---- .../content-hastebin.sh | 14 ++-- plugins-available/output-changelog-server.sh | 18 +++++ plugins-enabled/.gitignore | 1 + 6 files changed, 71 insertions(+), 52 deletions(-) rename changelog => changelog.sh (62%) delete mode 100644 hooks-output/changelog-server.sh rename hooks-content/hastebin.sh => plugins-available/content-hastebin.sh (51%) create mode 100644 plugins-available/output-changelog-server.sh create mode 100644 plugins-enabled/.gitignore diff --git a/.gitignore b/.gitignore index 726c3ca..8e329c3 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ .*.sw* +config.sh +build/ diff --git a/changelog b/changelog.sh similarity index 62% rename from changelog rename to changelog.sh index ee7baa0..de9dc6a 100755 --- a/changelog +++ b/changelog.sh @@ -1,8 +1,18 @@ #!/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 @@ -19,17 +29,17 @@ change(){ for (( ; ; )); do echo "Select number or x to quit" - for (( i=0; i<${#HOOKS_CONTENT_MENU[@]}; i++)) ; do - echo "$i) ${HOOKS_CONTENT_MENU[$i]}" + 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 "${HOOKS_CONTENT[$REPLY]}" ]] ; then + elif [[ -z "${PLUGIN_CONTENT[$REPLY]}" ]] ; then echo "Unknown Key." else - ${HOOKS_CONTENT[$REPLY]} $TMP + ${PLUGIN_CONTENT[$REPLY]} $TMP fi done read -e -i "n" -p "Another section? [yN]:" END @@ -37,8 +47,7 @@ change(){ 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 - echo $TMP + [[ "N" == ${RETURN^^} ]] && return 1 } hookContentComment(){ @@ -54,6 +63,7 @@ hookOutputFile(){ TMP="$1" cat << HEREDOC >$t + $(date "+%Y-%m-%d %H:%M") $CHANGELOG_USERNAME $( echo -e "$@"|sed 's/^/ /' ) @@ -65,29 +75,30 @@ HEREDOC } + +# 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 -# Build the hook content menu -HOOKS_CONTENT+=("hookContentComment") -declare -a HOOKS_CONTENT_MENU -[[ -n "hooks-content/*" ]] && source hooks-content/* -for (( i = 0; i < ${#HOOKS_CONTENT[@]} ; i++ )); do - TXT=$( ${HOOKS_CONTENT[$i]} ) - HOOKS_CONTENT_MENU+=("$( ${HOOKS_CONTENT[$i]} )") -done - -# Build the hook content menu -HOOKS_OUTPUT+=("hookOutputFile") -declare -a HOOKS_OUTPUT_MENU -[[ -n "hooks-output/*" ]] && source hooks-output/* -for (( i = 0; i < ${#HOOKS_OUTPUT[@]} ; i++ )); do - TXT=$( ${HOOKS_OUTPUT[$i]} ) - HOOKS_OUTPUT_MENU+=("$( ${HOOKS_OUTPUT[$i]} )") -done - - - - 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\"" @@ -99,9 +110,11 @@ echo -e "\nWelcome to Serious Changelogs Inc. Do you have something for me?\n" # parse command change "$ENTRY_FILE" -for (( i=0; i<${#HOOKS_OUTPUT_MENU[@]}; i++)) ; do - echo "${HOOKS_OUTPUT_MENU[$i]}" - ${HOOKS_OUTPUT[$i]} $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" diff --git a/hooks-output/changelog-server.sh b/hooks-output/changelog-server.sh deleted file mode 100644 index 0e37be3..0000000 --- a/hooks-output/changelog-server.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash - -HOOKS_OUTPUT+=( hookOutputChangelogServer ) - -CHANGELOG_SERVER=http://localhost:3000 - -hookOutputChangelogServer(){ - - [[ -z "$1" ]] && echo "Sending changelog to remote server" && return - CONTENT=$(jq -aRs . $1|tr '"' '\"') - PAYLOAD="{\"author\":\"$CHANGELOG_USERNAME\",\"server\":\"$(hostname -f)\",\"content\":${CONTENT}}" - URL=$( curl -X POST -H "Content-Type: application/json" -s -d "${PAYLOAD}" "$CHANGELOG_SERVER/changelog/_doc"; ) - echo "$FILE: $URL" >> $TMP - -} diff --git a/hooks-content/hastebin.sh b/plugins-available/content-hastebin.sh similarity index 51% rename from hooks-content/hastebin.sh rename to plugins-available/content-hastebin.sh index f1f77d7..2b7a238 100644 --- a/hooks-content/hastebin.sh +++ b/plugins-available/content-hastebin.sh @@ -1,16 +1,16 @@ #!/bin/bash - -HOOKS_CONTENT+=( hookContentHastebin ) - -HASTEBIN_SERVER=https://paste.interhacker.space - +# Sends the changelog information to a changelog server +# see https://git.interhacker.space/alban/changelog-server +# Requires an environmental variable to work +# ex: HASTEBIN_SERVER=https://paste.interhacker.space +PLUGIN_CONTENT+=( hookContentHastebin ) hookContentHastebin(){ - [[ -z "$1" ]] && echo "Store a file by name in pastebin" && return + [[ -z "$HASTEBIN_SERVER" ]] && echo "Missing HASTEBIN_SERVER variable. Exiting." && return TMP="$1" read -e -p "File location? " FILE [[ ! -f $FILE ]] && panic "Not a valid file" && exit URL=$( curl -X POST -s -d "$(cat "$FILE" )" "$HASTEBIN_SERVER/documents" | awk -F '"' '{print "'$HASTEBIN_SERVER'/raw/"$4}'; ) echo "$FILE: $URL" >> $TMP - } + diff --git a/plugins-available/output-changelog-server.sh b/plugins-available/output-changelog-server.sh new file mode 100644 index 0000000..003c9a3 --- /dev/null +++ b/plugins-available/output-changelog-server.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# Sends the changelog information to a changelog server +# see https://git.interhacker.space/alban/changelog-server +# Requires environmental variables to work +# CHANGELOG_SERVER +# CHANGELOG_AUTH_KEY +PLUGIN_OUTPUT+=( hookOutputChangelogServer ) +hookOutputChangelogServer(){ + [[ -z "$1" ]] && echo "Sending changelog to remote server" && return + [[ -z "$CHANGELOG_SERVER" ]] && echo "Missing CHANGELOG_SERVER variable. Exiting." && return + [[ -z "$CHANGELOG_AUTH_KEY" ]] && echo "Missing CHANGELOG_AUTH_KEY variable. Exiting." && return + CONTENT=$(jq -aRs . $1|tr '"' '\"') + PAYLOAD="{\"author\":\"$CHANGELOG_USERNAME\",\"server\":\"$(hostname -f)\",\"content\":${CONTENT}}" + URL=$( curl -X POST -H "AuthorizationToken: $CHANGELOG_AUTH_KEY" -H "Content-Type: application/json" -s -d "${PAYLOAD}" "$CHANGELOG_SERVER/changelog/_doc"; ) + [[ 0 -ne $? ]] && echo "Failed to upload the changelog..." && return + echo "$FILE: $URL" >> $TMP +} + diff --git a/plugins-enabled/.gitignore b/plugins-enabled/.gitignore new file mode 100644 index 0000000..72e8ffc --- /dev/null +++ b/plugins-enabled/.gitignore @@ -0,0 +1 @@ +*