diff --git a/templates/pastey.sh b/templates/pastey.sh index c509998..d989825 100755 --- a/templates/pastey.sh +++ b/templates/pastey.sh @@ -1,19 +1,228 @@ -#!/bin/bash +#!/usr/bin/env bash -# Install this script into your PATH and chmod +x -# Usage example: -# $ cat /var/log/nginx.log | pastey +# c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t +# vi: set shiftwidth=4 tabstop=4 noexpandtab: +# :indentSize=4:tabSize=4:noTabs=false: -if ! command -v curl &> /dev/null ; then - echo "Please install curl to use this script" - exit 1 -fi +# script framework based on https://betterdev.blog/minimal-safe-bash-script-template/ +# initially adapted and written by Serge van Ginderachter +set -Eeuo pipefail +#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 }}" -PASTEY_CONTENT=$(&2 -e "${1-}" +} + +die() { + local msg=$1 + local code=${2-1} # default exit status 1 + msg "$msg\n" + exit "$code" +} + +parse_params() { + # check required params and arguments + + + expiration= + content= + title= + file= + single= + encrypt= + + 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="-F single=" + ;; + + -e | --encrypt) + shift || : + encrypt="-F encrypt=" + ;; + + --) + shift || : + execute=($*) + shift $# + ;; + + -?*) + die "Unknown option: $1" + shift || : + ;; + + *) + if [[ -n "${1:-}" ]] + then + die "Unknown parameter: $1" + fi + ;; + + esac + done + +} + +parse_options(){ + + # warn if both single and expiration are set + if [[ -n "${expiration}" ]] && [[ -n "${single}" ]] + then + die "option -x|--expiration and -s|--single are mutually exclusive" + fi + + # warn if more than 1 source + if [[ -n "${content}" && -n "${file}" ]] || + [[ -n "${content}" && -n "${execute[*]}" ]] || + [[ -n "${execute[*]}" && -n "${file}" ]] + then + die "option -c|--content, -f|--file and -- are mutually exclusive" + fi + + if [[ -z "${content}" ]] + then + if [[ -n "${file}" ]] + then + if [[ ${file} = "-" ]] + then + content="$(&1 ||: )" + else + content="$(