Better maintainability for install.sh

[GitHub #98]
This commit is contained in:
Aoran Zeng 2024-10-26 14:08:49 +08:00
parent f8fbc5c90a
commit 34cda38165
No known key found for this signature in database
GPG Key ID: 8F8BA8488E10ED98

View File

@ -1,83 +1,95 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# --------------------------------------------------------------- # ---------------------------------------------------------------
# File Name : install.sh # File Name : install.sh
# File Authors : GnixAij <gaojiaxing0220@gmail.com> # File Authors : GnixAij <gaojiaxing0220@gmail.com>
# | xuan <wick.dynex@qq.com> # | xuan <wick.dynex@qq.com>
# | ChatGPT <https://chatgpt.com> # | ChatGPT <https://chatgpt.com>
# |
# Contributors : Aoran Zeng <ccmywish@qq.com> # Contributors : Aoran Zeng <ccmywish@qq.com>
# | # |
# Created On : <2024-10-25> # Created On : <2024-10-25>
# Last Modified : <2024-10-26> # Last Modified : <2024-10-26>
# #
#
# chsrc installer for Linux & macOS # chsrc installer for Linux & macOS
#
# --------------------------------------------------------------- # ---------------------------------------------------------------
install_dir=""
version="pre"
path_to_executable=""
default_install_path="/usr/local/bin"
noroot_default_install_path="$HOME/.local/bin"
binary_name="chsrc" binary_name="chsrc"
temp_install_dir="" # 用于存储临时安装目录
helpflag=0
lang="zh"
# 输出相关信息 # 用户指定的安装目录函数set_install_dir()将填充/校验该变量
userOpt_install_dir=""
# 默认安装目录
default_install_dir_4root="/usr/local/bin"
default_install_dir_4nonroot="$HOME/.local/bin"
# 若用户指定的目录不存在则会创建该目录
tmp_created_install_dir=""
userOpt_version="pre"
userOpt_help=0
userOpt_lang="zh"
info() { info() {
echo "[INFO] $*" echo "[INFO] $*"
} }
# 输出错误到stdout和stderr # 出现错误,直接强制退出
error() { error() {
echo -e "[ERROR] $*" >&2 echo -e "[ERROR] $*" >&2
exit 1 exit 1
} }
# 显示 chsrc 安装程序的帮助信息,包括使用说明和可用选项
help() { help() {
if [ "$lang" = "zh" ]; then if [ "$userOpt_lang" = "zh" ]; then
echo "chsrc-installer: 在任何类Unix操作系统上安装 chsrc" echo "chsrc-installer: 在类Unix操作系统上安装 chsrc"
echo echo
echo "使用: install.sh [选项]" echo "使用: install.sh [选项]"
echo "选项:" echo "选项:"
echo " -h 打印此帮助信息" echo "-h 打印此帮助信息"
echo " -d <destination> 指定安装目录,默认为 /usr/local/bin若已安装则覆盖旧版本" echo "-d <dir> 指定安装目录,默认为 $default_install_dir_4root$default_install_dir_4nonroot"
echo " -v <version> 指定 chsrc 版本" echo "-v <version> 指定 chsrc 版本"
echo " -l <language> 指定脚本语言,支持 zh 和 en " echo "-l <lang> 指定输出语言,支持 zh 和 en "
echo echo
else else
echo "chsrc-installer: Install chsrc on any Unix-like OS" echo "chsrc-installer: Install chsrc on any Unix-like OS"
echo echo
echo "Usage: install.sh [options]" echo "Usage: install.sh [options]"
echo "Options:" echo "Options:"
echo " -h Print this help information" echo "-h Print this help information"
echo " -d <destination> Specify installation directory, default is /usr/local/bin; will overwrite if already installed" echo "-d <dir> Specify installation directory, default is $default_install_dir_4root OR $default_install_dir_4nonroot"
echo " -v <version> Specify chsrc version" echo "-v <version> Specify chsrc version"
echo " -l <language> Specify script language, supports zh and en" echo "-l <lang> Specify output language, supports zh and en"
echo echo
fi fi
} }
set_install_path() { #
if [ -n "$install_dir" ]; then # 1. 若用户指定了安装目录,则安装至那里
#
# 若安装目录不存在,该脚本会为用户创建
#
# 2. 若用户没有指定安装目录:
#
# 2.1 若 chsrc 已存在,自动安装到该位置
# 2.2 若 chsrc 不存在,安装到两个预定义的默认安装位置
#
set_install_dir() {
if [ -n "$userOpt_install_dir" ]; then
# 扩展 ~ 符号 # 扩展 ~ 符号
install_dir="${install_dir/#\~/$HOME}" userOpt_install_dir="${userOpt_install_dir/#\~/$HOME}"
# 检查路径是否存在,如果不存在则创建该路径 # 检查目录是否存在,如果不存在则创建该目录
if [ ! -d "$install_dir" ]; then if [ ! -d "$userOpt_install_dir" ]; then
# 多种语言输出 # 多种语言输出
if [ "$lang" = "zh" ]; then if [ "$userOpt_lang" = "zh" ]; then
echo "目录 $install_dir 不存在,正在创建..." echo "目录 $userOpt_install_dir 不存在,正在创建..."
else else
echo "Directory $install_dir does not exist. Creating..." echo "Directory $userOpt_install_dir does not exist. Creating..."
fi fi
# 多语言输出 # 多语言输出
if ! mkdir -p "$install_dir"; then if ! mkdir -p "$userOpt_install_dir"; then
if [ "$lang" = "zh" ]; then if [ "$userOpt_lang" = "zh" ]; then
echo "创建目录失败,请重试" echo "创建目录失败,请重试"
else else
echo "Failed to create directory, please try again" echo "Failed to create directory, please try again"
@ -85,29 +97,29 @@ set_install_path() {
exit 1 exit 1
fi fi
temp_install_dir="$install_dir" # 记录临时安装目录 tmp_created_install_dir="$userOpt_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
if [ "$lang" = "zh" ]; then if [ "$userOpt_lang" = "zh" ]; then
info "$binary_name 已安装,更新路径: ${existing_path}" info "$binary_name 已安装,更新路径: ${existing_path}"
else else
info "$binary_name is already installed, updating path: ${existing_path}" info "$binary_name is already installed, updating path: ${existing_path}"
fi fi
install_dir=$(dirname "$existing_path") userOpt_install_dir=$(dirname "$existing_path")
else else
# 检查默认路径 # 检查默认安装目录
if [ -d "$default_install_path" ] && [ -w "$default_install_path" ]; then if [ -d "$default_install_dir_4root" ] && [ -w "$default_install_dir_4root" ]; then
install_dir="$default_install_path" userOpt_install_dir="$default_install_dir_4root"
elif [ -d "$noroot_default_install_path" ] && [ -w "$noroot_default_install_path" ]; then elif [ -d "$default_install_dir_4nonroot" ] && [ -w "$default_install_dir_4nonroot" ]; then
install_dir="$noroot_default_install_path" userOpt_install_dir="$default_install_dir_4nonroot"
else else
if [ "$lang" = "zh" ]; then if [ "$userOpt_lang" = "zh" ]; then
error "默认下载路径 /usr/local/bin 不可写,请使用 sudo 命令运行脚本;或通过 -d 参数指定其它路径安装" error "默认安装目录 $default_install_dir_4root$default_install_dir_4nonroot 不可写,请使用 sudo 命令运行脚本;或通过 -d 参数指定其它目录安装"
else else
error "Default download path /usr/local/bin is not writable. Please run the script with sudo; or specify another path using the -d option." error "Default install dir $default_install_dir_4root and $default_install_dir_4nonroot is not writable. Please run the script with sudo; or specify another dir using the -d option."
fi fi
fi fi
fi fi
@ -123,7 +135,7 @@ install() {
riscv64) arch="riscv64" ;; riscv64) arch="riscv64" ;;
armv7*) arch="armv7" ;; armv7*) arch="armv7" ;;
*) *)
if [ "$lang" = "zh" ]; then if [ "$userOpt_lang" = "zh" ]; then
error "不支持的架构: ${arch}" error "不支持的架构: ${arch}"
else else
error "Unsupported architecture: ${arch}" error "Unsupported architecture: ${arch}"
@ -137,7 +149,7 @@ install() {
linux) platform="linux" ;; linux) platform="linux" ;;
darwin) platform="macos" ;; darwin) platform="macos" ;;
*) *)
if [ "$lang" = "zh" ]; then if [ "$userOpt_lang" = "zh" ]; then
error "不支持的平台: ${platform}" error "不支持的平台: ${platform}"
else else
error "Unsupported platform: ${platform}" error "Unsupported platform: ${platform}"
@ -145,9 +157,9 @@ install() {
;; ;;
esac esac
if [[ ! "$version" =~ ^(pre|0\.1\.([4-9]))$ ]]; then if [[ ! "$userOpt_version" =~ ^(pre|0\.1\.([4-9]))$ ]]; then
# version 不符合条件,报错 # version 不符合条件,报错
if [ "$lang" = "zh" ]; then if [ "$userOpt_lang" = "zh" ]; then
error "不支持的版本: ${version},版本号必须在 0.1.4 到 0.1.9 之间或为 'pre'" error "不支持的版本: ${version},版本号必须在 0.1.4 到 0.1.9 之间或为 'pre'"
else else
error "Unsupported version: ${version}. Version number must be between 0.1.4 and 0.1.9 or 'pre'" error "Unsupported version: ${version}. Version number must be between 0.1.4 and 0.1.9 or 'pre'"
@ -156,9 +168,9 @@ install() {
url="https://gitee.com/RubyMetric/chsrc/releases/download/${version}/${binary_name}-${arch}-${platform}" url="https://gitee.com/RubyMetric/chsrc/releases/download/${version}/${binary_name}-${arch}-${platform}"
path_to_executable="${install_dir}/${binary_name}" path_to_executable="${userOpt_install_dir}/${binary_name}"
if [ "$lang" = "zh" ]; then if [ "$userOpt_lang" = "zh" ]; then
info "下载 ${binary_name} (${arch} 架构, ${platform} 平台, ${version}版本) 到 ${path_to_executable}" info "下载 ${binary_name} (${arch} 架构, ${platform} 平台, ${version}版本) 到 ${path_to_executable}"
else else
info "Downloading ${binary_name} (${arch} architecture, ${platform} platform, version ${version}) to ${path_to_executable}" info "Downloading ${binary_name} (${arch} architecture, ${platform} platform, version ${version}) to ${path_to_executable}"
@ -167,14 +179,14 @@ install() {
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"
if [ "$lang" = "zh" ]; then if [ "$userOpt_lang" = "zh" ]; then
info "🎉 安装完成,版本: $version,路径: $path_to_executable" info "🎉 安装完成,版本: $userOpt_version,路径: $path_to_executable"
else else
info "🎉 Installation completed, path: $path_to_executable" info "🎉 Installation completed, path: $path_to_executable"
fi fi
else else
if [ "$lang" = "zh" ]; then if [ "$userOpt_lang" = "zh" ]; then
error "下载失败,请检查您的网络连接和代理设置: ${url}" error "下载失败,请检查您的网络连接和代理设置: ${url}"
else else
error "Download failed, please check your network connection and proxy settings: ${url}" error "Download failed, please check your network connection and proxy settings: ${url}"
@ -183,37 +195,35 @@ install() {
fi fi
} }
# 清理函数
cleanup() { cleanup() {
if [ -n "$temp_install_dir" ] && [ -d "$temp_install_dir" ]; then if [ -n "$tmp_created_install_dir" ] && [ -d "$tmp_created_install_dir" ]; then
if [ "$lang" = "zh" ]; then if [ "$userOpt_lang" = "zh" ]; then
echo "清理创建的目录: $temp_install_dir" echo "清理创建的目录: $tmp_created_install_dir"
else else
echo "Cleaning up created directory: $temp_install_dir" echo "Cleaning up created directory: $tmp_created_install_dir"
fi fi
rm -rf "$temp_install_dir" rm -rf "$tmp_created_install_dir"
fi fi
} }
# 设置 trap 以捕获退出信号
trap cleanup EXIT trap cleanup EXIT
# 从命令行读取 安装路径与版本号 # main
while getopts ":hd:v:l:" option; do while getopts ":hd:v:l:" option; do
case $option in case $option in
h) h)
helpflag=1 userOpt_help=1
;; ;;
d) d)
install_dir=${OPTARG} userOpt_install_dir=${OPTARG}
;; ;;
v) v)
version=${OPTARG} userOpt_version=${OPTARG}
;; ;;
l) l)
lang=${OPTARG} userOpt_lang=${OPTARG}
;; ;;
\?) \?)
echo "无效的命令行选项,请使用 -h 查看帮助" echo "无效的命令行选项,请使用 -h 查看帮助"
@ -222,15 +232,14 @@ while getopts ":hd:v:l:" option; do
esac esac
done done
# 判断语言的类型,不符合直接退出 if [[ "$userOpt_lang" != "zh" && "$userOpt_lang" != "en" ]]; then
if [[ "$lang" != "zh" && "$lang" != "en" ]]; then error "无效的语言选项: $userOpt_lang,支持的选项为 zh 和 en"
error "无效的语言选项: $lang,支持的选项为 zh 和 en"
fi fi
if [ "$helpflag" -eq 1 ]; then if [ "$userOpt_help" -eq 1 ]; then
help help
exit 0; exit 0;
fi fi
set_install_path set_install_dir
install install