commit 5c73ec28412183ad314af392df2dceb333d73c03
parent 0878a83fa82ab49fda9ec8447f3394a62ad84b28
Author: Jake Bauer <jbauer@paritybit.ca>
Date: Tue, 22 Feb 2022 16:21:36 -0500
Refactor into functions+switch-case
Diffstat:
M | sbs | | | 93 | ++++++++++++++++++++++++++++++++++++++++++++----------------------------------- |
1 file changed, 52 insertions(+), 41 deletions(-)
diff --git a/sbs b/sbs
@@ -5,29 +5,16 @@
# Copyright (C) 2022 Jake Bauer
# Licensed under the terms of the ISC License, see LICENSE for details.
-if [ ! -x "$(command -v lowdown)" ]; then
- echo "The program 'lowdown' is needed but was not found."; exit 1
-fi
-
-if [ -z "$1" ] || [ "$1" = "help" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
- echo "Usage: sbs <command> [FILE ...]"; exit 0
-fi
-
-if [ "$1" = "version" ] || [ "$1" = "-v" ] || [ "$1" = "--version" ]; then
- echo "v0.1.0"; exit 0
-fi
-
set -o errexit
set -o nounset
-# Note: The script does not run measurably faster if files are written to using
-# one versus many printfs. Therefore, I use many printf statements to ease
-# reading, understanding, and modification of the code.
-# (This was tested with an Intel Core 2 Duo P7550 and an SSD running at SATA-II
-# speeds, so unless a user is generating their site on a 386, it should be fine)
+if [ ! -x "$(command -v lowdown)" ]; then
+ echo "The program 'lowdown' is needed but was not found."; exit 1
+fi
# Create a new page or a new site
-if [ "$1" = "new" ]; then
+new()
+{
if [ "$2" = "page" ]; then
{ printf "Title: \nSummary: \n\n"
printf "# [%%title]\n\n"
@@ -76,28 +63,30 @@ if [ "$1" = "new" ]; then
exit 1
fi
exit 0
-fi
+}
+
+parse_configuration()
+{
+ options="siteURL siteName blogDir languageCode buildOptions"
+ for key in $options; do
+ value=$(grep "$key" config.ini | cut -d'=' -f2 | xargs)
+ if [ -n "$value" ]; then
+ eval "$key='$value'"
+ else
+ printf "Error: %s not configured." "$key"
+ exit 1
+ fi
+ done
-# Parse the config file (everything after this requires the config)
-options="siteURL siteName blogDir languageCode buildOptions"
-for key in $options; do
- value=$(grep "$key" config.ini | cut -d'=' -f2 | xargs)
- if [ -n "$value" ]; then
- eval "$key='$value'"
- else
- printf "Error: %s not configured." "$key"
+ # Validate configuration
+ if ! echo "$siteURL" | grep -qE '^https?://.*\..*/$'; then
+ echo "Error: siteURL should be in canonical form (e.g. https://example.com/)"
exit 1
fi
-done
+}
-# Validate configuration
-if ! echo "$siteURL" | grep -qE '^https?://.*\..*/$'; then
- echo "Error: siteURL should be in canonical form (e.g. https://example.com/)"
- exit 1
-fi
-
-# Construct a complete atom feed if requested
-if [ "$1" = "genfeed" ]; then
+# Construct a complete atom feed
+genfeed() {
{ printf '<?xml version="1.0" encoding="utf-8"?>\n'
printf '<feed xmlns="http://www.w3.org/2005/Atom">\n'
printf "\t<title>%s</title>\n" "$siteName"
@@ -141,14 +130,14 @@ if [ "$1" = "genfeed" ]; then
printf "Created: static/feed.xml with %s entries.\n" "$numEntries"
rm "$tmp"
exit 0
-fi
+}
# Build the pages given as arguments
-if [ "$1" = "build" ]; then
- shift
+build()
+{
for file in "$@"; do
if [ -d "$file" ]; then
- "$0" "$file"/*
+ "$0" build "$file"/*
continue
elif [ ! -f "$file" ]; then
printf "ERROR: %s does not exist.\n" "$file"
@@ -175,5 +164,27 @@ if [ "$1" = "build" ]; then
printf "Created: static%s/%s.html\n" "$subDir" "$fileName"
done
-fi
+}
+
+case "$1" in
+ "build")
+ shift
+ parse_configuration
+ build "$@"
+ ;;
+ "genfeed")
+ parse_configuration
+ genfeed
+ ;;
+ "new")
+ new "$@"
+ ;;
+ "version")
+ echo "v0.1.0" ;
+ ;;
+ *)
+ echo "Usage: sbs <command> [FILE ...]"
+ ;;
+esac
+
exit 0