diff options
author | Marvin Borner | 2022-04-14 00:58:03 +0200 |
---|---|---|
committer | Marvin Borner | 2022-04-14 00:58:03 +0200 |
commit | 62146a790b7d11949dc987414d12c4eb4919e521 (patch) | |
tree | 931f1f38f07937e7764253304eb64cb519bc2fec /clients |
Diffstat (limited to 'clients')
-rw-r--r-- | clients/cli/README.md | 18 | ||||
-rwxr-xr-x | clients/cli/save | 101 |
2 files changed, 119 insertions, 0 deletions
diff --git a/clients/cli/README.md b/clients/cli/README.md new file mode 100644 index 0000000..ced2f31 --- /dev/null +++ b/clients/cli/README.md @@ -0,0 +1,18 @@ +# Save CLI + +## Requirements + +- `curl`, `sh`, `awk` (unix utilities) +- `jq` (JSON parser) +- `dmenu` (file selection menu; env-specifiable) +- `xclip` (copy to clipboard; env-specifiable) + +## Docs + +- see `./save -h` and source-code + +## Installation + +Just move the `save` script somewhere into your `$PATH` (e.g. +`~/.local/bin` `/usr/local/bin`). Also make sure that it's executable +(`chmod +x save`). diff --git a/clients/cli/save b/clients/cli/save new file mode 100755 index 0000000..5911969 --- /dev/null +++ b/clients/cli/save @@ -0,0 +1,101 @@ +#!/bin/sh + +set -e + +SAVE_TEXTVIEW=${SAVE_TEXTVIEW:-less} +SAVE_CLIP=${SAVE_CLIPCMD:-xclip -selection c -t} +SAVE_MENU=${SAVE_MENU:-dmenu} + +if [ -z "$SAVE_URL" ]; then + echo "Please specify the URL of your server first:" + printf "URL: " + read -r url + + echo + echo "Please add the following to your ~/.{bash_,zsh_,..}profile:" + echo " export SAVE_URL=$url" + echo "Then restart your current session or execute the export directly in the terminal (temporarily)" + exit 0 +fi + +if [ -z "$SAVE_TOKEN" ]; then + echo "Please login first:" + printf "username: " + read -r username + + stty -echo + printf "password: " + read -r password + stty echo + printf "\n" + + authorized=$(curl -d "username=$username&password=$password" -X POST "$SAVE_URL/login" 2>/dev/null) + echo + if [ "$(echo "$authorized" | jq -r .error)" = "success" ]; then + echo "Login was successful!" + echo "Please add the following to your ~/.{bash_,zsh_,..}profile:" + echo " export SAVE_TOKEN=$(echo "$authorized" | jq -r .token)" + echo "Then restart your current session or execute the export directly in the terminal (temporarily)" + else + echo "$authorized" | jq -r .error + fi + exit 0 +fi + +if [ "$1" = "-cf" ]; then + saved=$(curl -H "token: $SAVE_TOKEN" "$SAVE_URL/saved" 2>/dev/null) + selected=$(echo "$saved" | jq -r '.data | .[] | select(.type == "file") | "\(.name) \(.token)"' | $SAVE_MENU -l 5 -i) + mimetype=$(echo "$saved" | jq -r --arg TOKEN "$(echo "$selected" | awk '{print $NF}')" '.data | .[] | select(.token == $TOKEN).mimetype') + curl -H "token: $SAVE_TOKEN" "$SAVE_URL/file/$(echo "$selected" | awk '{print $NF}')" | $SAVE_CLIP "$mimetype" +elif [ "$1" = "-df" ]; then + saved=$(curl -H "token: $SAVE_TOKEN" "$SAVE_URL/saved" 2>/dev/null) + selected=$(echo "$saved" | jq -r '.data | .[] | select(.type == "file") | "\(.name) \(.token)"' | $SAVE_MENU -l 5 -i) + curl -H "token: $SAVE_TOKEN" "$SAVE_URL/file/$(echo "$selected" | awk '{print $NF}')" -o "$(echo "$selected" | awk 'NF--')" +elif [ "$1" = "-dt" ]; then + saved=$(curl -H "token: $SAVE_TOKEN" "$SAVE_URL/saved" 2>/dev/null) + echo "$saved" | jq -r '.data | .[] | select(.type == "text").data' | $SAVE_TEXTVIEW +elif [ "$1" = "-uf" ]; then + file=${2:-/dev/stdin} + echo "uploading $file..." + curl -H "token: $SAVE_TOKEN" -F "file=@$file" -X POST "$SAVE_URL/file" 2>/dev/null | jq -r .error +elif [ "$1" = "-ut" ]; then + printf "text: " + read -r text + curl -H "token: $SAVE_TOKEN" -d "data=$text" -X POST "$SAVE_URL/text" 2>/dev/null | jq -r .error +elif [ -z "$1" ]; then + if [ -p /dev/stdin ]; then + echo "Error: You need to apply arguments if you're piping!" + exit 1 + fi + + # TODO: This could be way more POSIX-y + read -p "[u]pload or [d]ownload? " -n 1 -r + echo + if [[ $REPLY =~ ^[Uu]$ ]]; then + read -p "upload [f]ile or [t]ext? " -n 1 -r + echo + if [[ $REPLY =~ ^[Ff]$ ]]; then + printf "file: " + read -e -r file + $0 -uf $file + elif [[ $REPLY =~ ^[Tt]$ ]]; then + $0 -ut + fi + elif [[ $REPLY =~ ^[Dd]$ ]]; then + read -p "download [f]ile or [t]ext? " -n 1 -r + echo + if [[ $REPLY =~ ^[Ff]$ ]]; then + read -p "to [c]lipboard or to [f]ile?" -n 1 -r + echo + if [[ $REPLY =~ ^[Cc]$ ]]; then + $0 -cf + elif [[ $REPLY =~ ^[Ff]$ ]]; then + $0 -df + fi + elif [[ $REPLY =~ ^[Tt]$ ]]; then + $0 -dt + fi + fi +else + printf "Don't panic!\n\nEnvironment variables:\n\tSAVE_URL\tthe URL of your save instance (e.g. example.com:8080)\n\tSAVE_TOKEN\tthe access token of your account\n\tSAVE_TEXTVIEW\tthe view for text (e.g. 'vim -R -', 'less')\n\tSAVE_CLIP\tthe clipboard manager command; adds mimetype as last argument (e.g. 'xclip -selection c -t')\n\tSAVE_MENU\tthe menu selector (e.g. dmenu)\nA magical prompt will show up if you haven't set the correct variables yet.\n\nUsage: $0 [OPTIONS]\nOptions:\n\t-h\tshow help\n\t-cf\tcopy files to clipboard using menu\n\t-df\tdownload files into current dir using menu\n\t-dt\tdownload/view all texts in a textview\n\t-uf\tupload a specified file (arg 2) or stdin (no arg)\n\t-ut\tupload a text (prompt)\n\t[none]\tinteractive CLI (only for non-stdin)\n" +fi |