From dcf2479d67b2c1bc11bf3e35e625e8fc7bc78cf8 Mon Sep 17 00:00:00 2001 From: Dominic Ricottone Date: Thu, 29 Sep 2022 11:52:40 -0500 Subject: [PATCH] Documents scripts Adding scripts used for a Hugo generated blog. Tests for basic CLI usage are done, a README is TODO. --- documents/Makefile | 19 ++++ documents/hugo-bump | 49 ++++++++++ documents/hugo-date | 17 ++++ documents/hugo-post | 56 ++++++++++++ documents/test/hugo-bump.bats | 165 ++++++++++++++++++++++++++++++++++ documents/test/hugo-date.bats | 39 ++++++++ documents/test/hugo-post.bats | 41 +++++++++ 7 files changed, 386 insertions(+) create mode 100644 documents/Makefile create mode 100755 documents/hugo-bump create mode 100755 documents/hugo-date create mode 100755 documents/hugo-post create mode 100644 documents/test/hugo-bump.bats create mode 100644 documents/test/hugo-date.bats create mode 100644 documents/test/hugo-post.bats diff --git a/documents/Makefile b/documents/Makefile new file mode 100644 index 0000000..c273393 --- /dev/null +++ b/documents/Makefile @@ -0,0 +1,19 @@ +BIN_DIR?=/usr/local/bin +LIB_DIR?=/usr/local/lib +COMP_DIR?=/usr/local/etc/bash_completion.d + +.PHONY: install uninstall test clean + +install: + install -m755 hugo-bump $(BIN_DIR)/hugo-bump + install -m755 hugo-date $(BIN_DIR)/hugo-date + install -m755 hugo-post $(BIN_DIR)/hugo-post + +uninstall: + rm $(BIN_DIR)/hugo-bump + rm $(BIN_DIR)/hugo-date + rm $(BIN_DIR)/hugo-post + +test: + bats test/ + diff --git a/documents/hugo-bump b/documents/hugo-bump new file mode 100755 index 0000000..16c689e --- /dev/null +++ b/documents/hugo-bump @@ -0,0 +1,49 @@ +#!/bin/bash + +name="hugo-bump" +version="1.0" +read -r -d '' help_message <<-EOF + Bump the date in Hugo content files + Usage: hugo-bump [OPTIONS] [TARGETS ..] + Options: + -h, --help print this message and exit + -q, --quiet suppress error messages + -v, --verbose show additional messages + -V, --version print version number and exit +EOF + +source /usr/local/lib/mylib.bash +. /usr/local/lib/myparse.bash + +# error if no filenames given +if [[ ${#positional[@]} -eq 0 ]]; then + usage_msg +fi + +bump() { + ed -s "$1" <&2 /usr/bin/printf "%s\n" "$usage_message") + exit 1 +fi + +# check if title can be made into a new filename +filename="$(/usr/bin/printf "$title" | /usr/bin/tr --squeeze-repeat ' ' '_' | /usr/bin/tr --complement --delete '[:alnum:]_-').md" +if [ -f "$filename" ]; then + if [ "$quiet" -eq 0 ]; then + (>&2 /usr/bin/printf "%s: '%s' already exists\n" "$name" "$filename") + fi + exit 1 +elif [ -d "$filename" ]; then + if [ "$quiet" -eq 0 ]; then + (>&2 /usr/bin/printf "%s: '%s' is a directory\n" "$name" "$filename") + fi + exit 1 +fi + +# create new file +/usr/bin/printf -- '---\ntitle: %s\ndate: %s\n---\n\n' "$title" "$(hugo-date)" >"$filename" + diff --git a/documents/test/hugo-bump.bats b/documents/test/hugo-bump.bats new file mode 100644 index 0000000..585da6a --- /dev/null +++ b/documents/test/hugo-bump.bats @@ -0,0 +1,165 @@ +#!/usr/bin/env bats +bats_require_minimum_version 1.5.0 + +@test "hugo-bump usage" { + run --separate-stderr hugo-bump + [ "$status" -eq 1 ] + [ "$output" = "" ] + [ "$stderr" = "Usage: hugo-bump [OPTIONS] [TARGETS ..]" ] +} + +@test "hugo-bump usage - quiet" { + run --separate-stderr hugo-bump --quiet + [ "$status" -eq 1 ] + [ "$output" = "" ] + [ "$stderr" = "Usage: hugo-bump [OPTIONS] [TARGETS ..]" ] +} + +@test "hugo-bump usage - quiet short" { + run --separate-stderr hugo-bump -q + [ "$status" -eq 1 ] + [ "$output" = "" ] + [ "$stderr" = "Usage: hugo-bump [OPTIONS] [TARGETS ..]" ] +} + +@test "hugo-bump version" { + run --separate-stderr hugo-bump --version + [ "$status" -eq 0 ] + [ "$output" = "hugo-bump 1.0" ] + [ "$stderr" = "" ] +} + +@test "hugo-bump version - quiet" { + run --separate-stderr hugo-bump --version --quiet + [ "$status" -eq 0 ] + [ "$output" = "hugo-bump 1.0" ] + [ "$stderr" = "" ] +} + +@test "hugo-bump version - quiet short" { + run --separate-stderr hugo-bump --version -q + [ "$status" -eq 0 ] + [ "$output" = "hugo-bump 1.0" ] + [ "$stderr" = "" ] +} + +@test "hugo-bump version short" { + run --separate-stderr hugo-bump -V + [ "$status" -eq 0 ] + [ "$output" = "hugo-bump 1.0" ] + [ "$stderr" = "" ] +} + +@test "hugo-bump version short - quiet" { + run --separate-stderr hugo-bump -V --quiet + [ "$status" -eq 0 ] + [ "$output" = "hugo-bump 1.0" ] + [ "$stderr" = "" ] +} + +@test "hugo-bump version short - quiet short" { + run --separate-stderr hugo-bump -V -q + [ "$status" -eq 0 ] + [ "$output" = "hugo-bump 1.0" ] + [ "$stderr" = "" ] +} + +@test "hugo-bump help" { + run --separate-stderr hugo-bump --help + [ "$status" -eq 0 ] + [ "${lines[0]}" = "Bump the date in Hugo content files" ] + [ "${lines[1]}" = "Usage: hugo-bump [OPTIONS] [TARGETS ..]" ] + [ "${lines[2]}" = "Options:" ] + [ "${lines[3]}" = " -h, --help print this message and exit" ] + [ "${lines[4]}" = " -q, --quiet suppress error messages" ] + [ "${lines[5]}" = " -v, --verbose show additional messages" ] + [ "${lines[6]}" = " -V, --version print version number and exit" ] + [ "$stderr" = "" ] +} + +@test "hugo-bump help - quiet" { + run --separate-stderr hugo-bump --help --quiet + [ "$status" -eq 0 ] + [ "${lines[0]}" = "Bump the date in Hugo content files" ] + [ "${lines[1]}" = "Usage: hugo-bump [OPTIONS] [TARGETS ..]" ] + [ "${lines[2]}" = "Options:" ] + [ "${lines[3]}" = " -h, --help print this message and exit" ] + [ "${lines[4]}" = " -q, --quiet suppress error messages" ] + [ "${lines[5]}" = " -v, --verbose show additional messages" ] + [ "${lines[6]}" = " -V, --version print version number and exit" ] + [ "$stderr" = "" ] +} + +@test "hugo-bump help - quiet short" { + run --separate-stderr hugo-bump --help -q + [ "$status" -eq 0 ] + [ "${lines[0]}" = "Bump the date in Hugo content files" ] + [ "${lines[1]}" = "Usage: hugo-bump [OPTIONS] [TARGETS ..]" ] + [ "${lines[2]}" = "Options:" ] + [ "${lines[3]}" = " -h, --help print this message and exit" ] + [ "${lines[4]}" = " -q, --quiet suppress error messages" ] + [ "${lines[5]}" = " -v, --verbose show additional messages" ] + [ "${lines[6]}" = " -V, --version print version number and exit" ] + [ "$stderr" = "" ] +} + +@test "hugo-bump help short" { + run --separate-stderr hugo-bump -h + [ "$status" -eq 0 ] + [ "${lines[0]}" = "Bump the date in Hugo content files" ] + [ "${lines[1]}" = "Usage: hugo-bump [OPTIONS] [TARGETS ..]" ] + [ "${lines[2]}" = "Options:" ] + [ "${lines[3]}" = " -h, --help print this message and exit" ] + [ "${lines[4]}" = " -q, --quiet suppress error messages" ] + [ "${lines[5]}" = " -v, --verbose show additional messages" ] + [ "${lines[6]}" = " -V, --version print version number and exit" ] + [ "$stderr" = "" ] +} + +@test "hugo-bump help short - quiet" { + run --separate-stderr hugo-bump -h --quiet + [ "$status" -eq 0 ] + [ "${lines[0]}" = "Bump the date in Hugo content files" ] + [ "${lines[1]}" = "Usage: hugo-bump [OPTIONS] [TARGETS ..]" ] + [ "${lines[2]}" = "Options:" ] + [ "${lines[3]}" = " -h, --help print this message and exit" ] + [ "${lines[4]}" = " -q, --quiet suppress error messages" ] + [ "${lines[5]}" = " -v, --verbose show additional messages" ] + [ "${lines[6]}" = " -V, --version print version number and exit" ] + [ "$stderr" = "" ] +} + +@test "hugo-bump help short - quiet short" { + run --separate-stderr hugo-bump -h -q + [ "$status" -eq 0 ] + [ "${lines[0]}" = "Bump the date in Hugo content files" ] + [ "${lines[1]}" = "Usage: hugo-bump [OPTIONS] [TARGETS ..]" ] + [ "${lines[2]}" = "Options:" ] + [ "${lines[3]}" = " -h, --help print this message and exit" ] + [ "${lines[4]}" = " -q, --quiet suppress error messages" ] + [ "${lines[5]}" = " -v, --verbose show additional messages" ] + [ "${lines[6]}" = " -V, --version print version number and exit" ] + [ "$stderr" = "" ] +} + +@test "hugo-bump no such file" { + run --separate-stderr hugo-bump foobarbaz + [ "$status" -eq 1 ] + [ "$output" = "" ] + [ "$stderr" = "hugo-bump: No such file 'foobarbaz'" ] +} + +@test "hugo-bump no such file - quiet" { + run --separate-stderr hugo-bump foobarbaz --quiet + [ "$status" -eq 1 ] + [ "$output" = "" ] + [ "$stderr" = "" ] +} + +@test "hugo-bump no such file - quiet short" { + run --separate-stderr hugo-bump foobarbaz -q + [ "$status" -eq 1 ] + [ "$output" = "" ] + [ "$stderr" = "" ] +} + diff --git a/documents/test/hugo-date.bats b/documents/test/hugo-date.bats new file mode 100644 index 0000000..3429e28 --- /dev/null +++ b/documents/test/hugo-date.bats @@ -0,0 +1,39 @@ +#!/usr/bin/env bats +bats_require_minimum_version 1.5.0 + +@test "hugo-date version" { + run --separate-stderr hugo-date --version + [ "$status" -eq 0 ] + [ "$output" = "hugo-date 1.0" ] + [ "$stderr" = "" ] +} + +@test "hugo-date version short" { + run --separate-stderr hugo-date -v + [ "$status" -eq 0 ] + [ "$output" = "hugo-date 1.0" ] + [ "$stderr" = "" ] +} + +@test "hugo-date help" { + run --separate-stderr hugo-date --help + [ "$status" -eq 0 ] + [ "${lines[0]}" = "Print current date and time in the Hugo/Go format" ] + [ "${lines[1]}" = "Usage: hugo-date" ] + [ "${lines[2]}" = "Options:" ] + [ "${lines[3]}" = " -h, --help print this message and exit" ] + [ "${lines[4]}" = " -v, --version print version number and exit" ] + [ "$stderr" = "" ] +} + +@test "hugo-date help short" { + run --separate-stderr hugo-date -h + [ "$status" -eq 0 ] + [ "${lines[0]}" = "Print current date and time in the Hugo/Go format" ] + [ "${lines[1]}" = "Usage: hugo-date" ] + [ "${lines[2]}" = "Options:" ] + [ "${lines[3]}" = " -h, --help print this message and exit" ] + [ "${lines[4]}" = " -v, --version print version number and exit" ] + [ "$stderr" = "" ] +} + diff --git a/documents/test/hugo-post.bats b/documents/test/hugo-post.bats new file mode 100644 index 0000000..ea467c9 --- /dev/null +++ b/documents/test/hugo-post.bats @@ -0,0 +1,41 @@ +#!/usr/bin/env bats +bats_require_minimum_version 1.5.0 + +@test "hugo-post version" { + run --separate-stderr hugo-post --version + [ "$status" -eq 0 ] + [ "$output" = "hugo-post 1.0" ] + [ "$stderr" = "" ] +} + +@test "hugo-post version short" { + run --separate-stderr hugo-post -v + [ "$status" -eq 0 ] + [ "$output" = "hugo-post 1.0" ] + [ "$stderr" = "" ] +} + +@test "hugo-post help" { + run --separate-stderr hugo-post --help + [ "$status" -eq 0 ] + [ "${lines[0]}" = "Create a new Hugo post" ] + [ "${lines[1]}" = "Usage: hugo-post [OPTIONS] TITLE" ] + [ "${lines[2]}" = "Options:" ] + [ "${lines[3]}" = " -h, --help print this message and exit" ] + [ "${lines[4]}" = " -q, --quiet suppress error messages" ] + [ "${lines[5]}" = " -v, --version print version number and exit" ] + [ "$stderr" = "" ] +} + +@test "hugo-post help short" { + run --separate-stderr hugo-post -h + [ "$status" -eq 0 ] + [ "${lines[0]}" = "Create a new Hugo post" ] + [ "${lines[1]}" = "Usage: hugo-post [OPTIONS] TITLE" ] + [ "${lines[2]}" = "Options:" ] + [ "${lines[3]}" = " -h, --help print this message and exit" ] + [ "${lines[4]}" = " -q, --quiet suppress error messages" ] + [ "${lines[5]}" = " -v, --version print version number and exit" ] + [ "$stderr" = "" ] +} + -- 2.45.2