diff --git a/nag b/nag index 7f96f93..be81a86 100755 --- a/nag +++ b/nag @@ -1150,6 +1150,71 @@ stop() { printf "Stopped alarm %s.\\n" "${_target_id}" } +# skip ######################################################################## + +describe "skip" < + +Description: + Skip the next occurrence of a repeating alarm (reschedule without firing). + For one-shot alarms, this deletes them. +HEREDOC +skip() { + local _target_id="${1:-}" + [[ -n "${_target_id}" ]] || _exit_1 printf "Usage: %s skip \\n" "${_ME}" + + _acquire_lock + _read_alarms + + local -a _new_alarms=() + local _found=0 + local _line + + for _line in "${_ALARMS[@]:-}" + do + [[ -n "${_line}" ]] || continue + local _id _timestamp _rule _message + _id="$(_get_alarm_field "${_line}" 1)" + _timestamp="$(_get_alarm_field "${_line}" 2)" + _rule="$(_get_alarm_field "${_line}" 3)" + _message="$(_get_alarm_field "${_line}" 4)" + + if [[ "${_id}" == "${_target_id}" ]] + then + _found=1 + if [[ -n "${_rule}" ]] + then + local _next_ts _human_time + _next_ts="$(_next_occurrence "${_rule}" "${_timestamp}")" + _new_alarms+=("$(printf "%s\t%s\t%s\t%s" "${_id}" "${_next_ts}" "${_rule}" "${_message}")") + _human_time="$(_format_time "${_next_ts}")" + printf "Skipped. Next: %s\\n" "${_human_time}" + else + printf "Stopped alarm %s.\\n" "${_id}" + fi + else + _new_alarms+=("${_line}") + fi + done + + if [[ "${_found}" -eq 0 ]] + then + _release_lock + _exit_1 printf "No alarm with ID %s.\\n" "${_target_id}" + fi + + if [[ "${#_new_alarms[@]}" -eq 0 ]] + then + _ALARMS=() + : > "${_ALARMS_FILE}" + else + _ALARMS=("${_new_alarms[@]}") + _write_alarms + fi + _release_lock +} + # check ####################################################################### describe "check" <" ]] +} + +@test "skip reschedules a repeating alarm" { + run_nag every day "tomorrow 3pm" daily task + run_nag skip 1 + [ "${status}" -eq 0 ] + [[ "${output}" =~ "Skipped" ]] + [ -s "${NAG_DIR}/alarms" ] + local _ts _now + _ts="$(cut -f2 "${NAG_DIR}/alarms")" + _now="$(date +%s)" + (( _ts > _now )) +} + +@test "skip deletes a one-shot alarm" { + run_nag at "tomorrow 3pm" "one-shot" + run_nag skip 1 + [ "${status}" -eq 0 ] + [[ "${output}" =~ "Stopped" ]] + run_nag + [[ "${output}" =~ "Nothing to nag about" ]] +} + +@test "skip with nonexistent ID fails" { + run_nag skip 99 + [ "${status}" -eq 1 ] +} + +@test "skip without ID fails" { + run_nag skip + [ "${status}" -eq 1 ] +} + @test "help every shows every usage" { run_nag help every [ "${status}" -eq 0 ]