diff options
| author | Paul Buetow <paul@buetow.org> | 2021-05-12 10:07:06 +0100 |
|---|---|---|
| committer | Paul Buetow <git@mx.buetow.org> | 2021-05-21 05:11:04 +0100 |
| commit | 5cab54c35ef2b256b31754fb3df0a376becee897 (patch) | |
| tree | 61885ac66f781fa00b9087a0e7150492e97e9ee5 | |
| parent | bdf56f978f490f97e89afb6650e3a1913abd8e28 (diff) | |
better logging and better testing
| -rw-r--r-- | Makefile | 2 | ||||
| -rwxr-xr-x | buetow.org.sh | 1 | ||||
| -rw-r--r-- | modules/assert.source.sh | 20 | ||||
| -rw-r--r-- | modules/atomfeed.source.sh | 18 | ||||
| -rw-r--r-- | modules/gemfeed.source.sh | 3 | ||||
| -rw-r--r-- | modules/generate.source.sh | 26 | ||||
| -rw-r--r-- | modules/log.source.sh | 26 |
7 files changed, 86 insertions, 10 deletions
@@ -6,7 +6,7 @@ publish: git commit -a git push test: shellcheck - ./buetow.org.sh --test + LOG_VERBOSE=yes ./buetow.org.sh --test shellcheck: shellcheck \ --norc \ diff --git a/buetow.org.sh b/buetow.org.sh index bd89d648..7dd09338 100755 --- a/buetow.org.sh +++ b/buetow.org.sh @@ -15,6 +15,7 @@ source ./modules/atomfeed.source.sh source ./modules/gemfeed.source.sh source ./modules/generate.source.sh source ./modules/html.source.sh +source ./modules/log.source.sh source ./modules/md.source.sh help () { diff --git a/modules/assert.source.sh b/modules/assert.source.sh index 3367809d..d7c507a4 100644 --- a/modules/assert.source.sh +++ b/modules/assert.source.sh @@ -1,10 +1,11 @@ assert::equals () { local -r result="$1"; shift local -r expected="$1"; shift + local -r callee=${FUNCNAME[1]} if [ "$result" != "$expected" ]; then - cat <<ERROR -Expected + cat <<ERROR | log::pipe ERROR +In $callee expected '$expected' But got '$result' @@ -12,5 +13,18 @@ ERROR exit 2 fi - echo "Assert OK: $expected" + log VERBOSE "Result in $callee as expected: '$expected'" +} + +assert::not_empty () { + local -r name="$1"; shift + local -r content="$1"; shift + local -r callee=${FUNCNAME[1]} + + if [ -z "$content" ]; then + log ERROR "In $callee expected '$name' not to be empty!" + exit 2 + fi + + log VERBOSE "Result in $callee as expected not empty" } diff --git a/modules/atomfeed.source.sh b/modules/atomfeed.source.sh index e9402119..d449cd86 100644 --- a/modules/atomfeed.source.sh +++ b/modules/atomfeed.source.sh @@ -2,6 +2,8 @@ atomfeed::meta () { local -r gmi_file_path="$1"; shift local -r meta_file=$($SED 's|gemtext|meta|; s|.gmi$|.meta|;' <<< "$gmi_file_path") + log VERBOSE "Generating meta info for post $gmi_file_path" + local is_draft=no if grep -E -q '\.draft\.meta$' <<< "$meta_file"; then is_draft=yes @@ -36,6 +38,8 @@ META atomfeed::content () { local -r gmi_file_path="$1"; shift + log VERBOSE "Retrieving feed content from $gmi_file_path" + # sed: Remove all before the first header # sed: Make HTML links absolute, Atom relative URLs feature seems a mess # across different Atom clients. @@ -50,6 +54,9 @@ atomfeed::generate () { local -r gemfeed_dir="$CONTENT_DIR/gemtext/gemfeed" local -r atom_file="$gemfeed_dir/atom.xml" local -r now=$($DATE --iso-8601=seconds) + log INFO "Generating Atom feed to $atom_file" + + assert::not_empty now "$now" cat <<ATOMHEADER > "$atom_file.tmp" <?xml version="1.0" encoding="utf-8"?> @@ -68,6 +75,13 @@ ATOMHEADER # Get HTML content for the feed local content="$(atomfeed::content "$gemfeed_dir/$gmi_file")" + assert::not_empty meta_title "$meta_title" + assert::not_empty meta_date "$meta_date" + assert::not_empty meta_author "$meta_author" + assert::not_empty meta_email "$meta_email" + assert::not_empty meta_summary "$meta_summary" + assert::not_empty content "$content" + cat <<ATOMENTRY >> "$atom_file.tmp" <entry> <title>$meta_title</title> @@ -94,11 +108,11 @@ ATOMFOOTER # Delete the 3rd line of the atom feeds (global feed update timestamp) if ! diff -u <($SED 3d "$atom_file") <($SED 3d "$atom_file.tmp"); then - echo "Feed got something new!" + log INFO 'Feed got something new!' mv "$atom_file.tmp" "$atom_file" test "$ADD_GIT" == yes && git add "$atom_file" else - echo "Nothing really new in the feed" + log INFO 'Nothing really new in the feed' rm "$atom_file.tmp" fi } diff --git a/modules/gemfeed.source.sh b/modules/gemfeed.source.sh index 66ed48fc..c68c5070 100644 --- a/modules/gemfeed.source.sh +++ b/modules/gemfeed.source.sh @@ -12,6 +12,8 @@ gemfeed::updatemainindex () { local -r index_gmi="$CONTENT_DIR/gemtext/index.gmi" local -r gemfeed_dir="$CONTENT_DIR/gemtext/gemfeed" + log VERBOSE "Updating $index_gmi with posts from $gemfeed_dir" + # Remove old gemfeeds from main index $SED '/^=> .\/gemfeed\/[0-9].* - .*/d;' "$index_gmi" > "$index_gmi.tmp" # Add current gemfeeds to main index @@ -24,6 +26,7 @@ gemfeed::updatemainindex () { # This generates a index.gmi in the ./gemfeed subdir. gemfeed::generate () { local -r gemfeed_dir="$CONTENT_DIR/gemtext/gemfeed" + log INFO "Generating Gemfeed index for $gemfeed_dir" cat <<GEMFEED > "$gemfeed_dir/index.gmi.tmp" # $DOMAIN's Gemfeed diff --git a/modules/generate.source.sh b/modules/generate.source.sh index b82b5671..171c31cf 100644 --- a/modules/generate.source.sh +++ b/modules/generate.source.sh @@ -65,6 +65,8 @@ generate::convert_gmi_atom_to_html_atom () { local -r format="$1"; shift test "$format" != html && return + log INFO 'Converting Gemtext Atom feed to HTML Atom feed' + $SED 's|.gmi|.html|g; s|gemini://|https://|g' \ < $CONTENT_DIR/gemtext/gemfeed/atom.xml \ > $CONTENT_DIR/html/gemfeed/atom.xml @@ -82,19 +84,31 @@ generate::fromgmi_cleanup () { } generate::fromgmi () { - find "$CONTENT_DIR/gemtext" -type f -name \*.gmi | while read -r src; do + local -i num_gmi_files=0 + local -i num_doc_files=0 + + log INFO "Generating $* from Gemtext" + + while read -r src; do + (( num_gmi_files++ )) for format in "$@"; do generate::fromgmi_ "$src" "$format" done - done + done < <(find "$CONTENT_DIR/gemtext" -type f -name \*.gmi) + + log INFO "Converted $num_gmi_files Gemtext files" # Add non-.gmi files to html dir. - find "$CONTENT_DIR/gemtext" -type f | grep -E -v '(.gmi|atom.xml|.tmp)$' | + log VERBOSE "Adding other docs to $*" + while read -r src; do + (( num_doc_files++ )) for format in "$@"; do generate::fromgmi_add_docs "$src" "$format" done - done + done < <(find "$CONTENT_DIR/gemtext" -type f | grep -E -v '(.gmi|atom.xml|.tmp)$') + + log INFO "Added $num_doc_files other documents to each of $*" # Add atom feed for HTML for format in "$@"; do @@ -107,4 +121,8 @@ generate::fromgmi () { generate::fromgmi_cleanup "$src" "$format" done done + + for format in "$@"; do + log INFO "$format can be found in $CONTENT_DIR/$format now" + done } diff --git a/modules/log.source.sh b/modules/log.source.sh new file mode 100644 index 00000000..55d693ec --- /dev/null +++ b/modules/log.source.sh @@ -0,0 +1,26 @@ +log () { + local -r level="$1"; shift + + for message in "$@"; do + echo "$message" + done | log::_pipe "$level" +} + +log::pipe () { + log::_pipe "$1" +} + +log::_pipe () { + local -r level="$1"; shift + + if [[ "$level" == VERBOSE && -z "$LOG_VERBOSE" ]]; then + return + fi + + local -r callee=${FUNCNAME[2]} + local -r stamp=$($DATE +%Y%m%d-%H%M%S) + + while read -r line; do + echo "$level|$stamp|$callee|$line" >&2 + done +} |
