From 39ec016d0d0271e98163814d6f3e81feab0a16b8 Mon Sep 17 00:00:00 2001 From: tsosunchia <59512455+tsosunchia@users.noreply.github.com> Date: Thu, 26 May 2022 17:30:09 +0800 Subject: [PATCH] Update nt_install.sh MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构结构 --- nt_install.sh | 287 ++++++++++++++------------------------------------ 1 file changed, 79 insertions(+), 208 deletions(-) diff --git a/nt_install.sh b/nt_install.sh index 015bc95..b1a2e2d 100644 --- a/nt_install.sh +++ b/nt_install.sh @@ -8,28 +8,31 @@ fi usrPath="/usr/local/bin" +function red(){ + echo -e "\e[1;31m$1\e[0m" +} + checkRootPermit() { - [[ $EUID -ne 0 ]] && echo "请使用sudo/root权限运行本脚本" && exit 1 + [[ $EUID -ne 0 ]] && red "请使用sudo/root权限运行本脚本" && exit 1 } checkSystemArch() { arch=$(uname -m) - if [[ $arch == "x86_64" ]]; then - archParam="amd64" - fi - - if [[ $arch == "aarch64" ]]; then + case $arch in + 'x86_64') + archParam='amd64' + ;; + 'mips') + archParam='mips' + ;; + 'arm64'|'aarch64') archParam="arm64" - fi - - if [[ $arch == "arm64" ]]; then - archParam="arm64" - fi - - if [[ $archParam == "" ]]; then - echo "未知的系统架构,请联系作者" + ;; + *) + red "未知的系统架构,请联系开发者." exit 1 - fi + ;; + esac } checkSystemDistribution() { @@ -43,217 +46,86 @@ checkSystemDistribution() { downPath="/var/tmp/nexttrace" ;; *) - echo "unknown: $OSTYPE" + red "unknown: $OSTYPE" exit 1 ;; esac } getLocation() { - echo "正在获取地理位置信息..." + red "正在获取地理位置信息..." countryCode=$(curl -s "http://ip-api.com/line/?fields=countryCode") } -installWgetPackage() { - echo "wget 正在安装中..." - # try apt - # 是时候直接使用 APT 来管理包了 - apt-get -h &>/dev/null - if [ $? -eq 0 ]; then - # 先更新一下数据源,有些机器数据源比较老可能会404 - apt-get update -y &>/dev/null - apt-get --no-install-recommends install wget -y &>/dev/null - return 0 - fi - - # try yum - yum -h &>/dev/null - if [ $? -eq 0 ]; then - yum -y update &>/dev/null - yum install wget -y &>/dev/null - return 0 - fi - - # try dnf - dnf -h &>/dev/null - if [ $? -eq 0 ]; then - dnf check-update &>/dev/null - dnf install wget -y &>/dev/null - return 0 - fi - - # try pacman - pacman -h &>/dev/null - if [ $? -eq 0 ]; then - pacman -Sy &>/dev/null - pacman -S wget &>/dev/null - return 0 - fi - - # try zypper - zypper -h &>/dev/null - if [ $? -eq 0 ]; then - zypper refresh &>/dev/null - zypper install -y --no-recommends wget &>/dev/null - return 0 - fi - - # try brew - brew -v &>/dev/null - if [ $? -eq 0 ]; then - brew update &>/dev/null - brew install wget &>/dev/null - return 0 - fi - - # 有的发行版自带的wget,只有 --help 参数 - wget --help &>/dev/null - if [ $? -ne 0 ]; then - echo "wget 安装失败" - exit 1 +checkPackageManger() { + if [[ "$(which brew)" ]]; then #务必将brew置于第一位,macOS的apt是假的 + brew update + PACKAGE_MANAGEMENT_INSTALL='brew install' + PACKAGE_MANAGEMENT_REMOVE='brew uninstall' + elif [[ "$(which apt)" ]]; then + apt-get update + PACKAGE_MANAGEMENT_INSTALL='apt-get -y --no-install-recommends install' + PACKAGE_MANAGEMENT_REMOVE='apt-get purge' + elif [[ "$(which dnf)" ]]; then + dnf check-update + PACKAGE_MANAGEMENT_INSTALL='dnf -y install' + PACKAGE_MANAGEMENT_REMOVE='dnf remove' + elif [[ "$(which yum)" ]]; then + PACKAGE_MANAGEMENT_INSTALL='yum -y install' + PACKAGE_MANAGEMENT_REMOVE='yum remove' + elif [[ "$(which zypper)" ]]; then + zypper refresh + PACKAGE_MANAGEMENT_INSTALL='zypper install -y --no-recommends' + PACKAGE_MANAGEMENT_REMOVE='zypper remove' + elif [[ "$(which pacman)" ]]; then + PACKAGE_MANAGEMENT_INSTALL='pacman -Syu --noconfirm' + PACKAGE_MANAGEMENT_REMOVE='pacman -Rsn' + else + red "error: The script does not support the package manager in this operating system." + exit 1 fi } -installJqPackage() { - echo "jq 正在安装中...(此步骤时间可能较长,请耐心等待)" - # try apt - apt-get -h &>/dev/null - if [ $? -eq 0 ]; then - # 先更新一下数据源,有些机器数据源比较老可能会404 - apt-get update -y &>/dev/null - apt-get --no-install-recommends install jq -y &>/dev/null - return 0 - fi - - # try yum - yum -h &>/dev/null - if [ $? -eq 0 ]; then - yum -y update &>/dev/null - yum install jq -y &>/dev/null - return 0 - fi - - # try dnf - dnf -h &>/dev/null - if [ $? -eq 0 ]; then - dnf check-update &>/dev/null - dnf install jq -y &>/dev/null - return 0 - fi - - # try zypper - zypper -h &>/dev/null - if [ $? -eq 0 ]; then - zypper refresh &>/dev/null - zypper install -y --no-recommends jq &>/dev/null - return 0 - fi - - # try pacman - pacman -h &>/dev/null - if [ $? -eq 0 ]; then - pacman -Sy &>/dev/null - pacman -S jq &>/dev/null - return 0 - fi - - # try brew - brew -v &>/dev/null - if [ $? -eq 0 ]; then - brew update &>/dev/null - brew install jq &>/dev/null - return 0 - fi - - jq -h &>/dev/null - if [ $? -ne 0 ]; then - echo "jq 安装失败" - exit 1 - fi -} - -checkWgetPackage() { - wget -h &>/dev/null - if [ $? -ne 0 ]; then - if [[ $auto == True ]]; then - installWgetPackage - return 0 - fi - read -r -p "您还没有安装wget,是否安装? (y/n)" input - - case $input in - [yY][eE][sS] | [yY]) - installWgetPackage - ;; - - [nN][oO] | [nN]) - echo "您选择了取消安装,脚本即将退出" - exit 1 - ;; - - *) - installWgetPackage - ;; - esac - fi -} - -checkJqPackage() { - jq -h &>/dev/null - if [ $? -ne 0 ]; then - if [[ $auto == True ]]; then - installJqPackage - return 0 - fi - read -r -p "您还没有安装jq,是否安装? (y/n)" input - - case $input in - [yY][eE][sS] | [yY]) - installJqPackage - ;; - - [nN][oO] | [nN]) - echo "您选择了取消安装,脚本即将退出" - exit 1 - ;; - - *) - installJqPackage - ;; - esac - fi - return 1 +install_software() { + package_name="$1" + which "$package_name" > /dev/null 2>&1 && return + red "${package_name} 正在安装中...(此步骤时间可能较长,请耐心等待)" + if ${PACKAGE_MANAGEMENT_INSTALL} "$package_name"; then + red "info: $package_name is installed." + else + red "error: Installation of $package_name failed, please check your network." + exit 1 + fi } checkVersion() { - checkJqPackage nexttrace -h &>/dev/null if [ $? -ne 0 ]; then return 0 fi - echo "正在检查版本..." + red "正在检查版本..." version=$(curl -sL https://api.github.com/repos/xgadget-lab/nexttrace/releases/latest | jq -r '.tag_name') if [[ $version == "" ]]; then - echo "获取版本失败,请检查网络连接" + red "获取版本失败,请检查网络连接" exit 1 fi currentVersion=$(nexttrace -V | head -n 1 | awk '{print $2}') &> /dev/null if [[ $currentVersion == $version ]]; then - echo "当前版本已是最新版本" + red "当前版本已是最新版本" exit 0 fi - echo 当前最新release版本:${version} - echo 您当前的版本:${currentVersion} + red 当前最新release版本:${version} + red 您当前的版本:${currentVersion} if [[ $auto == True ]]; then return 0 fi - read -r -p "是否安装/更新软件? (y/n)" input + read -r -p "是否更新软件? (y/n)" input case $input in [yY][eE][sS] | [yY]) return 0 ;; [nN][oO] | [nN]) - echo "您选择了取消安装/更新,脚本即将退出" + red "您选择了取消更新,脚本即将退出" exit 1 ;; *) @@ -263,12 +135,11 @@ checkVersion() { } downloadBinrayFile() { - echo "正在获取最新版的 NextTrace 发行版文件信息..." - checkJqPackage + red "正在获取最新版的 NextTrace 发行版文件信息..." # 简单说明一下,Github提供了一个API,可以获取最新发行版本的二进制文件下载地址(对应的是browser_download_url),根据刚刚测得的osDistribution、archParam,获取对应的下载地址 if [[ $? -eq 1 ]]; then # 支持 jq 不回退 - # echo nexttrace_${osDistribution}_${archParam} + # red nexttrace_${osDistribution}_${archParam} latestURL=$(curl -s https://api.github.com/repos/xgadget-lab/nexttrace/releases/latest | jq ".assets[] | select(.name == \"nexttrace_${osDistribution}_${archParam}\") | .browser_download_url") latestURL=${latestURL:1:-1} else @@ -286,7 +157,7 @@ downloadBinrayFile() { ;; [nN][oO] | [nN]) - echo "您选择了不使用镜像,下载可能会变得异常缓慢,或者失败" + red "您选择了不使用镜像,下载可能会变得异常缓慢,或者失败" ;; *) @@ -296,29 +167,28 @@ downloadBinrayFile() { fi fi - echo "正在下载 NextTrace 二进制文件..." + red "正在下载 NextTrace 二进制文件..." wget -O ${downPath} ${latestURL} &>/dev/null if [ $? -eq 0 ]; then - echo "NextTrace 现在已经在您的系统中可用" + red "NextTrace 现在已经在您的系统中可用" changeMode mv ${downPath} ${usrPath} - if [[ ${osDistribution} == "darwin" ]]; then - xattr -r -d com.apple.quarantine ${usrPath}/nexttrace - fi else - echo "NextTrace 下载失败,请检查您的网络是否正常" + red "NextTrace 下载失败,请检查您的网络是否正常" exit 1 fi } changeMode() { chmod +x ${downPath} &>/dev/null + [[ ${osDistribution} == "darwin" ]] && xattr -r -d com.apple.quarantine ${downPath} } runBinrayFileHelp() { if [ -e ${usrPath} ]; then ${usrPath}/nexttrace -h fi + red "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE wget jq" } addCronTask() { @@ -329,13 +199,13 @@ addCronTask() { case $input in [yY][eE][sS] | [yY]) if [[ ${osDistribution} == "darwin" ]]; then - crontab -l >crontab.bak + crontab -l >crontab.bak 2>/dev/null sed -i '' '/nt_install.sh/d' crontab.bak elif [[ ${osDistribution} == "linux" ]]; then - crontab -l >crontab.bak + crontab -l >crontab.bak 2>/dev/null sed -i '/nt_install.sh/d' crontab.bak else - echo "暂不支持您的系统,无法自动添加crontab任务" + red "暂不支持您的系统,无法自动添加crontab任务" return 0 fi echo "1 1 * * * $(dirname $(readlink -f "$0"))/nt_install.sh --auto >> /var/log/nt_install.log" >>crontab.bak @@ -343,10 +213,10 @@ addCronTask() { rm -f crontab.bak ;; [nN][oO] | [nN]) - echo "您选择了不添加自动更新任务,您也可以通过命令 再次执行此脚本 手动更新" + red "您选择了不添加自动更新任务,您也可以通过命令 再次执行此脚本 手动更新" ;; *) - echo "您选择了不添加自动更新任务,您可以通过命令 再次执行此脚本 手动更新" + red "您选择了不添加自动更新任务,您可以通过命令 再次执行此脚本 手动更新" ;; esac } @@ -355,8 +225,9 @@ addCronTask() { checkRootPermit checkSystemDistribution checkSystemArch -checkWgetPackage -checkJqPackage +checkPackageManger +install_software wget +install_software jq checkVersion # Download Procedure