fixed the pastey.sh template which had serious bugs. (#27)

pull/31/head
Steve Hay 2 years ago committed by GitHub
parent 9729ac9b5b
commit 8972cb8a0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,234 +1,178 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t PASTEY_ENDPOINT={{ endpoint }}
# vi: set shiftwidth=4 tabstop=4 noexpandtab:
# :indentSize=4:tabSize=4:noTabs=false:
# script framework based on https://betterdev.blog/minimal-safe-bash-script-template/ show_help() {
# initially adapted and written by Serge van Ginderachter <serge@vanginderachter.be> cat <<-EOF
Usage: ${0##*/} [-h] [-e] [-f INFILE] [-s] [-t title] [-x time_hrs] [-- command]
set -Eeo pipefail CLI interface to pastey.
#execute=(echo popo)trap cleanup SIGINT SIGTERM ERR EXIT
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
# your custom endpoint
PASTEY_ENDPOINT="{{ endpoint }}"
#
## functions
usage() {
cat <<-EOF
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-v] [-f] -p param_value arg1 [arg2...]
Script description here.
Available options: Available options:
-h, --help Print this help and exit -h, --help Print this help and exit
-v, --verbose Print script debug info
-c, --content Pass the content of the paste in a simple argument
-e, --encrypt Encrypt the paste content -e, --encrypt Encrypt the paste content
-f, --file Read the content from this file. If file is "-", read from stdin -f, --file Read the content from this file.
-s, --single Create a paste that expires after the first view -s, --single Create a paste that expires after the first view
-t, --title Set the title of the paste -t, --title Set the title of the paste
-x, --expiration -x, --expiration
Set the time in hours after which the paste expires Set the time in hours after which the paste expires
(Default is expiration is disabled)
-- Stop further option parsing -- Stop further option parsing
Arguments passed after the -- option are evaluated Arguments passed after the -- option are evaluated
as a command, and that command's output is pasted. as a command, and that command's output is pasted.
The full command is used a the title. The full command is used a the title.
If zero arguments are passed, If zero arguments are passed,
or none of --content, --file or -- are passed, or none of --file or -- are passed,
content is read from stdin. content is read from stdin.
EOF EOF
exit
}
cleanup() {
trap - SIGINT SIGTERM ERR EXIT
msg "Some unhandled error happened.\n"
usage
}
msg() {
echo >&2 -e "${1-}"
} }
die() { die() {
local msg=$1 printf '%s\n' "$1" >&2
local code=${2-1} # default exit status 1 exit 1
msg "$msg\n"
exit "$code"
} }
parse_params() {
# check required params and arguments
expiration=
content=
title=
file=
single="false"
encrypt="false"
while (( "$#" ))
do
case "${1-}" in
-h | --help)
usage
;;
-v | --verbose)
set -x
;;
-t | --title)
shift || :
title="${1}"
shift || :
;;
-c | --content)
shift || :
content="${1}"
shift || :
;;
-f | --file)
shift || :
file="${1}"
shift || :
;;
-x | --expiration)
shift || :
expiration="${1}"
shift || :
;;
-s | --single)
shift || :
single="true"
;;
-e | --encrypt)
shift || :
encrypt="true"
;;
--)
shift || :
execute=($*)
shift $#
;;
-?*)
die "Unknown option: $1"
shift || :
;;
*)
if [[ -n "${1:-}" ]]
then
die "Unknown parameter: $1"
fi
;;
esac
done
}
parse_options(){ # check required params and arguments
# warn if both single and expiration are set file=
if [[ -n "${expiration}" ]] && [[ -n "${single}" ]] expiration=
then title=
die "option -x|--expiration and -s|--single are mutually exclusive" single=
fi encrypt=
execute=
# warn if more than 1 source
if [[ -n "${content}" && -n "${file}" ]] || while :; do
[[ -n "${content}" && -n "${execute[*]}" ]] || case $1 in
[[ -n "${execute[*]}" && -n "${file}" ]] -h|--help)
then show_help
die "option -c|--content, -f|--file and -- <command> are mutually exclusive" exit
fi ;;
-e|--encrypt)
if [[ -z "${content}" ]] encrypt='-F encrypt='
then ;;
if [[ -n "${file}" ]] -f|--file)
then if [ "$2" ]; then
if [[ ${file} = "-" ]] file=$2
then if [ ! "$title" ]; then
content="$(</dev/stdin)" title=$file
elif [ -r ${file} ] fi
then shift
content="$(cat ${file} | tr -d '\0')"
else else
die "Could not read from ${file}" die 'ERROR: "--file requires a non-empty option argument.'
fi fi
elif [[ -n "${execute[*]}" ]] ;;
then --file=?*)
content="$( bash -c "${execute[*]}" 2>&1 ||: )" file=${1#*=}
else title=$file
content="$(</dev/stdin)" ;;
fi --file=) # Handle the case of an empty --file=
fi die 'ERROR: "--file requires a non-empty option argument.'
;;
-s|--single)
single='-F single='
expiration=
;;
-t|--title)
if [ "$2" ]; then
title=$2
shift
else
die 'ERROR: --title requires a non-empty option argument'
fi
;;
--title=?*)
title=${1#*=}
;;
--title=) # Handle the case of an empty --title=
die 'ERROR: --title requires a non-empty option argument'
;;
-x|--expiration)
if [ "$2" ]; then
expiration=$2
shift
else
die 'ERROR: --expiration requires a non-empty option argument'
fi
;;
--expiration=?*)
expiration=${1#*=}
;;
--expiration=) # Handle the case of an empty --expiration=
die 'ERROR: --expiration requires a non-empty option argument'
;;
--) # End of all options
shift
execute=("$@")
if [ ! "$title" ]; then
title=${execute[*]}
fi
break
;;
-?*)
printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
;;
*) # Default case: No more options, so break out of the loop.
break
esac
shift
done
# expiration needs to be set to disabled
if [ -z "${expiration}" ]
then
expiration="-1"
fi
# alternative titles if possible # do some sanity checks
if [ -z "${title}" ]
then if [ "$expiration" ] && [ "$single" ]; then
if [ -n "${file}" ] die 'option -x|--expiration and -s|--single are mutually exclusive'
then fi
title="${file}"
elif [ -n "${execute[*]}" ] if [ "${execute[*]}" ] && [ "$file" ]; then
then die "-f|--file and -- <command> are mutually exclusive"
title="${execute[@]}" fi
fi
# set title to unknown if empty
if [ ! "$title" ]; then
title=untitled
fi
# read contents
if [ "$execute" ]; then # input from an executed command
exec {content_fd}< <("${execute[@]}" 2>&1)
elif [ "$file" ]; then # input from a file
if [ -r "$file" ]; then
exec {content_fd}< "$file"
else
die "Could not read from file \"$file\""
fi fi
else # read from stdin
exec {content_fd}< /dev/fd/0
fi
}
# expiration needs to be set to -1 if disabled
if [ ! "${expiration}" ]; then
expiration="-1"
fi
# just do it now
paste_it() {
content=$(echo -n "${content}" | base64)
payload="{
\"content\": \"${content}\",
\"title\": \"${title}\",
\"expiration\": \"${expiration}\",
\"encrypt\": ${encrypt},
\"single\": ${single},
\"base64\": true
}"
echo -n $payload | curl \
-X POST \
-H "Content-Type: application/json" \
--data-binary @- \
${PASTEY_ENDPOINT}; echo
}
## main execution # upload
parse_params $* curl \
parse_options -X POST \
paste_it -F "cli=" \
-F "title=$title" \
-F "content=</dev/fd/$content_fd" \
-F "expiration=$expiration" \
${encrypt} \
${single} \
"${PASTEY_ENDPOINT}"
echo
# exit cleanly exec {content_fd}<&-
trap - SIGINT SIGTERM ERR EXIT
exit 0

Loading…
Cancel
Save