nostril

A C cli tool for creating nostr events
git clone git://jb55.com/nostril
Log | Files | Refs | Submodules | LICENSE

git-show-nostr (2827B)


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