From bfe3c496941c3568e42e7f764d07112689f349bb Mon Sep 17 00:00:00 2001 From: Gustav Eek Date: Thu, 19 May 2022 19:26:02 +0200 Subject: [PATCH] [event-participant] Improve control flow Print summary if no STDIN is provided, instead of if input was empty. Create file if not existing. Create cache if not existing. Update help accordingly. TODO Provide arguments as ordinary flags: `-f` for file, `-n` for "other name", `-c` for cancel, argument can be "comment". TODO Provide long list option for printing: `-s` for summary. TODO Provide title row and header row for the CSV-file: `-t` for setting title. TODO Provide PGP encryption by default: `-r` for GPG recipiant. --- event-participant | 50 +++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/event-participant b/event-participant index 8bb525a..534ce69 100755 --- a/event-participant +++ b/event-participant @@ -12,7 +12,7 @@ formermost record is reused. Add extra participants (email taken as From field) with argument +NAME. -If input is empty, also list of particiants is printed. +If no input is provided, instead list a summary particiants is printed. echo | event-participant @@ -22,6 +22,11 @@ Help is this message. CACHE="$HOME/.cache/${0##*/}/events.csv" HEADER="participant,date_att,date_cancel" +if [ ! -f "$CACHE" ]; then + mkdir -p $(dirname "$CACHE") + echo -n "" > "$CACHE" +fi + if [[ "$1" == "--help" ]]; then echo -e "$HELP" 1>&2 exit 0 @@ -32,21 +37,30 @@ if [[ "$1" =~ \+.* ]]; then shift fi -arg_event="$1" - -if [ -f "$CACHE" ]; then - event=$(tail -1 "$CACHE") +if [ -n "$1" ]; then + event="$1" + if [ "$event" != "$(tail -1 "$CACHE")" ];then + echo "$event" >> "$CACHE" + fi else - mkdir -p $(dirname "$CACHE") - echo -n "" > "$CACHE" + event=$(tail -1 "$CACHE") fi -if [ -n "$arg_event" -a "$arg_event" != "$event" ]; then - event="$arg_event" - echo "$event" >> "$CACHE" + +if [ ! -f "$event" ]; then + echo -n > "$event" fi -if [ -z "$event" ]; then - echo "${0##*/}: E: No event file." 1>&2 - exit 1 + +# Print summary, if no input data provided + +if test -t 0; then + n_t=$(cat "$event" | grep -v "$HEADER"| cut -d, -f2 | grep -v '^$' | wc -l) + n_c=$(cat "$event" | grep -v "$HEADER"| cut -d, -f3 | grep -v '^$' | wc -l) + n_p=$(( $n_t - $n_c )) + if [ -z "$info" ]; then + cat "$event" | tr ',' '\t' + fi + echo -ne "At $event:\n part: $n_p\n canc: $n_c\n" 1>&2 + exit 0 fi # Add info, sorted by date and uniq @@ -77,13 +91,3 @@ echo "${participant},${date}" | grep -v "^$" \ > "$event.sorted" mv "$event.sorted" "$event" - -# Print summary - -n_t=$(cat "$event" | grep -v "$HEADER"| cut -d, -f2 | grep -v '^$' | wc -l) -n_c=$(cat "$event" | grep -v "$HEADER"| cut -d, -f3 | grep -v '^$' | wc -l) -n_p=$(( $n_t - $n_c )) -echo -ne "At $event:\n part: $n_p\n canc: $n_c\n" 1>&2 -if [ -z "$info" ]; then - cat "$event" | tr ',' '\t' -fi -- 2.39.2