git-vendor-name: bash3boilerplate
git-vendor-dir: vendor/git.knownelement.com/ExternalVendorCode/bash3boilerplate
git-vendor-repository: https://git.knownelement.com/ExternalVendorCode/bash3boilerplate.git
git-vendor-ref: main
This commit is contained in:
2024-12-09 12:44:28 -06:00
64 changed files with 3997 additions and 0 deletions

View File

@@ -0,0 +1,113 @@
#!/usr/bin/env bash
# BASH3 Boilerplate: ini_val
#
# This file:
#
# - Can read and write .ini files using pure bash
#
# Limitations:
#
# - All keys inside a section of the .ini file must be unique
# - Optional comment parameter for the creation of new entries
#
# Usage as a function:
#
# source ini_val.sh
# ini_val data.ini connection.host 127.0.0.1 "Host name or IP address"
#
# Usage as a command:
#
# ini_val.sh data.ini connection.host 127.0.0.1 "Host name or IP address"
#
# Based on a template by BASH3 Boilerplate vv2.7.2
# http://bash3boilerplate.sh/#authors
#
# The MIT License (MIT)
# Copyright (c) 2013 Kevin van Zonneveld and contributors
# You are not obligated to bundle the LICENSE file with your b3bp projects as long
# as you leave these references intact in the header comments of your source files.
function ini_val() {
local file="${1:-}"
local sectionkey="${2:-}"
local val="${3:-}"
local comment="${4:-}"
local delim="="
local comment_delim=";"
local section=""
local key=""
local current=""
# add default section
local section_default="default"
if [[ ! -f "${file}" ]]; then
# touch file if not exists
touch "${file}"
fi
# Split on . for section. However, section is optional
IFS='.' read -r section key <<< "${sectionkey}"
if [[ ! "${key}" ]]; then
key="${section}"
# default section if not given
section="${section_default}"
fi
# get current value (if exists)
current=$(sed -En "/^\[/{h;d;};G;s/^${key}([[:blank:]]*)${delim}(.*)\n\[${section}\]$/\2/p" "${file}"|awk '{$1=$1};1')
# get current comment (if exists)
current_comment=$(sed -En "/^\[${section}\]/,/^\[.*\]/ s|^(${comment_delim}\[${key}\])(.*)|\2|p" "${file}"|awk '{$1=$1};1')
if ! grep -q "\[${section}\]" "${file}"; then
# create section if not exists (empty line to seperate new section for better readability)
echo >> "${file}"
echo "[${section}]" >> "${file}"
fi
if [[ ! "${val}" ]]; then
# get a value
echo "${current}"
else
# set a value
if [[ ! "${section}" ]]; then
# if no section is given, propagate the default section
section=${section_default}
fi
if [[ ! "${comment}" ]]; then
# if no comment given, keep old comment
comment="${current_comment}"
fi
# maintenance area
# a) remove comment if new given / respect section
sed -i.bak "/^\[${section}\]/,/^\[.*\]/ s|^\(${comment_delim}\[${key}\] \).*$||" "${file}"
# b) remove old key / respect section
sed -i.bak "/^\[${section}\]/,/^\[.*\]/ s|^\(${key}=\).*$||" "${file}"
# c) remove all empty lines in ini file
sed -i.bak '/^[[:space:]]*$/d' "${file}"
# d) insert line break before every section for better readability
sed -i.bak $'s/^\\[/\\\n\\[/g' "${file}"
# add to section
if [[ ! "${comment}" ]]; then
# add new key/value _without_ comment
RET="/\\[${section}\\]/a\\
${key}${delim}${val}"
else
# add new key/value _with_ preceeding comment
RET="/\\[${section}\\]/a\\
${comment_delim}[${key}] ${comment}\\
${key}${delim}${val}"
fi
sed -i.bak -e "${RET}" "${file}"
# this .bak dance is done for BSD/GNU portability: http://stackoverflow.com/a/22084103/151666
rm -f "${file}.bak"
fi
}
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
export -f ini_val
else
ini_val "${@}"
exit ${?}
fi

View File

@@ -0,0 +1,78 @@
#!/usr/bin/env bash
# BASH3 Boilerplate: megamount
#
# This file:
#
# - Takes a URL (smb, nfs, afs) and tries to mount it at a given target directory
# - Forcefully unmounts any active mount at the target directory first
# - Displays the mount's contents for verification
#
# Depends on:
#
# - ./parse_url.sh
#
# Usage as a function:
#
# source megamount.sh
# megamount smb://janedoe:abc123@192.168.0.1/documents /mnt/documents
#
# Usage as a command:
#
# megamount.sh smb://janedoe:abc123@192.168.0.1/documents /mnt/documents
#
# Based on a template by BASH3 Boilerplate vv2.7.2
# http://bash3boilerplate.sh/#authors
#
# The MIT License (MIT)
# Copyright (c) 2013 Kevin van Zonneveld and contributors
# You are not obligated to bundle the LICENSE file with your b3bp projects as long
# as you leave these references intact in the header comments of your source files.
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=src/parse_url.sh
source "${__dir}/parse_url.sh"
function megamount () {
local url="${1}"
local target="${2}"
local proto
local user
local pass
local host
local port
local path
proto=$(parse_url "${url}" "proto")
user=$(parse_url "${url}" "user")
pass=$(parse_url "${url}" "pass")
host=$(parse_url "${url}" "host")
port=$(parse_url "${url}" "port")
path=$(parse_url "${url}" "path")
(umount -lf "${target}" || umount -f "${target}") > /dev/null 2>&1 || true
mkdir -p "${target}"
if [[ "${proto}" = "smb://" ]]; then
mount -t cifs --verbose -o "username=${user},password=${pass},hard" "//${host}/${path}" "${target}"
elif [[ "${proto}" = "afp://" ]]; then
# start syslog-ng
# afpfsd || echo "Unable to run afpfsd. Does /dev/log exist?" && exit 1
mount_afp "${url}" "${target}"
elif [[ "${proto}" = "nfs://" ]]; then
mount -t nfs --verbose -o "vers=3,nolock,soft,intr,rsize=32768,wsize=32768" "${host}:/${path}" "${target}"
else
echo "ERR: Unknown protocol: '${proto}'"
exit 1
fi
# chmod 777 "${target}"
ls -al "${target}/"
}
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
export -f megamount
else
megamount "${@}"
exit ${?}
fi

