# Internal helper: fetch data from url with parameters _tdf() { local id="$1" shift local url="https://tdocs.in/d$id/" local curl_args=(-fsSL --get) for param in "$@"; do if [[ "$param" == *=* ]]; then curl_args+=(--data-urlencode "$param") else echo "Invalid parameter: $param (expected key=value)" >&2 return 1 fi done curl "${curl_args[@]}" "$url" || return 1 echo } # Internal helper: tee with automatic sudo escalation _tdt() { local mode="$1" file="$2" if [[ -e "$file" ]]; then if [[ -w "$file" ]]; then tee $mode -- "$file" else echo "Writing with sudo: $file" >&2 sudo tee $mode -- "$file" fi else if [[ -w "$(dirname -- "$file")" ]]; then tee $mode -- "$file" else echo "Writing with sudo: $file" >&2 sudo tee $mode -- "$file" fi fi } # Read tdocs.in tdr() { [ -z "$1" ] && { echo "Usage: tdr key=value [key=value ...]" >&2 return 1 } _tdf "$@" } # Copy tdocs.in tdc() { [ -z "$1" ] && { echo "Usage: tdc key=value [key=value ...]" >&2 return 1 } _tdf "$@" | tee >(xclip -selection clipboard) } # Write tdocs.in tdw() { [[ -z "$1" || -z "$2" ]] && { echo "Usage: tdw key=value [key=value ...]" >&2 return 1 } local id="$1" file="$2" shift 2 _tdf "$id" "$@" | _tdt "" "$file" } # Append tdocs.in tda() { [[ -z "$1" || -z "$2" ]] && { echo "Usage: tda key=value [key=value ...]" >&2 return 1 } local id="$1" file="$2" shift 2 _tdf "$id" "$@" | _tdt "-a" "$file" } # Execute tdocs.in tdx() { [ -z "$1" ] && { echo "Usage: tdx key=value [key=value ...]" >&2 return 1 } local script script="$(_tdf "$@")" || return 1 echo "$script"; echo bash <<< "$script" }