From f82c173e92b9c57bfe7eaae17700b0681c34ef6f Mon Sep 17 00:00:00 2001 From: Efterklang <113397634+Efterklang@users.noreply.github.com> Date: Fri, 25 Oct 2024 17:13:45 +0800 Subject: [PATCH 1/2] Specify version from `install.sh` (#102) --- tool/download-release.sh | 109 +++++++++++++++++++++++++++++++++++++++ tool/install.sh | 14 +++-- 2 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 tool/download-release.sh diff --git a/tool/download-release.sh b/tool/download-release.sh new file mode 100644 index 0000000..29be04c --- /dev/null +++ b/tool/download-release.sh @@ -0,0 +1,109 @@ +# --------------------------------------------------------------- +# File : download-release.sh +# Authors : GnixAij +# Created on : <2024-10-25> +# Last modified : <2024-10-25> +# +# download: +# +# Download from GitHub Releases +# --------------------------------------------------------------- + +install_dir="" +path_to_executable="" +default_install_path="/usr/local/bin" +binary_name="chsrc" + +info() { + echo "[INFO] $*" +} + +error() { + echo -e "[ERROR] $*" >&2 + exit 1 +} + +help() { + # 显示帮助 + echo "chsrc 安装脚本" + echo + echo "用法: install.sh [-h] [-d <安装目录>]" + echo "选项:" + echo "-h 打印此帮助信息。" + echo "-d 指定安装目录。默认为 /usr/local/bin;如果已安装,则覆盖旧版本。" + echo +} + +# 解析命令行选项 +while getopts ":hd:" option; do + case $option in + h) + help + exit 0 + ;; + d) + install_dir=${OPTARG} + ;; + v) + version=${OPTARG} + ;; + \?) + echo "无效的命令行选项。使用 -h 查看帮助。" + exit 1 + ;; + esac +done + +# 设置安装路径 +set_install_path() { + if [ -n "$install_dir" ]; then + # 扩展 ~ 符号 + install_dir="${install_dir/#\~/$HOME}" + elif existing_path=$(command -v "$binary_name" 2>/dev/null); then + info "$binary_name 已安装,更新路径: ${existing_path}" + install_dir=$(dirname "$existing_path") + else + # 检查默认路径 + if [ -d "$default_install_path" ] && [ -w "$default_install_path" ]; then + install_dir="$default_install_path" + else + error "默认下载路径/usr/local/bin 不可写,请使用 sudo 命令运行脚本;或通过-v参数指定其它路径安装" + fi + fi +} + +# 下载并安装函数 +install() { + arch="$(uname -m | tr '[:upper:]' '[:lower:]')" + case "$arch" in + x86_64) arch="x64" ;; + aarch64) arch="aarch64" ;; + riscv64) arch="riscv64" ;; + armv7*) arch="armv7" ;; + *) error "不支持的架构: ${arch}" ;; + esac + + platform="$(uname -s | awk '{print tolower($0)}')" + case "$platform" in + linux) platform="linux" ;; + darwin) platform="macos" ;; + *) error "不支持的平台: ${platform}" ;; + esac + + url="https://gitee.com/RubyMetric/chsrc/releases/download/pre/${binary_name}-${arch}-${platform}" + + path_to_executable="${install_dir}/${binary_name}" + + info "下载 ${binary_name} (${arch} 架构, ${platform} 平台) 到 ${path_to_executable}" + + # 下载文件并设置权限 + if curl -sL "$url" -o "$path_to_executable"; then + chmod +x "$path_to_executable" + info "🎉 安装完成,路径: $path_to_executable" + else + error "下载失败,请检查您的网络连接和代理设置: ${url}" + fi +} + +set_install_path +install diff --git a/tool/install.sh b/tool/install.sh index 963a760..e5e8d62 100644 --- a/tool/install.sh +++ b/tool/install.sh @@ -16,6 +16,7 @@ install_dir="" path_to_executable="" default_install_path="/usr/local/bin" binary_name="chsrc" +version="" info() { echo "[INFO] $*" @@ -74,8 +75,13 @@ install() { *) error "不支持的平台: ${platform}" ;; esac - url="https://gitee.com/RubyMetric/chsrc/releases/download/pre/${binary_name}-${arch}-${platform}" - + if [ -n "$version" ]; then + url="https://gitee.com/RubyMetric/chsrc/releases/download/v${version}/${binary_name}-${arch}-${platform}" + else + url="https://gitee.com/RubyMetric/chsrc/releases/download/pre/${binary_name}-${arch}-${platform}" + version="latest" + fi + path_to_executable="${install_dir}/${binary_name}" info "下载 ${binary_name} (${arch} 架构, ${platform} 平台) 到 ${path_to_executable}" @@ -83,7 +89,7 @@ install() { # 下载文件并设置权限 if curl -sL "$url" -o "$path_to_executable"; then chmod +x "$path_to_executable" - info "🎉 安装完成,路径: $path_to_executable" + info "🎉 安装完成,版本: $version,路径: $path_to_executable" else error "下载失败,请检查您的网络连接和代理设置: ${url}" fi @@ -91,7 +97,7 @@ install() { # main -while getopts ":hd:" option; do +while getopts ":hd:v:" option; do case $option in h) help From 6a81785a4d11575895e6030b2c59f44fb3b8e854 Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Fri, 25 Oct 2024 17:26:29 +0800 Subject: [PATCH 2/2] Supplement `-v` usage for `install.sh` --- tool/download-release.sh | 109 --------------------------------------- tool/install.sh | 15 +++--- 2 files changed, 8 insertions(+), 116 deletions(-) delete mode 100644 tool/download-release.sh diff --git a/tool/download-release.sh b/tool/download-release.sh deleted file mode 100644 index 29be04c..0000000 --- a/tool/download-release.sh +++ /dev/null @@ -1,109 +0,0 @@ -# --------------------------------------------------------------- -# File : download-release.sh -# Authors : GnixAij -# Created on : <2024-10-25> -# Last modified : <2024-10-25> -# -# download: -# -# Download from GitHub Releases -# --------------------------------------------------------------- - -install_dir="" -path_to_executable="" -default_install_path="/usr/local/bin" -binary_name="chsrc" - -info() { - echo "[INFO] $*" -} - -error() { - echo -e "[ERROR] $*" >&2 - exit 1 -} - -help() { - # 显示帮助 - echo "chsrc 安装脚本" - echo - echo "用法: install.sh [-h] [-d <安装目录>]" - echo "选项:" - echo "-h 打印此帮助信息。" - echo "-d 指定安装目录。默认为 /usr/local/bin;如果已安装,则覆盖旧版本。" - echo -} - -# 解析命令行选项 -while getopts ":hd:" option; do - case $option in - h) - help - exit 0 - ;; - d) - install_dir=${OPTARG} - ;; - v) - version=${OPTARG} - ;; - \?) - echo "无效的命令行选项。使用 -h 查看帮助。" - exit 1 - ;; - esac -done - -# 设置安装路径 -set_install_path() { - if [ -n "$install_dir" ]; then - # 扩展 ~ 符号 - install_dir="${install_dir/#\~/$HOME}" - elif existing_path=$(command -v "$binary_name" 2>/dev/null); then - info "$binary_name 已安装,更新路径: ${existing_path}" - install_dir=$(dirname "$existing_path") - else - # 检查默认路径 - if [ -d "$default_install_path" ] && [ -w "$default_install_path" ]; then - install_dir="$default_install_path" - else - error "默认下载路径/usr/local/bin 不可写,请使用 sudo 命令运行脚本;或通过-v参数指定其它路径安装" - fi - fi -} - -# 下载并安装函数 -install() { - arch="$(uname -m | tr '[:upper:]' '[:lower:]')" - case "$arch" in - x86_64) arch="x64" ;; - aarch64) arch="aarch64" ;; - riscv64) arch="riscv64" ;; - armv7*) arch="armv7" ;; - *) error "不支持的架构: ${arch}" ;; - esac - - platform="$(uname -s | awk '{print tolower($0)}')" - case "$platform" in - linux) platform="linux" ;; - darwin) platform="macos" ;; - *) error "不支持的平台: ${platform}" ;; - esac - - url="https://gitee.com/RubyMetric/chsrc/releases/download/pre/${binary_name}-${arch}-${platform}" - - path_to_executable="${install_dir}/${binary_name}" - - info "下载 ${binary_name} (${arch} 架构, ${platform} 平台) 到 ${path_to_executable}" - - # 下载文件并设置权限 - if curl -sL "$url" -o "$path_to_executable"; then - chmod +x "$path_to_executable" - info "🎉 安装完成,路径: $path_to_executable" - else - error "下载失败,请检查您的网络连接和代理设置: ${url}" - fi -} - -set_install_path -install diff --git a/tool/install.sh b/tool/install.sh index e5e8d62..a4e6107 100644 --- a/tool/install.sh +++ b/tool/install.sh @@ -8,7 +8,7 @@ # Last Modified : <2024-10-25> # # -# chsrc Installer for Linux & macOS +# chsrc installer for Linux & macOS # # --------------------------------------------------------------- @@ -28,12 +28,13 @@ error() { } help() { - echo "chsrc Installer" + echo "chsrc-installer: Install chsrc on any Unix-like OS and any architect " echo - echo "使用: install.sh [-h] [-d <安装目录>]" + echo "使用: install.sh [options]" echo "选项:" - echo "-h 打印此帮助信息" - echo "-d 指定安装目录,默认为 /usr/local/bin;如果已安装,则覆盖旧版本" + echo "-h 打印此帮助信息" + echo "-d 指定安装目录,默认为 /usr/local/bin;若已安装,则覆盖旧版本" + echo "-v 指定chsrc版本" echo } @@ -81,7 +82,7 @@ install() { url="https://gitee.com/RubyMetric/chsrc/releases/download/pre/${binary_name}-${arch}-${platform}" version="latest" fi - + path_to_executable="${install_dir}/${binary_name}" info "下载 ${binary_name} (${arch} 架构, ${platform} 平台) 到 ${path_to_executable}" @@ -110,7 +111,7 @@ while getopts ":hd:v:" option; do version=${OPTARG} ;; \?) - echo "无效的命令行选项。使用 -h 查看帮助" + echo "无效的命令行选项,请使用 -h 查看帮助" exit 1 ;; esac