-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpasttle.bashrc
334 lines (303 loc) · 8.93 KB
/
pasttle.bashrc
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/bin/bash
SW_VERSION="0.7"
UPSTREAM_URL="https://raw.github.com/thekad/pasttle/main/pasttle.bashrc"
function gettle() {
# default values
local encrypt="yes";
local filename="-";
local verbose="no";
local insecure="no";
local checkupdate="yes";
local clipboard="no";
local clipin="";
local clipargs="";
local command="";
local password="";
local upstream_version="$SW_VERSION";
# You can override this via environment variable
local rcfile=${PASTTLERC:-~/.pasttlerc}
version="gettle/${SW_VERSION}/curl/$( curl --version | head -1 | cut -d\ -f2- )";
# load user preferences
if [ -f $rcfile ];
then
echo "Loading ${rcfile}" > /dev/stderr
source $rcfile;
fi;
local usage="\n
USAGE\n\n
gettle [options] -e <entry number>\n\n
OPTIONS\n\n
-a API URL, this is, the root URL of the service (var: apiurl)\n\n
-n Do not encrypt your password before sending it (var: encrypt)\n\n
-p 'PASSWORD' If the entry is password-protected (var: password)\n\n
-f 'FILENAME' (OPTIONAL) Output the content to the given file (ver: filename)\n\n
-i (OPTIONAL) If you want to skip on SSL errors (var: insecure)\n\n
-v (OPTIONAL) Print verbose output (var: verbose)\n\n
-C (OPTIONAL) Don't check for updates from upstream (var: checkupdate)\n\n
-x (OPTIONAL) Put contents in clipboard, requires xclip in linux (var: clipboard)\n\n
"
# load runtime options
OPTIND=1;
while getopts ":a:np:f:hviCxe:" flag;
do
case $flag in
h)
echo -e $usage;
return 0;
;;
a)
apiurl="$OPTARG"
;;
n)
encrypt="no"
;;
p)
password="$OPTARG"
;;
f)
filename="$OPTARG"
;;
v)
verbose="yes"
;;
i)
insecure="yes"
;;
e)
entry="$OPTARG"
;;
C)
checkupdate="no"
;;
x)
clipboard="yes"
;;
\?)
echo "Invalid option: -${flag}"
;;
:)
echo "Option -${flag} requires an argument"
;;
esac;
done;
if [ -z "$entry" ];
then
echo "Tell me what entry you want";
return 1;
fi;
if [ -z "$apiurl" ];
then
echo "You don't have any apiurl in ~/.pasttlerc or missing -a parameter";
return 1;
else
if [ "yes" == "$verbose" ];
then
echo "API URL is ${apiurl}";
fi;
fi;
if [ "yes" == "$checkupdate" ];
then
upstream_version="$(curl -s $UPSTREAM_URL | grep "^SW_VERSION" | cut -d\" -f2)";
diff <(echo "$SW_VERSION") <(echo "$upstream_version" ) > /dev/null ||
echo "The upstream version ($upstream_version from $UPSTREAM_URL) differs from your version ($SW_VERSION). Time to update?"
fi;
command="curl -A '${version}'"
if [ ! -z "$password" ];
then
if [ "yes" == "$encrypt" ];
then
finalpass=$( echo -n "${password}" | sha1sum | cut -c 1-40 );
if [ "yes" == "$verbose" ];
then
echo "echo -n '${password}' | sha1sum | cut -c 1-40 # == ${finalpass}";
fi;
command="${command} -d 'is_encrypted=yes'";
else
finalpass="$password"
fi;
command="${command} -d 'password=${finalpass}'";
fi;
if [ "yes" == "$insecure" ];
then
command="${command} --insecure";
fi;
command="${command} -o${filename} ${apiurl}/raw/${entry}";
if [ "yes" == "$verbose" ];
then
command="${command} -v";
echo $command;
fi;
if [ "yes" == "$clipboard" ];
then
case "$( uname -s )" in
Linux)
clipin="xclip";
clipargs="-in -selection clipboard"
;;
Darwin)
clipin="pbcopy";
;;
esac;
if which $clipin &> /dev/null;
then
command="${command} | tee >(${clipin} ${clipargs})";
fi;
fi;
eval $command;
echo;
}
function pasttle() {
# default values
local encrypt="yes";
local filename="-";
local verbose="no";
local insecure="no";
local checkupdate="yes";
local clipboard="no";
local clipin="";
local clipargs="";
local command="";
local syntax=""
local password="";
local upstream_version="$SW_VERSION";
# You can override this via environment variable
local rcfile=${PASTTLERC:-~/.pasttlerc}
version="pasttle/${SW_VERSION}/curl/$( curl --version | head -1 | cut -d\ -f2- )";
# load user preferences
if [ -f $rcfile ];
then
echo "Loading ${rcfile}" > /dev/stderr
source $rcfile;
fi;
local usage="\n
USAGE\n\n
cat 'filename.ext' | pasttle [options]\n\n
or\n\n
pasttle -f 'filename.ext' [options]\n\n
OPTIONS\n\n
-a API URL, this is, the root URL of the service (var: apiurl)\n\n
-n Do not encrypt your password before sending it (var: encrypt)\n\n
-p 'PASSWORD' If you want to protect this entry with a password (var: password)\n\n
-f 'FILENAME' (OPTIONAL) Upload the given plain-text file (var: filename)\n\n
-i (OPTIONAL) If you want to skip on SSL errors (var: insecure)\n\n
-v (OPTIONAL) Print verbose output (var: verbose)\n\n
-s (OPTIONAL) Force the syntax of the paste (var: syntax)\n\n
-C (OPTIONAL) Don't check for updates from upstream (var: checkupdate)\n\n
-x (OPTIONAL) Put resulting URL in clipboard, requires xclip in linux (var: clipboard)\n\n
"
# load runtime options
OPTIND=1;
while getopts ":a:s:np:f:hvCix" flag;
do
case $flag in
h)
echo -e $usage;
return 0;
;;
a)
apiurl="$OPTARG"
;;
s)
syntax="$OPTARG"
;;
n)
encrypt="no"
;;
p)
password="$OPTARG"
;;
f)
filename="$OPTARG"
;;
v)
verbose="yes"
;;
i)
insecure="yes"
;;
x)
clipboard="yes"
;;
C)
checkupdate="no"
;;
\?)
echo "Invalid option: -${flag}"
;;
:)
echo "Option -${flag} requires an argument"
;;
esac;
done;
if [ -z "$apiurl" ];
then
echo "You don't have any apiurl in ~/.pasttlerc or missing -a parameter";
return 1;
else
if [ "yes" == "$verbose" ];
then
echo "API URL is ${apiurl}";
fi;
fi;
if [ "yes" == "$checkupdate" ];
then
upstream_version="$(curl -s $UPSTREAM_URL | grep "^SW_VERSION" | cut -d\" -f2)";
diff <(echo "$SW_VERSION") <(echo "$upstream_version" ) > /dev/null ||
echo "The upstream version ($upstream_version from $UPSTREAM_URL) differs from your version ($SW_VERSION). Time to update?"
fi;
if [ -z "$syntax" ];
then
syntax="${filename#*.}"
if [ "yes" == "$verbose" ];
then
echo "Syntax is ${syntax}";
fi;
fi;
command="curl -s -A '${version}' -F 'upload=<${filename}' -F 'filename=${filename}' -F 'syntax=${syntax}'"
if [ ! -z "$password" ];
then
if [ "yes" == "$encrypt" ];
then
finalpass=$( echo -n "${password}" | sha1sum | cut -c 1-40 );
if [ "yes" == "$verbose" ];
then
echo "echo -n '${password}' | sha1sum | cut -c 1-40 # == ${finalpass}";
fi;
command="${command} -F 'is_encrypted=yes'";
else
finalpass="$password"
fi;
command="${command} -F 'password=${finalpass}'";
fi;
if [ "yes" == "$insecure" ];
then
command="${command} --insecure";
fi;
command="${command} ${apiurl}/post";
if [ "yes" == "$verbose" ];
then
command="${command} -v";
fi;
if [ "yes" == "$clipboard" ];
then
case "$( uname -s )" in
Linux)
clipin="xclip";
clipargs="-in -selection clipboard"
;;
Darwin)
clipin="pbcopy";
;;
esac;
if which $clipin &> /dev/null;
then
command="${command} | tee >(${clipin} ${clipargs})";
fi;
fi;
if [ "yes" == "$verbose" ];
then
echo $command;
fi;
eval $command;
echo;
}