mirror of
https://github.com/nicholaswilde/recipes.git
synced 2026-02-04 14:35:37 +00:00
feat: move common functions to libbash
Signed-off-by: nιcнolaѕ wιlde <ncwilde43@gmail.com>
This commit is contained in:
77
scripts/lib/libbash
Normal file
77
scripts/lib/libbash
Normal file
@ -0,0 +1,77 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Check is variable is null
|
||||
function is_null {
|
||||
[ -z "${1}" ]
|
||||
}
|
||||
|
||||
# Check if file exists
|
||||
function file_exists(){
|
||||
[ -f "{1}" ]
|
||||
}
|
||||
|
||||
# printf usage_error if something isn't right.
|
||||
function usage_error() {
|
||||
show_usage "${1}"
|
||||
printf "\nTry %s -h for more options.\n" "${1}" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
function show_usage(){
|
||||
printf "Usage: %s [OPTIONS] FILENAME\n" "${1}"
|
||||
}
|
||||
|
||||
function show_version(){
|
||||
printf "%s version %s\n" "${1}" "${2}"; exit 0
|
||||
}
|
||||
|
||||
function script_desc(){
|
||||
printf "%s\n\n" "${1}"
|
||||
}
|
||||
|
||||
# Show the help
|
||||
function show_help(){
|
||||
show_usage "${1}"
|
||||
script_desc "${2}"
|
||||
printf "Mandatory arguments:\n"
|
||||
printf " FILENAME The cook filename to move\n\n"
|
||||
printf "Options:\n"
|
||||
printf " -h, --help Print this Help.\n"
|
||||
printf " -v, --version Print script version and exit.\n"
|
||||
exit 0
|
||||
}
|
||||
|
||||
function get_category(){
|
||||
basename "$(dirname "${1}")"
|
||||
}
|
||||
|
||||
function show_error(){
|
||||
printf "Could not get \`%s\`\n" "${1}"
|
||||
exit 1
|
||||
}
|
||||
|
||||
function get_extension() {
|
||||
printf '%s\n' "${1#*.}"
|
||||
}
|
||||
|
||||
function to_lower() {
|
||||
s="${1// /-}"
|
||||
printf '%s\n' "${s,,}"
|
||||
}
|
||||
|
||||
function remove_extension() {
|
||||
printf '%s\n' "${1%%.*}"
|
||||
}
|
||||
|
||||
function get_image() {
|
||||
s="${1}"
|
||||
if [ -f "${s}.jpg" ]; then
|
||||
readlink -f "${s}.jpg"
|
||||
elif [ -f "${s}.png" ]; then
|
||||
readlink -f "${s}.png"
|
||||
fi
|
||||
}
|
||||
|
||||
function get_filename() {
|
||||
basename "${1}"
|
||||
}
|
||||
@ -17,99 +17,38 @@ set -o pipefail
|
||||
|
||||
# https://stackoverflow.com/a/246128/1061279
|
||||
|
||||
# DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
|
||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
|
||||
ROOT_DIR="$(git rev-parse --show-toplevel)"
|
||||
DOCS_PATH="${ROOT_DIR}/docs"
|
||||
COOK_PATH="${ROOT_DIR}/cook"
|
||||
IMAGES_PATH="${DOCS_PATH}/assets/images"
|
||||
SCRIPT_NAME=$(basename "${0}")
|
||||
SCRIPT_VERSION="0.1.0"
|
||||
SCRIPT_DESC="Move recipes to their intended locations"
|
||||
DEBUG=false
|
||||
DEBUG=true
|
||||
|
||||
# readonly DIR
|
||||
readonly DIR
|
||||
readonly SCRIPT_NAME
|
||||
readonly SCRIPT_VERSION
|
||||
readonly ROOT_DIR
|
||||
readonly DOCS_PATH
|
||||
readonly DEBUG
|
||||
readonly IMAGES_PATH
|
||||
readonly COOK_PATH
|
||||
|
||||
# Check is variable is null
|
||||
function is_null {
|
||||
[ -z "${1}" ]
|
||||
}
|
||||
|
||||
# printf usage_error if something isn't right.
|
||||
function usage_error() {
|
||||
show_usage "${1}"
|
||||
printf "\nTry %s -h for more options.\n" "${1}" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
function show_usage(){
|
||||
printf "Usage: %s [OPTIONS] FILENAME\n" "${1}"
|
||||
}
|
||||
|
||||
function show_version(){
|
||||
printf "%s version %s\n" "${1}" "${2}"; exit 0
|
||||
}
|
||||
|
||||
function script_desc(){
|
||||
printf "%s\n\n" "${1}"
|
||||
}
|
||||
|
||||
# Show the help
|
||||
function show_help(){
|
||||
show_usage "${1}"
|
||||
script_desc "${2}"
|
||||
printf "Mandatory arguments:\n"
|
||||
printf " FILENAME The cook filename to move\n\n"
|
||||
printf "Options:\n"
|
||||
printf " -h, --help Print this Help.\n"
|
||||
printf " -v, --version Print script version and exit.\n"
|
||||
exit 0
|
||||
}
|
||||
|
||||
function get_category(){
|
||||
basename "$(dirname "${1}")"
|
||||
}
|
||||
|
||||
function show_error(){
|
||||
printf "Could not get \`%s\`\n" "${1}"
|
||||
exit 1
|
||||
}
|
||||
|
||||
function get_extension() {
|
||||
printf '%s\n' "${1#*.}"
|
||||
}
|
||||
|
||||
function to_lower() {
|
||||
s="${1// /-}"
|
||||
printf '%s\n' "${s,,}"
|
||||
}
|
||||
|
||||
function remove_extension() {
|
||||
printf '%s\n' "${1%%.*}"
|
||||
}
|
||||
|
||||
function get_filename() {
|
||||
basename "${1}"
|
||||
}
|
||||
|
||||
function get_image() {
|
||||
s="${1}"
|
||||
if [ -f "${s}.jpg" ]; then
|
||||
readlink -f "${s}.jpg"
|
||||
elif [ -f "${s}.png" ]; then
|
||||
readlink -f "${s}.png"
|
||||
fi
|
||||
}
|
||||
# shellcheck source=/dev/null
|
||||
source "${DIR}/lib/libbash"
|
||||
|
||||
function spell_check() {
|
||||
npx spellchecker -d "${ROOT_DIR}"/dictionary.txt -f "${@}"
|
||||
for path in "${@}"; do
|
||||
s=$(realpath --relative-to="${ROOT_DIR}" "${path}")
|
||||
arr+=("${s}")
|
||||
done
|
||||
cd "${ROOT_DIR}"
|
||||
npx spellchecker -d "${ROOT_DIR}/dictionary.txt" -f "${arr[@]}"
|
||||
}
|
||||
|
||||
function check_links() {
|
||||
function links_check() {
|
||||
docker run --rm -v /:/tmp:ro -i -w /tmp ghcr.io/tcort/markdown-link-check:stable "/tmp${1}" -c "/tmp${ROOT_DIR}/mlc_config.json"
|
||||
}
|
||||
|
||||
@ -132,13 +71,13 @@ function move_files(){
|
||||
[ "${DEBUG}" = true ] && printf "recipe_extension: %s\n" "${recipe_extension}"
|
||||
lower=$(to_lower "${recipe_name}")
|
||||
[ "${DEBUG}" = true ] && printf "lower: %s\n" "${lower}"
|
||||
markdown_path=$(readlink -f "cook/${category}/${lower}.md")
|
||||
markdown_path=$(readlink -f "${COOK_PATH}/${category}/${lower}.md")
|
||||
[ "${DEBUG}" = true ] && printf "markdown_path: %s\n" "${markdown_path}"
|
||||
if [ ! -f "${markdown_path}" ]; then
|
||||
cook-docs -c "${ROOT_DIR}/cook/${category}"
|
||||
cook-docs -c "${COOK_PATH}/${category}"
|
||||
fi
|
||||
test -f "${markdown_path}" || (printf "File does not exist, %s\n" "${markdown_path}" && exit 1)
|
||||
image_path=$(get_image "cook/${category}/${recipe_name}")
|
||||
image_path=$(get_image "${COOK_PATH}/${category}/${recipe_name}")
|
||||
[ "${DEBUG}" = true ] && printf "image_path: %s\n" "${image_path}"
|
||||
image_extension=$(get_extension "${image_path}")
|
||||
[ "${DEBUG}" = true ] && printf "image_extension: %s\n" "${image_extension}"
|
||||
@ -152,7 +91,7 @@ function move_files(){
|
||||
fi
|
||||
mv "${markdown_path}" "${new_markdown_path}"
|
||||
spell_check "${new_markdown_path}" "${recipe_path}"
|
||||
check_links "${new_markdown_path}"
|
||||
links_check "${new_markdown_path}"
|
||||
}
|
||||
|
||||
function main(){
|
||||
|
||||
Reference in New Issue
Block a user