git-show-nostr (2794B)
1 #!/usr/bin/env bash 2 3 set -eo pipefail 4 5 NOSTR_RELAYER=${NOSTR_RELAYER:-nostcat} 6 FUZZER=${FUZZER:-fzf} 7 8 check_cmd() { 9 if ! command -v $1 >&2 >/dev/null; then 10 printf "error: $1 is required. please install. \n" 11 exit 0 12 fi 13 } 14 15 if ! command -v $NOSTR_RELAYER >&2 >/dev/null; then 16 printf "error: nostr relayer '%s' not found, set \$NOSTR_RELAYER or install '%s'\n" $NOSTR_RELAYER $NOSTR_RELAYER 17 exit 1 18 fi 19 20 if ! command -v $FUZZER >&2 >/dev/null; then 21 printf "error: fuzzer '%s' not found, set \$FUZZER or install '%s'\n" $FUZZER $FUZZER 22 exit 1 23 fi 24 25 check_cmd "nostril" 26 check_cmd "nostril-query" 27 check_cmd "jq" 28 29 usage() { 30 printf "usage: git-show-nostr [OPTIONS...]\n" 31 printf "\n" 32 printf "OPTIONS\n\n" 33 printf " -O output patches to a local file.\n" 34 printf " -o name output patches to a local file with a specific filename.\n" 35 printf " -r relay nostr relay to query. Uses 'git config nostr.relays' option by default\n" 36 printf "\n" 37 printf "Including query OPTIONS from nostril-query:\n\n" 38 nostril query -h | grep -A100 ^options: 39 exit 0 40 } 41 42 if [ "$1" == "--help" ]; then 43 usage 44 exit 1 45 fi 46 47 limit=20 48 49 while true; do 50 if [ -z $1 ]; then 51 break 52 fi 53 case "$1" in 54 -O) 55 writeout=1 56 shift 57 ;; 58 -o) 59 writeout=1 60 fname=$OPTARG 61 shift 62 ;; 63 -r) 64 relay=$2 65 shift 66 shift 67 ;; 68 *) 69 break 70 ;; 71 esac 72 done 73 74 if [ -z $relay ]; then 75 relay="$(git config nostr.relays || :)" 76 fi 77 78 hashtag=$(git config nostr.hashtag || :) 79 if [[ "$hashtag" != "" ]]; then 80 hashtag="-t $hashtag" 81 fi 82 83 if [ -z "$relay" ]; then 84 usage 85 exit 0 86 fi 87 88 target=$tag 89 90 fullquery=$(nostril query --kinds 19691228 --limit $limit $hashtag "$@") 91 92 if [ -z $fname ]; then 93 target=${target:-all} 94 outname="nostr-patches-$target.json" 95 else 96 outname=$fname 97 fi 98 99 if [ -z $writeout ]; then 100 outname=$(mktemp) 101 fi 102 103 echo "$fullquery" | $NOSTR_RELAYER $relay | sort -u | jq -c '.[2]' > "$outname" 104 105 if [ ! -z $writeout ]; then 106 printf "saved results to %s\n" "$outname" >&2 107 fi 108 109 pager="$(git config core.pager || :)" 110 if [ ! -t 1 ]; then 111 pager=cat 112 fi 113 114 ev_count=$(wc -l < "$outname") 115 if [ $ev_count = 1 ]; then 116 jq -r .content "$outname" | $pager 117 exit 1 118 fi 119 120 dateformatter="" 121 if ! command -v datefmt >&2 >/dev/null; then 122 printf "install https://jb55.com/datefmt if you want nice relative timestamp formatting\n" >&2 123 dateformatter=cat 124 else 125 dateformatter="datefmt --relative" 126 fi 127 128 evid=$(jq -r ' 129 def tag(name): .tags[] | select(.[0] == name) | .[1]; 130 131 . | [(.created_at | tostring), ((.|tag("t")) // " "), (.|tag("subject")), (.|tag("author")), .pubkey[0:20], "id:\(.id)"] | @tsv' "$outname" | 132 sort -rn | 133 $dateformatter | 134 column -t -s $'\t' | 135 $FUZZER | 136 sed -E -n 's,.*id:([a-f0-9]{64}).*,\1,p' 137 ) 138 139 q=$(printf 'select(.id == "%s") | .content' "$evid") 140 jq -r "$q" "$outname" | $pager 141 142 if [ -z $writeout ]; then 143 rm -f "$outname" 144 fi