Files
alpine/prebuilds/usr/local/lib/libcommon.sh
T

102 lines
3.0 KiB
Bash

#!/bin/bash
# Ver: 1.6 by Endial Fang (endial@126.com)
#
# 通用函数库
# 加载依赖项
source "/usr/local/lib/liblog.sh"
# 函数列表
# 打印包含包含Logo的欢迎信息
print_welcome_info() {
[[ -n "${APP_NAME:-}" ]] && github_repo="https://gitee.com/colovu/docker-${APP_NAME}"
info " ____ _ "
info " / ___|___ | | _____ ___ _ "
info "| | / _ \| |/ _ \ \ / / | | | Application: ${APP_NAME:-undefined}"
info "| |__| (_) | | (_) \ V /| |_| | Version: ${APP_VER:-0.0.0}"
info " \____\___/|_|\___/ \_/ \__,_| PowerBy: Endial@126.com"
debug " Project Repo: ${github_repo:-}"
info ""
}
# 根据需要打印欢迎信息
print_image_welcome() {
if [[ -z "${DISABLE_WELCOME_MESSAGE:-}" ]]; then
if [[ ! "$(id -u)" = "0" ]]; then
print_welcome_info
fi
fi
}
# 检测可能导致容器执行后直接退出的命令,如"--help";如果存在,直接返回 0
# 参数:
# $1 - 待检测的参数表
print_command_help() {
local arg
for arg; do
if [[ "$arg" =~ ^(-[?HhVv]|--help|--version)$ ]]; then
exec "$@"
exit
fi
done
}
# 检测应用相应的配置文件是否存在,如果不存在,则从默认配置文件目录拷贝一份
# 默认配置文件路径:/etc/${APP_NAME}
# 目标配置文件路径:/srv/conf/${APP_NAME}
# 参数:
# $1 - 基础路径
# $* - 基础路径下的文件及目录列表,以" "分割
# 例子:
# ensure_config_file_exist /etc/${APP_NAME} conf.d server.conf
ensure_config_file_exist() {
local -r base_path="${1:?paths is missing}"
shift
local files=("$@")
debug "List to check: ${files[*]}"
for f in "${files[@]}"; do
debug "Processing: ${f}"
local dist="${base_path/$'\/etc'/$'\/srv\/conf'}/${f}"
if [[ -d "${base_path}/${f}" ]]; then
[[ ! -d "$dist" ]] && { debug "Creating directory: ${dist}"; mkdir -p "$dist"; }
[[ $(ls -A "${base_path}/${f}") ]] &&
ensure_config_file_exist "${base_path}/${f}" $(ls -A "${base_path}/${f}")
else
[[ ! -e "$dist" ]] && {
debug "Copying: ${base_path}/${f} => ${dist}"
cp "${base_path}/${f}" "$dist"
rm -f "/srv/conf/${APP_NAME}/.app_init_flag"
}
fi
done
}
# 根据脚本扩展名及权限,执行相应的初始化脚本
# 参数:
# $1 - 文件列表,支持路径通配符
# 使用:
# process_init_files [file [file [...]]]
# 例子:
# process_init_files /src/conf/${APP_NAME}/initdb.d/*
process_init_files() {
local f
while IFS= read -r -d '' f; do
case "$f" in
*.sh)
if [[ -x "$f" ]]; then
info "$0: executing $f"
"$f"
else
info "$0: sourcing $f"
source "$f"
fi
;;
*) warn "$0: ignoring $f" ;;
esac
done < <(find "$@" -type f -print0 2>/dev/null || true)
}