diff --git a/bread/bin/bread b/bread/bin/bread index 79f2ea6..b9b5171 100755 --- a/bread/bin/bread +++ b/bread/bin/bread @@ -8,6 +8,14 @@ #shellcheck disable=1091 . /bread/site/lib.sh +groupck() +{ + groups "$USER" | grep -q "$1" || { + echo "You're not in the $1 group!" + exit 3 + } +} + usage() { cat <<-ENDOFUSAGE @@ -50,10 +58,7 @@ bread_news() cmd="grep -R \"$OPTARG\" \"$BREADNEWS\" -l" ;; w) - groups "$USER" | grep -q admin || { - echo "You're not an admin!" - exit 3 - } + groupck admin article="$BREADNEWS/$(date +%F)@$(date +%H:%M).md" cat <<-ENDOFARTICLE >"$article" title @@ -80,10 +85,18 @@ bread_news() bread_docs() { - while getopts hs:w opt; do + while getopts hs:ne: opt; do case "$opt" in h) cat <<-ENDOFHELP + bread docs : read helpful documents + usage: bread docs [-s QUERY] [-n] [-e QUERY] + + -s QUERY search for QUERY in documents + -n write a new document + : if it has the same name as another, + : it'll fail + -e QUERY edit a document matching QUERY ENDOFHELP exit ;; @@ -97,7 +110,37 @@ bread_docs() echo " 🍞 🥖 🥐 " return ;; - w) + n) + groupck bakers + doc="$(mktemp)" + cat <<-ENDOFDOC >"$doc" + title + author $USER + ENDOFDOC + $EDITOR "$doc" + fname="$BREADDOCS/$(M title "$doc"|slugify).md" + [[ "$fname" =~ */.md ]] && return + if [ -f "$fname" ]; then + echo "$fname already exists!" + echo "Your article is still at $doc." + exit 4 + else + cp "$doc" "$fname" + fi + return + ;; + e) + groupck bakers + matches=( $(grep -R "$OPTARG" "$BREADDOCS" -l) ) + if [[ "${#matches[@]}" -eq 1 ]]; then + $EDITOR "${matches[0]}" + return + fi + select doc in "${matches[@]}"; do + [ "$REPLY" = q ] && return + $EDITOR "$doc" + return + done ;; \?) return 2 ;; *) return 2 ;; @@ -105,6 +148,7 @@ bread_docs() done select doc in "$BREADDOCS"/*; do + [ "$REPLY" = q ] && return echo " 🍞 🥖 🥐 " printf '\033[34;1;4m%s\033[0m\n' "$(M title "$doc")" printf '\033[34;1m%s\033[0m\n' "$(M date "$doc")"