feat: moved to an actual directory, moving from /nag[f] to /nag/alarms

This commit is contained in:
Lewis Wynne 2026-04-02 00:18:58 +01:00
parent 5a71505dc2
commit 9de60f23cc
3 changed files with 22 additions and 24 deletions

27
nag
View file

@ -35,8 +35,9 @@ IFS=$'\n\t'
_ME="$(basename "${0}")" _ME="$(basename "${0}")"
_VERSION="2026.14" _VERSION="2026.14"
NAG_PATH="${NAG_PATH:-${HOME}/.local/share/nag}" NAG_DIR="${NAG_DIR:-${HOME}/.local/share/nag}"
_LOCKFILE="${NAG_PATH}.lock" _ALARMS_FILE="${NAG_DIR}/alarms"
_LOCKFILE="${NAG_DIR}/alarms.lock"
# The command nag runs to execute its notifications. # The command nag runs to execute its notifications.
NAG_CMD="${NAG_CMD:-notify-send}" NAG_CMD="${NAG_CMD:-notify-send}"
@ -371,11 +372,9 @@ _ALARMS=()
# _ensure_nag_dir # _ensure_nag_dir
# #
# Description: # Description:
# Create the parent directory for NAG_PATH if it doesn't already exist. # Create the nag directory if it doesn't already exist.
_ensure_nag_dir() { _ensure_nag_dir() {
local _dir [[ -d "${NAG_DIR}" ]] || mkdir -p "${NAG_DIR}"
_dir="$(dirname "${NAG_PATH}")"
[[ -d "${_dir}" ]] || mkdir -p "${_dir}"
} }
# Usage: # Usage:
@ -409,18 +408,18 @@ _release_lock() {
# _read_alarms # _read_alarms
# #
# Description: # Description:
# Read alarms from NAG_PATH into the global _ALARMS array. Each element # Read alarms from _ALARMS_FILE into the global _ALARMS array. Each element
# is one raw TSV line. If the file is missing or empty, _ALARMS is set to # is one raw TSV line. If the file is missing or empty, _ALARMS is set to
# an empty array. # an empty array.
_read_alarms() { _read_alarms() {
_ALARMS=() _ALARMS=()
if [[ -f "${NAG_PATH}" ]] && [[ -s "${NAG_PATH}" ]] if [[ -f "${_ALARMS_FILE}" ]] && [[ -s "${_ALARMS_FILE}" ]]
then then
local _line local _line
while IFS= read -r _line || [[ -n "${_line}" ]] while IFS= read -r _line || [[ -n "${_line}" ]]
do do
[[ -n "${_line}" ]] && _ALARMS+=("${_line}") [[ -n "${_line}" ]] && _ALARMS+=("${_line}")
done < "${NAG_PATH}" || true done < "${_ALARMS_FILE}" || true
fi fi
} }
@ -428,20 +427,20 @@ _read_alarms() {
# _write_alarms # _write_alarms
# #
# Description: # Description:
# Write the _ALARMS array atomically to NAG_PATH. Writes to a temporary # Write the _ALARMS array atomically to _ALARMS_FILE. Writes to a temporary
# file first, then moves it over the original. If _ALARMS is empty, an # file first, then moves it over the original. If _ALARMS is empty, an
# empty file is written. # empty file is written.
_write_alarms() { _write_alarms() {
_ensure_nag_dir _ensure_nag_dir
local _tmp local _tmp
_tmp="$(mktemp "${NAG_PATH}.XXXXXX")" _tmp="$(mktemp "${_ALARMS_FILE}.XXXXXX")"
if (( ${#_ALARMS[@]} > 0 )) if (( ${#_ALARMS[@]} > 0 ))
then then
printf "%s\n" "${_ALARMS[@]}" > "${_tmp}" printf "%s\n" "${_ALARMS[@]}" > "${_tmp}"
else else
: > "${_tmp}" : > "${_tmp}"
fi fi
mv -f "${_tmp}" "${NAG_PATH}" mv -f "${_tmp}" "${_ALARMS_FILE}"
} }
# Usage: # Usage:
@ -978,7 +977,7 @@ Description:
List all alarms. This is the default when no subcommand is given. List all alarms. This is the default when no subcommand is given.
HEREDOC HEREDOC
list() { list() {
if [[ ! -f "${NAG_PATH}" ]] || [[ ! -s "${NAG_PATH}" ]] if [[ ! -f "${_ALARMS_FILE}" ]] || [[ ! -s "${_ALARMS_FILE}" ]]
then then
printf "Nothing to nag about.\\n" printf "Nothing to nag about.\\n"
return 0 return 0
@ -1063,7 +1062,7 @@ stop() {
if [[ "${#_new_alarms[@]}" -eq 0 ]] if [[ "${#_new_alarms[@]}" -eq 0 ]]
then then
_ALARMS=() _ALARMS=()
: > "${NAG_PATH}" : > "${_ALARMS_FILE}"
else else
_ALARMS=("${_new_alarms[@]}") _ALARMS=("${_new_alarms[@]}")
_write_alarms _write_alarms

View file

@ -51,11 +51,11 @@ load test_helper
run_nag at "tomorrow 3pm" "take a break" run_nag at "tomorrow 3pm" "take a break"
[ "${status}" -eq 0 ] [ "${status}" -eq 0 ]
[[ "${output}" =~ "[1] Tomorrow, 3pm — take a break" ]] [[ "${output}" =~ "[1] Tomorrow, 3pm — take a break" ]]
[ -f "${NAG_PATH}" ] [ -f "${NAG_DIR}/alarms" ]
[ "$(wc -l < "${NAG_PATH}")" -eq 1 ] [ "$(wc -l < "${NAG_DIR}/alarms")" -eq 1 ]
# Verify TSV structure: id<TAB>timestamp<TAB><TAB>message # Verify TSV structure: id<TAB>timestamp<TAB><TAB>message
local _line local _line
_line="$(cat "${NAG_PATH}")" _line="$(cat "${NAG_DIR}/alarms")"
[[ "${_line}" =~ ^1$'\t'[0-9]+$'\t'$'\t'take\ a\ break$ ]] [[ "${_line}" =~ ^1$'\t'[0-9]+$'\t'$'\t'take\ a\ break$ ]]
} }
@ -161,7 +161,7 @@ load test_helper
[[ "${output}" =~ "standup meeting" ]] [[ "${output}" =~ "standup meeting" ]]
# Verify TSV has rule in field 3. # Verify TSV has rule in field 3.
local _rule local _rule
_rule="$(cut -f3 "${NAG_PATH}")" _rule="$(cut -f3 "${NAG_DIR}/alarms")"
[ "${_rule}" = "weekday" ] [ "${_rule}" = "weekday" ]
} }
@ -169,7 +169,7 @@ load test_helper
run_nag every "tuesday,thursday" "tomorrow 3pm" standup run_nag every "tuesday,thursday" "tomorrow 3pm" standup
[ "${status}" -eq 0 ] [ "${status}" -eq 0 ]
[[ "${output}" =~ "(tue, thu)" ]] [[ "${output}" =~ "(tue, thu)" ]]
grep -q "tue,thu" "${NAG_PATH}" grep -q "tue,thu" "${NAG_DIR}/alarms"
} }
@test "every snaps to next matching day" { @test "every snaps to next matching day" {
@ -177,7 +177,7 @@ load test_helper
run_nag every weekend "tomorrow 3pm" relax run_nag every weekend "tomorrow 3pm" relax
[ "${status}" -eq 0 ] [ "${status}" -eq 0 ]
local _ts _dow local _ts _dow
_ts="$(cut -f2 "${NAG_PATH}")" _ts="$(cut -f2 "${NAG_DIR}/alarms")"
_dow="$(date -d "@${_ts}" +%u)" _dow="$(date -d "@${_ts}" +%u)"
# Day-of-week should be 6 (Sat) or 7 (Sun). # Day-of-week should be 6 (Sat) or 7 (Sun).
[[ "${_dow}" == "6" || "${_dow}" == "7" ]] [[ "${_dow}" == "6" || "${_dow}" == "7" ]]

View file

@ -4,14 +4,13 @@
_NAG="$(cd "$(dirname "${BATS_TEST_FILENAME}")/.." && pwd)/nag" _NAG="$(cd "$(dirname "${BATS_TEST_FILENAME}")/.." && pwd)/nag"
setup() { setup() {
export NAG_PATH export NAG_DIR
NAG_PATH="$(mktemp)" NAG_DIR="$(mktemp -d)"
rm -f "${NAG_PATH}"
export NAG_CMD="true" export NAG_CMD="true"
} }
teardown() { teardown() {
rm -f "${NAG_PATH}" "${NAG_PATH}.lock" rm -rf "${NAG_DIR}"
} }
run_nag() { run_nag() {