Initial file structrue and rdlink with essential content

This commit is contained in:
2022-03-18 17:17:28 +01:00
parent 9ab4b12829
commit 6dc6b305b9
3 changed files with 190 additions and 0 deletions

6
.editorconfig Normal file
View File

@@ -0,0 +1,6 @@
root = true
[*.sh]
charset = utf-8
indent_style = space
indent_size = 2

112
rdlink.sh Executable file
View File

@@ -0,0 +1,112 @@
#!/usr/bin/env bash
## rdlink [-d|--debug] <PATH>
##
## License: GNU GPL V3 or later
## Author: Martin Winkler
# Exit on error. Append "|| true" if you expect an error.
set -o errexit
# Exit on error inside any functions or subshells.
set -o errtrace
# Do not allow use of undefined vars. Use ${VAR:-} to use an undefined VAR
set -o nounset
# Catch the error in case mysqldump fails (but gzip succeeds) in `mysqldump |gzip`
set -o pipefail
LOG_LEVEL="${LOG_LEVEL:-1}" # 7 = debug -> 0 = emergency
rl::rdlink() {
local normalized=
info "Processing... $@"
normalized="$(rl::normalize "${1:-}")"
rl::quicklink "${normalized}"
}
rl::canon() {
info "Canonicalize path... $@"
}
rl::quicklink() {
info "Quick rdlink... ${1:-}"
readlink "${1:-}" || printf "${1:-}\n"
}
rl::normalize() {
info "Normalizing... ${1:-}"
printf "${1:-}\n"
}
rl::main() {
local file=
local arg=
for arg in "$@"; do
case "$arg" in
--)
shift
break ;;
-d|--debug)
shift
LOG_LEVEL=7 ;;
-dd)
shift
set -o xtrace ;;
-*)
shift ;;
esac
done
info "BASH rdlink"
for file in "$@"; do
rl::rdlink "$file"
done
}
function rl::log () {
local log_level="${1:-${LOG_LEVEL}}"
shift
# all remaining arguments are to be printed
local log_line=""
while IFS=$'\n' read -r log_line; do
printf "%s [%9s] %s\n" "$(date -u +"%Y-%m-%d %H:%M:%S UTC")" "${log_level}" "${log_line}" 1>&2
done <<< "${@:-}"
}
emergency () { rl::log emergency "${@}"; exit 1; }
alarm() { [[ "${LOG_LEVEL:-0}" -ge 1 ]] && rl::log alert "${@}"; true; }
critical () { [[ "${LOG_LEVEL:-0}" -ge 2 ]] && rl::log critical "${@}"; true; }
error () { [[ "${LOG_LEVEL:-0}" -ge 3 ]] && rl::log error "${@}"; true; }
warning () { [[ "${LOG_LEVEL:-0}" -ge 4 ]] && rl::log warning "${@}"; true; }
notice () { [[ "${LOG_LEVEL:-0}" -ge 5 ]] && rl::log notice "${@}"; true; }
info () { [[ "${LOG_LEVEL:-0}" -ge 6 ]] && rl::log info "${@}"; true; }
debug () { [[ "${LOG_LEVEL:-0}" -ge 7 ]] && rl::log debug "${@}"; true; }
msg () { echo "$@"; true; }
# Provide as function to be called when sourced
rdlink() {
rl::main "$@"
}
### Check if script is _sourced
### https://stackoverflow.com/a/28776166
_sourced=0
if [ -n "${ZSH_EVAL_CONTEXT:-}" ]; then
case ${ZSH_EVAL_CONTEXT:-} in *:file) _sourced=1;; esac
elif [ -n "${KSH_VERSION:-}" ]; then
[ "$(cd $(dirname -- $0) && pwd -P)/$(basename -- $0)" != "$(cd $(dirname -- ${.sh.file}) && pwd -P)/$(basename -- ${.sh.file})" ] && _sourced=1
elif [ -n "${BASH_VERSION:-}" ]; then
(return 0 2>/dev/null) && _sourced=1
else # All other shells: examine $0 for known shell binary filenames
# Detects `sh` and `dash`; add additional shell filenames as needed.
case ${0##*/} in sh|dash) _sourced=1;; esac
fi
###
if (( ! _sourced )); then
rl::main "$@"
fi

72
test/test_rdlink.sh Executable file
View File

@@ -0,0 +1,72 @@
#!/bin/bash
tool_a="/opt/rdlink/rdlink.sh"
tool_b="$(command -v readlink) -f"
test_links=( "Valid links"
"/dev/stdin"
"$HOME/tmp/a"*
"$HOME/tmp/a 3"
"/bin/adb"
)
test_all=( "Test everything starting from /"
/**/*
)
test_args=( "Tests from command line"
"$@"
)
totest=(
test_links
)
[[ "$@" ]] && totest=(test_args)
rl::test() {
local i=0
local ra=
local rb=
local path=
local testarray=
local testend=0
for testarray in "${totest[@]}"; do
i=0
testarray="$testarray"[@]
for path in "${!testarray}"; do
if (( ! i )); then
# Print title in array element 0
printf " ### %s ###\n" "$path"
else
# Execute tests
printf -- "%-3d --- %-30s ---" $i "$path"
ra="$(${tool_a} "$path")"
rb="$(${tool_b} "$path")"
if [[ "$ra" = "$rb" ]]; then
# Test successful
printf " OK\n"
else
# Test failed
printf " FAIL\n"
printf "\n Result:\n%s\n%s\n" "$ra" "$rb"
printf "\n Subject:\n"
ls -al "$path"
# Print debug output of tool_a
printf "\n Debug:\n"
( "${tool_a}" -d "${path}" )
testend=1 && break
fi
fi
((i++))
done
(( testend )) && break
done
}
time rl::test