nostril

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

git-send-nostr (2206B)


      1 #!/usr/bin/env bash
      2 
      3 NOSTR_RELAYER=${NOSTR_RELAYER:-nostcat}
      4 
      5 if ! command -v $NOSTR_RELAYER >&2 >/dev/null; then
      6 	printf "error: nostr relayer '%s' not found, set \$NOSTR_RELAYER or install '%s'\n" $NOSTR_RELAYER $NOSTR_RELAYER
      7 	exit 1
      8 fi
      9 
     10 usage() {
     11 	printf "usage: git-send-nostr [OPTIONS...] <commit> [NOSTRIL OPTIONS...]\n\n"
     12 
     13 	printf "OPTIONS\n\n"
     14 	printf "     -r            Relay to broadcast to. Will use 'git config nostr.relays' by default\n"
     15 	printf "     -d            Dry run. Just print event to stdout instead of relaying.\n"
     16 	printf "\n"
     17 	printf "     any other nostril option is supported here:\n\n"
     18 	printf "NOSTRIL OPTIONS\n"
     19 	nostril | sed '1,/OPTIONS/d'
     20 	printf "\nEXAMPLES\n\n"
     21 	printf "git send-nostr -d HEAD^- -t nostril -t nostrildev --pow 15\n\n"
     22 	exit 0
     23 }
     24 
     25 while getopts "dr:t:" o; do
     26     case "${o}" in
     27         r)
     28             relay=${OPTARG}
     29             ;;
     30         t)
     31             tag=${OPTARG}
     32             ;;
     33         d)
     34             dryrun=1
     35             ;;
     36         *)
     37             usage
     38             ;;
     39     esac
     40 done
     41 shift $((OPTIND-1))
     42 
     43 
     44 if [ -z $relay ]; then
     45 	relay=$(git config nostr.relays)
     46 	if [[ $relay == "" ]]; then
     47 		unset relay
     48 	fi
     49 fi
     50 
     51 if [ -z $1 ]; then
     52 	usage
     53 	exit 0
     54 fi
     55 
     56 commit=$1
     57 shift
     58 
     59 # this can be overridden
     60 sec="$(git config nostr.secretkey)"
     61 if [[ $sec != "" ]]; then
     62 	sec="--sec $sec"
     63 fi
     64 
     65 patch="$(git format-patch --stdout $commit)"
     66 subject=$(<<<"$patch" grep "^Subject:"| head -n1 | sed 's,^Subject: ,,')
     67 author=$(<<<"$patch" grep "^From:"| head -n1 | sed 's,^From: ,,')
     68 json=$(nostril --envelope $sec --kind 19691228 --tag author "$author" --tag subject "$subject" --content "$patch" "$@")
     69 
     70 id=$(jq -r '.[1].id' <<<"$json")
     71 
     72 if [ -n "$dryrun" ]; then
     73 	echo "$json"
     74 	printf "\nDRY RUN. printing event data\n" >&2
     75 elif [ -z "$relay" ]; then
     76 	echo "$json"
     77 	printf "\nRELAY NOT SET, not relaying. Use -r wss://relay.damus.io or 'git config --global nostr.relays wss://relay.damus.io'\n" >&2
     78 else
     79 	echo "$id"
     80 	printf "relaying to $relay using $NOSTR_RELAYER...\n" >&2
     81 	<<<$json $NOSTR_RELAYER "$relay"
     82 fi
     83 
     84 if [[ $sec == "" ]]; then
     85 	printf "NOSTR SECRET KEY NOT SET, using random key. Use --sec <key> or 'git config --global nostr.secretkey <hexkey>'\n" >&2
     86 fi