View File

@@ -0,0 +1,84 @@
#!/usr/bin/env bash
# BASH3 Boilerplate: parse_url
#
# This file:
#
# - Takes a URL and parses protocol, user, pass, host, port, path.
#
# Based on:
#
# - http://stackoverflow.com/a/6174447/151666
#
# Usage as a function:
#
# source parse_url.sh
# parse_url 'http://johndoe:abc123@example.com:8080/index.html' pass
#
# Usage as a command:
#
# parse_url.sh 'http://johndoe:abc123@example.com:8080/index.html'
#
# Based on a template by BASH3 Boilerplate vv2.7.2
# http://bash3boilerplate.sh/#authors
#
# The MIT License (MIT)
# Copyright (c) 2013 Kevin van Zonneveld and contributors
# You are not obligated to bundle the LICENSE file with your b3bp projects as long
# as you leave these references intact in the header comments of your source files.
function parse_url() {
local parse="${1}"
local need="${2:-}"
local proto
local url
local userpass
local user
local pass
local hostport
local host
local port
local path
proto="$(echo "${parse}" | grep :// | sed -e's,^\(.*://\).*,\1,g')"
url="${parse/${proto}/}"
userpass="$(echo "${url}" | grep @ | cut -d@ -f1)"
user="$(echo "${userpass}" | grep : | cut -d: -f1)"
pass="$(echo "${userpass}" | grep : | cut -d: -f2)"
hostport="$(echo "${url/${userpass}@/}" | cut -d/ -f1)"
host="$(echo "${hostport}" | grep : | cut -d: -f1)"
port="$(echo "${hostport}" | grep : | cut -d: -f2)"
path="$(echo "${url}" | grep / | cut -d/ -f2-)"
[[ ! "${user}" ]] && user="${userpass}"
[[ ! "${host}" ]] && host="${hostport}"
if [[ ! "${port}" ]]; then
[[ "${proto}" = "http://" ]] && port="80"
[[ "${proto}" = "https://" ]] && port="443"
[[ "${proto}" = "mysql://" ]] && port="3306"
[[ "${proto}" = "redis://" ]] && port="6379"
fi
if [[ "${need}" ]]; then
echo "${!need}"
else
echo ""
echo " Use second argument to return just 1 variable."
echo " parse_url() demo: "
echo ""
echo " proto: ${proto}"
echo " user: ${user}"
echo " pass: ${pass}"
echo " host: ${host}"
echo " port: ${port}"
echo " path: ${path}"
echo ""
fi
}
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
export -f parse_url
else
parse_url "${@}"
exit ${?}
fi

View File

@@ -0,0 +1,68 @@
#!/usr/bin/env bash
# BASH3 Boilerplate: templater
#
# This file:
#
# - takes a source (template) & destination (config) filepath argument
# - then replaces placeholders with variables found in the environment
#
# Usage as a function:
#
# source templater.sh
# export NAME=kevin
# templater input.cfg output.cfg
#
# Usage as a command:
#
# ALLOW_REMAINDERS=1 templater.sh input.cfg output.cfg
#
# Based on a template by BASH3 Boilerplate vv2.7.2
# http://bash3boilerplate.sh/#authors
#
# The MIT License (MIT)
# Copyright (c) 2013 Kevin van Zonneveld and contributors
# You are not obligated to bundle the LICENSE file with your b3bp projects as long
# as you leave these references intact in the header comments of your source files.
function templater() {
ALLOW_REMAINDERS="${ALLOW_REMAINDERS:-0}"
templateSrc="${1:-}"
templateDst="${2:-}"
if [[ ! -f "${templateSrc}" ]]; then
echo "ERROR: Template source '${templateSrc}' needs to exist"
exit 1
fi
if [[ ! "${templateDst}" ]]; then
echo "ERROR: Template destination '${templateDst}' needs to be specified"
exit 1
fi
if [[ "$(command -v perl)" ]]; then
perl -p -e 's/\$\{(\w+)\}/(exists $ENV{$1} ? $ENV{$1} : "\${$1}")/eg' < "${templateSrc}" > "${templateDst}"
else
cp -f "${templateSrc}" "${templateDst}"
for var in $(env |awk -F= '{print $1}' |grep -E '^(_[A-Z0-9_]+|[A-Z0-9][A-Z0-9_]*)$'); do
sed -i.bak -e "s#\${${var}}#${!var//#/\\#/}#g" "${templateDst}"
# this .bak dance is done for BSD/GNU portability: http://stackoverflow.com/a/22084103/151666
rm -f "${templateDst}.bak"
done
fi
# cat "${templateDst}"
# shellcheck disable=SC2016
if grep '${' "${templateDst}" && [[ "${ALLOW_REMAINDERS}" = "0" ]]; then
echo "ERROR: Unable to replace the above template vars"
exit 1
fi
}
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
export -f templater
else
templater "${@}"
exit ${?}
fi