commit 1e17c19038dc6de5df30221898abf71904578f06 Author: fanyx Date: Fri Jul 23 14:08:34 2021 +0200 Initial commit and default settings diff --git a/mkshare b/mkshare new file mode 100755 index 0000000..dc1d807 --- /dev/null +++ b/mkshare @@ -0,0 +1,76 @@ +#!/bin/bash + +set -e + +BASE="$0" +SHARE_DIR="" +SOURCE_DIR="" + +compress="false" +expire="false" +expire_time="$(date -Iseconds -d +7days)" +shorten="false" + +print_usage() { + echo 'usage: mkshare [-c] [-e [ISO-8601]] [-h] [-s] + +mkshare - create symlink, output sharable link and optionally set expire timer + +common options: + -h show this help message and exit + -c compress target using zip and link to zip instead + -e set expire time using ISO-8601 string or default to 7 days from creation + -s shorten link to a random string in root share folder + e.g. https://fanyx.xyz/s/DkVbDMbN4ZpSYx08 +' +} + +# if no arguments given, print usage and exit +[ $# -eq 0 ] && print_usage && exit 1 + +# parse flags +# c: compress +# use zip to compress files or directories and put link to created zip file +# e: expire +# use ISO-8061 datetime string to set expiry date +# default expiry date is 7 days from creation +# h: help +# print usage +# s: shorten +# generate random string of characters using `head /dev/urandom | tr -dc A-Za-z0-9 | head -c16` + +while getopts ':cehs' flag; do + case "$flag" in + h) print_usage ;; + c) + compress="true" + ;; + e) + if ! [ -z "${OPTARG}" ]; then + # set expire time to given date + # fail if not iso-8601 + if ! date -d "${OPTARG}"; then + exit 1 + fi + expire_time="$(date -d "${OPTARG}" -Iseconds)" + else + # no expire time set + # default to 7 days + expire="true" + fi + ;; + s) + shorten="true" + ;; + ?) + echo "Invalid option: -${OPTARG}" + echo + print_usage + ;; + esac +done + +shift $((OPTIND-1)) + +[ "${1:-}" = "--" ] && shift +