summaryrefslogtreecommitdiffstats
path: root/fb.in
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2013-08-11 18:27:46 +0200
committerFlorian Pritz <bluewind@xinu.at>2013-08-11 18:27:46 +0200
commit1c514095696ac48f9a127d278c29608a3c89c767 (patch)
treeb6057411028fed2308708eac345b969426392437 /fb.in
parent8786eeac017c2247daebbcc57b0cf5594f25986a (diff)
Add -a option to generate a new api key
Signed-off-by: Florian Pritz <bluewind@xinu.at>
Diffstat (limited to 'fb.in')
-rw-r--r--fb.in52
1 files changed, 50 insertions, 2 deletions
diff --git a/fb.in b/fb.in
index 762eb88..c271605 100644
--- a/fb.in
+++ b/fb.in
@@ -47,6 +47,7 @@ get=
tar=
compress=0
display_history=
+create_apikey=
clipboard=""
exitcode=0
debug=
@@ -219,6 +220,11 @@ read_stdin() {
cat > "$1"
}
+read_string() {
+ read -r tmp
+ echo "$tmp"
+}
+
id_from_arg() {
if printf "%s" "$1" | grep -qE "^https?://"; then
printf "%s" "$1" | sed -r 's/https?:\/\/[^\/]+\/([^\/]+).*/\1/'
@@ -227,6 +233,44 @@ id_from_arg() {
fi
}
+create_apikey() {
+ if [ -z "$HOST" ]; then
+ HOST=`hostname`
+ fi
+
+ require_executable curl
+
+ printf "%s" "Username: "
+ read_string | tr -d "\n" > "$tmpdir/username"
+
+ printf "%s" "Password: "
+ stty -echo
+ read_string | tr -d "\n" > "$tmpdir/password"
+ stty echo
+ printf "\n"
+
+ curlopts="$default_curlopts"
+ if [ "$debug" ]; then
+ curlopts="$curlopts -v"
+ fi
+
+ curl $curlopts -w "%{http_code}\n" -s -F "username=<$tmpdir/username" -F "password=<$tmpdir/password" -F "comment=fb-client $USER@$HOST" "$pastebin/user/create_apikey" > "$tmpdir/api-result"
+
+ rm "$tmpdir/username" "$tmpdir/password"
+
+ status_code=`tail -n 1 "$tmpdir/api-result"`
+
+ if [ "$status_code" == "200" ]; then
+ head -n 1 "$tmpdir/api-result" > "$apikey_file"
+ return 0
+ fi
+
+ echo "Failed to generate API key:" >&2
+ sed '$d' "$tmpdir/api-result" >&2
+
+ return 1
+}
+
help() {
cat <<!
fb-client version $version
@@ -241,6 +285,7 @@ usage: [cat |] `basename "$0"` [switches] [options] [<file(s)|ID(s)|folder(s)>]
-h this help
-v show the client version
-H display an upload history
+ -a create a new api key
Options:
-e <extension> extension for default highlighting (e.g. "diff")
@@ -258,7 +303,7 @@ if ! type getopts >/dev/null 2>&1; then
exit 1
fi
-while getopts "e:n:gdhHtcvD" option; do
+while getopts "e:n:gdhHatcvD" option; do
case $option in
e) extension="$OPTARG";;
n) filename="$OPTARG";;
@@ -267,6 +312,7 @@ while getopts "e:n:gdhHtcvD" option; do
t) tar=1;;
d) delete=1;;
H) display_history=1;;
+ a) create_apikey=1;;
v) printf "%s\n" "$version"; exit 0;;
D) debug=1;;
h|\?) help; exit 0;;
@@ -306,7 +352,9 @@ if [ "$delete" ] || [ "$get" ]; then
done
elif [ "$display_history" ]; then
request_helper d "$pastebin/file/upload_history" || exitcode=1
-
+elif [ "$create_apikey" ]; then
+ create_apikey
+ exit $?
elif [ $# -eq 0 ]; then
if [ "$tar" ]; then
printf "%s\n" "Error: -t is not supported when operating on stdin" >&2