~dricottone/my-utils

dcf2479d67b2c1bc11bf3e35e625e8fc7bc78cf8 — Dominic Ricottone 2 years ago bc266dc
Documents scripts

Adding scripts used for a Hugo generated blog. Tests for basic CLI usage
are done, a README is TODO.
A documents/Makefile => documents/Makefile +19 -0
@@ 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/


A documents/hugo-bump => documents/hugo-bump +49 -0
@@ 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" <<EOF
g/^date:/c
i
date: "$2"
.
w
EOF
}

code=0
newdate="$(hugo-date)"
for filename in "${positional[@]}"; do
  if [[ ! -f "$filename" ]]; then
    nonfatal_error_msg "No such file '$filename'"
    code=1
  else
    if [[ "$verbose" -eq 1 ]]; then
      olddate="$(/usr/bin/grep "$filename" --regexp="^date:")"
      debug_msg "replacing ${olddate}..."
    fi
    bump "$filename" "$newdate"
  fi
done

exit "$code"


A documents/hugo-date => documents/hugo-date +17 -0
@@ 0,0 1,17 @@
#!/bin/sh

name="hugo-date"
version="1.0"
help_message=$(/usr/bin/cat <<-EOF
	Print current date and time in the Hugo/Go format
	Usage: hugo-date
	Options:
	 -h, --help     print this message and exit
	 -v, --version  print version number and exit
EOF
)

. /usr/local/lib/myminiparse.sh

date +%FT%T%:z


A documents/hugo-post => documents/hugo-post +56 -0
@@ 0,0 1,56 @@
#!/bin/sh

name="hugo-post"
version="1.0"
usage_message="Usage: hugo-post [OPTIONS] TITLE"
help_message=$(/usr/bin/cat <<-EOF
	Create a new Hugo post
	Usage: hugo-post [OPTIONS] TITLE
	Options:
	 -h, --help     print this message and exit
	 -q, --quiet    suppress error messages
	 -v, --version  print version number and exit
EOF
)

. /usr/local/lib/myminiparse.sh

# loop through arguments
title=
for arg; do
  case "$arg" in
  -q|--quiet)
    #ignore these
    ;;

  *)
    if [ -z "$title" ]; then
      title="$arg"
    fi
    ;;
  esac
done

# error if no title given
if [ -z "$title" ]; then
  (>&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"


A documents/test/hugo-bump.bats => documents/test/hugo-bump.bats +165 -0
@@ 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" = "" ]
}


A documents/test/hugo-date.bats => documents/test/hugo-date.bats +39 -0
@@ 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" = "" ]
}


A documents/test/hugo-post.bats => documents/test/hugo-post.bats +41 -0
@@ 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" = "" ]
}