~yerinalexey/dotfiles

ref: 144a38b0aa0a39003633d53b77fe4e3ca29c054b dotfiles/scripts/submit-paste -rwxr-xr-x 1.5 KiB
144a38b0Alexey Yerin river,update-volume: fix old wob crashes 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/sh

# Submits a paste to paste.sr.ht
# CAUTION: very ugly at the bottom

PASTE_BASE="${PASTE_BASE:-https://paste.sr.ht}"
SRHT_TOKEN="${SRHT_TOKEN:-$(cat "$XDG_CONFIG_HOME"/srht-token)}"

usage() {
	cat >&2 <<EOF
Usage: submit-paste [-n name | -N] [-v public|unlisted|private] [-d] <file>
Options:
  -n name       - set filename for the paste
  -N            - create unnamed paste
  -v visibility - set paste visibility: public, unlisted or private (default: unlisted)

Examples:
  Submit from a file:
  $ submit-paste main.rs

  Set another filename instead of auto-detecting:
  $ submit-paste -n "anotherfile.rs" file.rs

  Submit without a filename:
  $ submit-paste -N /tmp/log
EOF
	exit 1
}

# Options
visibility=unlisted
filename=""
unnamed=0

while getopts "n:Nv:dc" opt; do
	case "$opt" in
		n) filename="$OPTARG" ;;
		N) filename="" unnamed=1 ;;
		v) visibility="$OPTARG" ;;
		*) usage ;;
	esac
done
shift $((OPTIND - 1))

[ $# -lt 1 ] && usage
file="$1"

[ -z "$filename" ] && [ $unnamed -eq 0 ] && filename="$(basename "$file")"

jq -sR \
	--arg visibility "$visibility" \
	--arg filename "$filename" '{
  "visibility": $visibility,
  "files": [
    {
      "filename": (if ($filename | length) > 0 then $filename else null end),
      "contents": .,
    }
  ]
}' < "$file" \
	| curl -sS \
		-X POST \
		--oauth2-bearer "$SRHT_TOKEN" \
		-H Content-Type:application/json \
		-d @- \
		"$PASTE_BASE"/api/pastes \
	| jq -r \
		--arg base "$PASTE_BASE" \
		'$base + "/" + .user.canonical_name + "/" + .sha'