Refactor the script and change defaults

This commit is contained in:
Ceda EI 2021-09-09 20:47:05 +05:30
parent 7f19a6cf82
commit 9436f1213b
2 changed files with 18 additions and 15 deletions

View File

@ -2,7 +2,7 @@
Command line client for [mkr/bin](https://github.com/MKRhere/bin) and Command line client for [mkr/bin](https://github.com/MKRhere/bin) and
[hastebin](https://github.com/seejohnrun/haste-server/). Uses [hastebin](https://github.com/seejohnrun/haste-server/). Uses
[bin.mkr.pw](https://bin.mkr.pw) as the default instance. [bin.webionite.com](https://bin.webionite.com) as the default instance.
## Requirements ## Requirements

27
mbin.sh
View File

@ -2,30 +2,33 @@
# Set MKR_BIN if not set. Export it in your shell rc to use a self hosted # Set MKR_BIN if not set. Export it in your shell rc to use a self hosted
# instance. Include the trailing slash with custom urls. # instance. Include the trailing slash with custom urls.
MKR_BIN=${MKR_BIN:-https://bin.mkr.pw/} MKR_BIN=${MKR_BIN:-https://bin.webionite.com/}
HASTEBIN=${HASTEBIN:0} HASTEBIN=${HASTEBIN:-1}
EDITOR=${EDITOR:-vim} EDITOR=${EDITOR:-vim}
URL=$MKR_BIN$([[ $HASTEBIN == 1 ]] && echo documents)
if [[ $1 == "-c" || $1 == "--clipboard" ]]; then if [[ $1 == "-c" || $1 == "--clipboard" ]]; then
text=$(xclip -selection clipboard -out) text=$(xclip -selection clipboard -out)
op=$(curl -s --data-binary "${text}" -H "Content-Type:text/plain" $MKR_BIN$([[ $HASTEBIN == 1 ]] && echo documents)) op=$(curl -s --data-binary "${text}" -H "Content-Type:text/plain" "$URL")
elif [[ -f $1 ]]; then
op=$(curl -s --data-binary "@$1" -H "Content-Type:text/plain" "$URL")
elif [ -t 0 ]; then elif [ -t 0 ]; then
tmp_file=$(mktemp) tmp_file=$(mktemp)
$EDITOR $tmp_file "$EDITOR" "$tmp_file"
op=$(curl -s --data-binary "@${tmp_file}" -H "Content-Type:text/plain" $MKR_BIN$([[ $HASTEBIN == 1 ]] && echo documents)) op=$(curl -s --data-binary "@${tmp_file}" -H "Content-Type:text/plain" "$URL")
rm $tmp_file rm "$tmp_file"
else else
op=$(curl -s --data-binary "@-" -H "Content-Type:text/plain" $MKR_BIN$([[ $HASTEBIN == 1 ]] && echo documents)) op=$(curl -s --data-binary "@-" -H "Content-Type:text/plain" "$URL")
fi fi
if [[ $HASTEBIN == 1 ]]; then if [[ $HASTEBIN == 1 ]]; then
link=$(echo $op | jq -r .key) link=$(echo "$op" | jq -r .key)
else else
link=$(echo $op | cut -f2 -d'/') link=$(echo "$op" | cut -f2 -d'/')
fi fi
if [[ -v DISPLAY ]]; then if [[ -v DISPLAY ]]; then
echo ${MKR_BIN}${link} | tee /dev/tty | xclip -selection clipboard echo "${MKR_BIN}${link}" | tee /dev/tty | xclip -selection clipboard
xdg-open ${MKR_BIN}${link} xdg-open "${MKR_BIN}${link}"
else else
echo ${MKR_BIN}${link} echo "${MKR_BIN}${link}"
fi fi