blob: 46cca536c4542ce66b18b139d9b15ff3e6382ca5 (
plain)
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
|
# Shell helpers for estampa. Source this from your shell rc:
#
# export ESTAMPA_HOST=estampa@paste.example.com
# . /path/to/extras/estampa.sh
#
# Optional:
# export ESTAMPA_URL=https://paste.example.com
# Defaults to "https://" + the host part of $ESTAMPA_HOST.
#
# Functions:
# estampaup ./file.txt upload a file, print the URL.
# estampaste file.txt upload the clipboard as <file.txt>, print the URL.
#
# estampaste uses wl-paste on Wayland and pbpaste on macOS.
estampaup() {
if [ $# -ne 1 ]; then
printf 'usage: estampaup <file>\n' >&2
return 2
fi
if [ ! -f "$1" ]; then
printf 'estampaup: not a file: %s\n' "$1" >&2
return 1
fi
local host="${ESTAMPA_HOST:?set ESTAMPA_HOST=estampa@paste.example.com}"
local url="${ESTAMPA_URL:-https://${host#*@}}"
local name
name=$(basename -- "$1")
scp -q "$1" "$host:./$name" || return $?
printf '%s/%s\n' "$url" "$name"
}
estampaste() {
if [ $# -ne 1 ]; then
printf 'usage: estampaste <filename>\n' >&2
return 2
fi
local host="${ESTAMPA_HOST:?set ESTAMPA_HOST=estampa@paste.example.com}"
local url="${ESTAMPA_URL:-https://${host#*@}}"
local name="$1"
local clip
if command -v wl-paste >/dev/null 2>&1; then
clip=wl-paste
elif command -v pbpaste >/dev/null 2>&1; then
clip=pbpaste
else
printf 'estampaste: no clipboard tool (need wl-paste or pbpaste)\n' >&2
return 1
fi
local tmp
tmp=$(mktemp) || return 1
# >| overrides zsh/bash `noclobber`; mktemp just created the file
# and we intentionally want to write over it.
if ! $clip >|"$tmp"; then
rm -f "$tmp"
return 1
fi
if [ ! -s "$tmp" ]; then
printf 'estampaste: clipboard is empty\n' >&2
rm -f "$tmp"
return 1
fi
# mktemp creates 600; scp preserves source mode, so without this
# the upload lands unreadable to the nginx user.
chmod 644 "$tmp"
if ! scp -q "$tmp" "$host:./$name"; then
rm -f "$tmp"
return 1
fi
rm -f "$tmp"
printf '%s/%s\n' "$url" "$name"
}
|