Skip to content

Commit

Permalink
Merge pull request #10 from nikolay/dev
Browse files Browse the repository at this point in the history
v0.2.1
  • Loading branch information
nikolay committed Sep 14, 2014
2 parents 2b74872 + cf88eae commit bb39562
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions wshare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ readonly ERR_UNSUPPORTED_OS=1
readonly ERR_INVALID_USAGE=2
readonly ERR_FILE_NOT_FOUND=3

die () {
die() {
local message="$1"
local -i exit_code="${2:-1}"

Expand All @@ -29,11 +29,23 @@ die () {

alias call="eval"

result () {
clipboard_copy() {
if [[ -n "$(type -t pbcopy)" ]]; then
pbcopy
elif [[ -n "$(type -t xsel)" ]]; then
xsel --clipboard --input
elif [[ -n "$(type -t xclip)" ]]; then
xclip -selection clipboard
else
:
fi
}

result() {
declare -p $* | sed "s/^declare /local /"
}

upload () {
upload() {
local file="$1"

[[ -r "$file" ]] || die "File $file does not exist or is not accessible" $ERR_FILE_NOT_FOUND
Expand All @@ -56,7 +68,7 @@ upload () {
result upload_id upload_url upload_key
}

shorten () {
shorten() {
local url="$1"
local -i ttl="${2:-5}"

Expand All @@ -71,7 +83,7 @@ shorten () {
result shorturl_url shorturl_key shorturl_ttl
}

delete () {
delete() {
local file
local upload_id
local upload_key
Expand All @@ -98,7 +110,7 @@ delete () {
rmdir "$WSHARE_HOME" 2>/dev/null || true
}

cleanup () {
cleanup() {
[[ -d "$WSHARE_HOME" ]] || return

local ttl_dir
Expand All @@ -122,7 +134,7 @@ cleanup () {
fi
}

share () {
share() {
local uri="$1"
local ttl="${2:-5}"

Expand All @@ -141,7 +153,7 @@ share () {
mkdir -p "$db_dir"
local db_file="$db_dir/$upload_id"
if [[ -n "$upload_key" ]]; then
cat <<-EOF > "$db_file"
cat > "$db_file" <<-EOF
$upload_key
$shorturl_url
EOF
Expand All @@ -150,16 +162,16 @@ share () {
fi
fi

echo -n "$shorturl_url" | pbcopy
echo -n "$shorturl_url" | clipboard_copy

echo "$shorturl_url"
}

get_latest_version () {
get_latest_version() {
basename "$(curl -s -o /dev/null -I -w "%{redirect_url}" https://github.com/nikolay/wshare/releases/latest)"
}

show_usage () {
show_usage() {
cat <<-EOF
Usage: $(basename $0) COMMAND
Expand All @@ -172,12 +184,12 @@ show_usage () {
EOF
}

usage () {
usage() {
show_usage
exit $ERR_INVALID_USAGE
}

main () {
main() {
[[ $# -gt 0 ]] || usage

local command="${1:-}"
Expand All @@ -202,32 +214,32 @@ main () {
shift
share "$@"
;;
*)
(*)
main --share "$@"
;;
esac
}

assignment () {
assignment() {
local variable="${1:-}"
local value="${2:-}"

echo "$variable=\"$value\""
}

do_install () {
do_install() {
mkdir -p "$(dirname "$WSHARE_BIN")"
rm -f "$WSHARE_BIN"
local self="${BASH_EXECUTION_STRING:-$(curl -sL git.io/wshare)}"
echo "${self/$(assignment WSHARE_VERSION)/$(assignment WSHARE_VERSION "$(get_latest_version)")}" > "$WSHARE_BIN"
chmod +x "$WSHARE_BIN"
}

check_install () {
check_install() {
[[ "$(type -t wshare)" == "file" ]]
}

install () {
install() {
local os="$(uname)"
case "$os" in
(Linux | Darwin)
Expand Down Expand Up @@ -256,7 +268,7 @@ install () {
fi

;;
*)
(*)
die "Unsupported OS: $os"
;;
esac
Expand Down

0 comments on commit bb39562

Please sign in to comment.