-
Notifications
You must be signed in to change notification settings - Fork 2
/
editr-client.sh
executable file
·75 lines (64 loc) · 1.64 KB
/
editr-client.sh
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
#!/usr/bin/env bash
# Heavily copy/pasted from https://github.com/aurora/rmate/blob/master/rmate
host="${EDITR_HOST:-localhost}"
port="${EDITR_PORT:-32123}"
hostname=`hostname`
force=false
function showusage {
echo "Usage: $0 [-H hostname] [-p port] [-f] file
-H connect to host (default: $host)
-p port number to use for connection (default: $port)
-f open even if file is not writable
-h display this usage information
"
}
while getopts H:p:fh OPTIONS; do
case $OPTIONS in
H)
host=$OPTARG;;
p)
port=$OPTARG;;
f)
force=true;;
h)
showusage
exit 1;;
?)
showusage
exit 1;;
*)
showusage
exit 1;;
esac
done
filename="${@:$OPTIND}"
base=`basename "$filename"`
if [ "$filename" = "" ]; then
echo "No file given"
showusage
exit 1
fi
if [ -d "$filename" ]; then
echo "Cannot edit a directory"
showusage
exit 1
fi
if [ ! -e "$filename" ]; then
exists="no"
else
exists="yes"
fi
if [ -f "$filename" ] && [ ! -w "$filename" ]; then
if [[ $force = false ]]; then
echo "File $filename is not writable! Use -f to open anyway."
exit 1
fi
fi
response=`curl --silent --fail -X POST --data-binary "@$filename" -H "X-Exists: $exists" -H "X-File-Name: $base" -H "X-Hostname: $hostname" "$host:$port"`
responseCode=$?
if [[ responseCode -eq 0 ]]; then
# Bash variables get trailing new lines removed
# The server adds a trailing character to preserve new lines
# here we strip away the extra character
echo -n "${response%X}" > "$filename"
fi