nostril

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

commit 75a62601609397fdee5fa0597cd9a686e9979547
parent 1904c78efa80b1e47515c1d5f51a9a99ed0d17f2
Author: William Casarin <jb55@jb55.com>
Date:   Wed, 16 Nov 2022 21:13:19 -0800

scripts: add git-{send,show}-nostr

Diffstat:
Ascripts/git-send-nostr | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ascripts/git-show-nostr | 141+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 223 insertions(+), 0 deletions(-)

diff --git a/scripts/git-send-nostr b/scripts/git-send-nostr @@ -0,0 +1,82 @@ +#!/usr/bin/env bash + +NOSTR_RELAYER=${NOSTR_RELAYER:-nostcat} + +if ! command -v $NOSTR_RELAYER >&2 >/dev/null; then + printf "error: nostr relayer '%s' not found, set \$NOSTR_RELAYER or install '%s'\n" $NOSTR_RELAYER $NOSTR_RELAYER + exit 1 +fi + +usage() { + printf "usage: git-send-nostr [OPTIONS...] <commit> [NOSTRIL OPTIONS...]\n\n" + + printf "OPTIONS\n\n" + printf " -r Relay to broadcast to. Will use 'git config nostr.relays' by default\n" + printf " -d Dry run. Just print event to stdout instead of relaying.\n" + printf "\n" + printf " any other nostril option is supported here:\n\n" + printf "NOSTRIL OPTIONS\n" + nostril | sed '1,/OPTIONS/d' + printf "\nEXAMPLES\n\n" + printf "git send-nostr -d HEAD^- -t nostril -t nostrildev --pow 15\n\n" + exit 0 +} + +while getopts "dr:t:" o; do + case "${o}" in + r) + relay=${OPTARG} + ;; + t) + tag=${OPTARG} + ;; + d) + dryrun=1 + ;; + *) + usage + ;; + esac +done +shift $((OPTIND-1)) + + +if [ -z $relay ]; then + relay=$(git config nostr.relays) + if [ $relay = "" ]; then + unset relay + fi +fi + +if [ -z $1 ]; then + usage + exit 0 +fi + +commit=$1 +shift + +# this can be overridden +sec="$(git config nostr.secretkey)" +if [ $sec != "" ]; then + sec="--sec $sec" +fi + +patch="$(git format-patch --stdout $commit)" +subject=$(<<<"$patch" grep "^Subject:"| head -n1 | sed 's,^Subject: ,,') +author=$(<<<"$patch" grep "^From:"| head -n1 | sed 's,^From: ,,') +json=$(nostril --envelope $sec --kind 19691228 --tag author "$author" --tag subject "$subject" --content "$patch" "$@") + +id=$(jq -r '.[1].id' <<<"$json") + +if [ -n "$dryrun" ]; then + printf "DRY RUN. printing event data\n\n" >&2 + echo "$json" +elif [ -z "$relay" ]; then + printf "RELAY NOT SET, not relaying.\n\n" >&2 + echo "$json" +else + echo "$id" + printf "relaying to $relay using $NOSTR_RELAYER...\n" >&2 + <<<$json $NOSTR_RELAYER "$relay" +fi diff --git a/scripts/git-show-nostr b/scripts/git-show-nostr @@ -0,0 +1,141 @@ +#!/usr/bin/env bash + +set -eo pipefail + +NOSTR_RELAYER=${NOSTR_RELAYER:-nostcat} +FUZZER=${FUZZER:-fzf} + +if ! command -v $NOSTR_RELAYER >&2 >/dev/null; then + printf "error: nostr relayer '%s' not found, set \$NOSTR_RELAYER or install '%s'\n" $NOSTR_RELAYER $NOSTR_RELAYER + exit 1 +fi + +if ! command -v $FUZZER >&2 >/dev/null; then + printf "error: fuzzer '%s' not found, set \$FUZZER or install '%s'\n" $FUZZER $FUZZER + exit 1 +fi + +if ! command -v jq >&2 >/dev/null; then + printf "error: jq is required. please install. \n" + exit 1 +fi + +if ! command -v fzf >&2 >/dev/null; then + printf "error: fzf is required. please install. \n" + exit 1 +fi + +usage() { + printf "usage: git-show-nostr <-t tag OR id> [OPTIONS...]\n" + printf "\n" + printf " -O output patches to a local file.\n" + printf " -o name output patches to a local file with a specific filename.\n" + printf " -l limit Max number of results to return. defaults to 20.\n" + printf " -t tag Query a nostr tag.\n" + printf " -r relay nostr relay to query. Uses 'git config nostr.relays' option by default\n" + exit 0 +} + +if [ "$1" == "--help" ]; then + usage + exit 1 +fi + +limit=20 +while getopts "Oo:t:r:l:" o; do +case "$o" in + O) + writeout=1 + ;; + o) + writeout=1 + fname=$OPTARG + ;; + r) + relay=$OPTARG + ;; + t) + tag=$OPTARG + ;; + l) + limit=$OPTARG + ;; + *) + usage + ;; +esac +done +shift $((OPTIND-1)) + +if [ -z $relay ]; then + relay="$(git config nostr.relays)" +fi + +if [ -z $relay ]; then + usage + exit 0 +fi + +target=$tag +query="" +if [ -z $tag ]; then + target=$id + query=$(printf '"ids":["%s"],' $id) +else + query=$(printf '"#t":["%s"],' $tag) +fi + +fullquery=$(printf '["REQ","git-show-nostr",{"kinds":[19691228],%s"limit": %d}]\n' "$query" $limit) + +if [ -z $fname ]; then + target=${target:-all} + outname="nostr-patches-$target.json" +else + outname=$fname +fi + +if [ -z $writeout ]; then + outname=$(mktemp) +fi + +echo "$fullquery" | $NOSTR_RELAYER $relay | jq -c '.[2]' > "$outname" + +if [ ! -z $writeout ]; then + printf "saved results to %s\n" "$outname" >&2 +fi + +pager="$(git config core.pager)" +if [ ! -t 1 ]; then + pager=cat +fi + +ev_count=$(wc -l < "$outname") +if [ $ev_count = 1 ]; then + jq -r .content "$outname" | $pager + exit 1 +fi + +dateformatter="" +if ! command -v datefmt >&2 >/dev/null; then + printf "install https://jb55.com/datefmt if you want nice relative timestamp formatting\n" >&2 + dateformatter=cat +else + dateformatter="datefmt --relative" +fi + +evid=$(jq -r ' +def tag(name): .tags[] | select(.[0] == name) | .[1]; + +. | [(.created_at | tostring), (.|tag("t")), (.|tag("author")), (.|tag("subject")), .pubkey[0:20], "id:\(.id)"] | @tsv' "$outname" | +sort -rn | +$dateformatter | +column -t -s $'\t' | +$FUZZER | +sed -E -n 's,.*id:(.*)$,\1,p' +) + +jq -r 'select(.id == "'$evid'") | .content' "$outname" | $pager + +if [ -z $writeout ]; then + rm -f "$outname" +fi