git-nostr-tools

git nostr tools
git clone git://jb55.com/git-nostr-tools
Log | Files | Refs

git-send-nostr (2489B)


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