aboutsummaryrefslogtreecommitdiffstats
path: root/hypo
blob: 77c6d175c098f504accc9962c55e49ea57fc9ddb (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/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