~fmac/pasticcio

ref: 868041c64f92fe3999e1a4601f656d9f4ec6767c pasticcio/cmd/misc/sh/cvcv -rwxr-xr-x 1.9 KiB
868041c6 — Francesco Camuffo cvcv.sh: usage: explain RANDID 3 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/sh -eu

#
# Utility to interact with the Pasticcio pastebin.
#

PASTHOST=${PASTHOST:="https://cvcv.cz"}

usage() {
	printf "%s\n" "Usage: ${0##*/} [options...] [FILE] [RANDID]"
	printf "\t%s\n" "-h: print this help."
	printf "\t%s\n" "-p: create a paste."
	printf "\t%s\n" "-P: make it public."
	printf "\t%s\n" "-l: specify the RandID length."
	printf "\n"
	printf "\t%s\n" "FILE: file to submit. If empty or '-', read stdin."
	printf "\t%s\n" "RANDID: RandID to fetch (no -p)."
	printf "\n"
	printf "\t%s\n" "env:"
	printf "\t%s\n" "PASTHOST: remote host. Default: cvcv.cz."
	printf "\n"
	printf "\t%s\n" "example 1:"
	printf "\t%s\n" "\$ ${0##*/} -p \$(which ${0##*/})"
	printf "\t%s\n" "example 2:"
	printf "\t%s\n" "\$ echo \"Maynard J. Keenan\"|${0##*/} -p -"
	printf "\t%s\n" "example 3:"
	printf "\t%s\n" "\$ ${0##*/} nudqw7dt"
}

submit_paste() {
	curl \
		-X POST \
		-s \
		-D - \
		-o /dev/null \
		--data-urlencode rand_id_length="$randid_length" \
		--data-urlencode public="$make_it_public" \
		--data-urlencode paste_blob@"$1" \
		--no-buffer \
		"$PASTHOST/p/new" \
		2>/dev/null
}

fetch_paste() {
	curl \
		"$PASTHOST"/p/v/"$1".txt
}

create_paste=0
file_name="-"
randid_length=4
make_it_public="n"

while getopts hpl:P flag
do
	case $flag in
		h) usage && exit 0 ;;
		p) create_paste=1 ;;
		l) randid_length="$OPTARG" ;;
		P) make_it_public="y" ;;
		*) usage && exit 1 ;;
	esac
done
shift $(($OPTIND-1))

if [ $# -ne 1 ]; then
	usage
	exit 1
fi

if [ $create_paste -eq 1 ]; then
	file_name="$1"

	resp=$(submit_paste "$file_name")
	first_line=$(printf "%s" "$resp" | grep '^HTTP')
	if printf "%s" "$first_line" | grep ' 302 ' >/dev/null; then
		path="$( \
			printf "%s" "$resp" | \
			grep -i '^Location:' | \
			sed -n 's/^Location: \(\S*\).*$/\1/pi' \
			)"
		printf "%s%s.txt\n" "$PASTHOST" "$path"
	else
		printf "%s\n" "$resp" >&2
		exit 1
	fi
	exit 0
fi

randid="$1"

fetch_paste "$randid"