<fix>: if exit with error, catch the signal, clean up the tmp dir.

This commit is contained in:
YiXuan Ding 2024-10-25 15:34:00 +08:00
parent 73b23fc18e
commit 9f3046b423

View File

@ -17,6 +17,7 @@ version="pre"
path_to_executable="" path_to_executable=""
default_install_path="/usr/local/bin" default_install_path="/usr/local/bin"
binary_name="chsrc" binary_name="chsrc"
temp_install_dir="" # 用于存储临时安装目录
info() { info() {
echo "[INFO] $*" echo "[INFO] $*"
@ -37,7 +38,6 @@ help() {
echo echo
} }
set_install_path() { set_install_path() {
if [ -n "$install_dir" ]; then if [ -n "$install_dir" ]; then
# 扩展 ~ 符号 # 扩展 ~ 符号
@ -47,6 +47,7 @@ set_install_path() {
if [ ! -d "$install_dir" ]; then if [ ! -d "$install_dir" ]; then
echo "目录 $install_dir 不存在,正在创建..." echo "目录 $install_dir 不存在,正在创建..."
mkdir -p "$install_dir" || { echo "创建目录失败,请重试"; exit 1; } mkdir -p "$install_dir" || { echo "创建目录失败,请重试"; exit 1; }
temp_install_dir="$install_dir" # 记录临时安装目录
fi fi
elif existing_path=$(command -v "$binary_name" 2>/dev/null); then elif existing_path=$(command -v "$binary_name" 2>/dev/null); then
info "$binary_name 已安装,更新路径: ${existing_path}" info "$binary_name 已安装,更新路径: ${existing_path}"
@ -61,7 +62,6 @@ set_install_path() {
fi fi
} }
install() { install() {
arch="$(uname -m | tr '[:upper:]' '[:lower:]')" arch="$(uname -m | tr '[:upper:]' '[:lower:]')"
@ -92,7 +92,6 @@ install() {
info "下载 ${binary_name} (${arch} 架构, ${platform} 平台, ${version}版本) 到 ${path_to_executable}" info "下载 ${binary_name} (${arch} 架构, ${platform} 平台, ${version}版本) 到 ${path_to_executable}"
# 下载文件并设置权限
if curl -sL "$url" -o "$path_to_executable"; then if curl -sL "$url" -o "$path_to_executable"; then
chmod +x "$path_to_executable" chmod +x "$path_to_executable"
info "🎉 安装完成,路径: $path_to_executable" info "🎉 安装完成,路径: $path_to_executable"
@ -101,6 +100,16 @@ install() {
fi fi
} }
# 清理函数
cleanup() {
if [ -n "$temp_install_dir" ] && [ -d "$temp_install_dir" ]; then
echo "清理创建的目录: $temp_install_dir"
rm -rf "$temp_install_dir"
fi
}
# 设置 trap 以捕获退出信号
trap cleanup EXIT
# main # main
while getopts ":hd:v:" option; do while getopts ":hd:v:" option; do
@ -123,4 +132,4 @@ while getopts ":hd:v:" option; do
done done
set_install_path set_install_path
install install