hypo-cli/hypo
Tom Willemse 07d5ddf22c Remove _temporary_ files
Instead of removing a file when it _isn't_ temporary, remove it when it
_is_. Leave existing files alone.
2014-08-16 16:01:27 +02:00

132 lines
3.3 KiB
Bash
Executable file

#!/bin/bash
# hypo-cli -- Easily communicate with hypo
# Copyright (C) 2013 Tom Willemse
#
# This file is part of hypo-cli
#
# hypo-cli is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# hypo-cli is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with hypo-cli. If not, see <http://www.gnu.org/licenses/>.
URL="https://ryuslash.org/hypo"
function fold () { /usr/bin/fold -s -w $(tput cols); }
function help_send
{
echo "Usage: $xname send FILE"
echo " $xname send [SUFFIX]"
echo
echo "Using the first form FILE will be uploaded to a hypo instance \
and the URL at which it can be visited will be printed." | fold
echo
echo "Using the second form requires that input come from a pipe. \
Such as:" | fold
echo
echo " cat somefile.txt | hypo send"
echo
echo "In this case the argument to send is optional, if it is \
specified it should include a \`.' to help pygments decide on the \
syntax highlighting to use. If the argument is left unspecified it \
defaults to \`.txt'." | fold
}
function cmd_send
{
local sendfile="$1"
if [[ ! -t 0 ]]; then
sendfile="$(mktemp --suffix ${1-.txt})"
cat - > "$sendfile"
elif [[ -z "$1" ]]; then
cmd_help "send"
exit 1
fi
curl --upload-file "$sendfile" "$URL/"
if [[ ! -t 0 ]]; then
rm "$sendfile"
fi
}
function help_scrot
{
echo "Usage: $xname scrot ARG"
echo
echo "The ARG will be passed directly to scrot. The resulting \
screenshot will be sent directly to hypo and removed locally."
}
function cmd_scrot
{
scrot $1 -e "curl --upload-file \$f $URL/ && rm \$f"
}
function help_rm
{
echo "Usage: $xname rm HASH"
echo
echo "Remove a file from hypo. The HASH under which it is stored \
remotely (the last part of the URL) should be specified." | fold
}
function cmd_rm
{
if [[ -z "$1" ]]; then
cmd_help "rm"
exit 1
fi
curl -XDELETE "$URL/$1"
}
function help_help { cmd_help "$@"; }
function cmd_help
{
local xname="$(basename $0)"
if [[ -n "$1" ]]; then
local prefix="help"
dispatch "$@"
else
echo "Usage: $xname <command> [OPT ...] [ARG ...]"
echo
echo "Commands include: "
echo " help Show this help message"
echo " rm Remove a file from a hypo instance"
echo " scrot Take a screenshot and send it to a hypo instance"
echo " send Send a file to a hypo instance"
echo
echo "You can use \`$xname help <command>' to get more \
information about a command." | fold
fi
}
function dispatch
{
local name="${prefix-cmd}_$1"
if [[ $(type -t "$name") == "function" ]]; then
shift
"$name" "$@"
else
echo "Unknown command: $1"
exit 1
fi
}
if [[ ${#@} -lt 1 ]]; then
cmd_help "$@"
exit 1
else
dispatch "$@"
fi