重构代码, 将一键脚本封装为main函数

This commit is contained in:
Olia Lisa 2026-01-14 22:35:56 +08:00
parent 4b4e15a7fc
commit 4eb55e029f

View File

@ -1,10 +1,9 @@
#!/bin/bash #!/bin/bash
script_dir="$(cd "$(dirname "$0")"; pwd)/bin" # 脚本所在目录
source "${script_dir}/utils/base.sh"
install(){ install(){
local script_dir="$(cd "$(dirname "$0")"; pwd)/bin" # 脚本所在目录
source "${script_dir}/utils/base.sh"
if ! command -v curl >/dev/null 2>&1; then if ! command -v curl >/dev/null 2>&1; then
install_package curl install_package curl
fi fi
@ -30,65 +29,72 @@ install(){
fi fi
} }
# 显示菜单 main(){
echo "请选择一个操作:" local script_dir="$(cd "$(dirname "$0")"; pwd)/bin" # 脚本所在目录
echo "1. 一键部署" source "${script_dir}/utils/base.sh"
echo "2. 生成配置 / 重置配置"
echo "3. 查看分享链接"
echo "4. 启动容器"
echo "5. 停止容器"
echo "6. 更新镜像"
echo "7. 修改域名"
echo "8. 修改端口"
# 读取用户选择 # 显示菜单
read -p "输入您的选择: " choice echo "请选择一个操作:"
echo "1. 一键部署"
echo "2. 生成配置 / 重置配置"
echo "3. 查看分享链接"
echo "4. 启动容器"
echo "5. 停止容器"
echo "6. 更新镜像"
echo "7. 修改域名"
echo "8. 修改端口"
# 根据用户选择执行相应的操作 # 读取用户选择
case $choice in read -p "输入您的选择: " choice
1)
# 一键部署 # 根据用户选择执行相应的操作
install case $choice in
bash ./bin/create_config.sh 1)
bash ./bin/run.sh # 一键部署
bash ./bin/print_share_link.sh install
;; bash ./bin/create_config.sh
2) bash ./bin/run.sh
# 生成配置 bash ./bin/print_share_link.sh
bash ./bin/create_config.sh ;;
;; 2)
3) # 生成配置
# 查看分享链接 bash ./bin/create_config.sh
check_config_file ;;
bash ./bin/print_share_link.sh 3)
;; # 查看分享链接
4) check_config_file
# 启动容器 bash ./bin/print_share_link.sh
bash ./bin/run.sh ;;
bash ./bin/print_share_link.sh 4)
;; # 启动容器
5) bash ./bin/run.sh
# 停止容器 bash ./bin/print_share_link.sh
echo "正在停止容器.." ;;
docker-compose -f ./docker-compose.yml down 5)
;; # 停止容器
6) echo "正在停止容器.."
# 更新镜像 docker-compose -f ./docker-compose.yml down
bash ./bin/update_docker_images.sh ;;
;; 6)
7) # 更新镜像
# 修改域名 bash ./bin/update_docker_images.sh
check_config_file ;;
bash ./bin/update_domain.sh 7)
restart_docker # 修改域名
;; check_config_file
8) bash ./bin/update_domain.sh
# 修改端口 restart_docker
check_config_file ;;
bash ./bin/update_port.sh 8)
restart_docker # 修改端口
;; check_config_file
*) bash ./bin/update_port.sh
echo "无效的选择, 请重新选择." restart_docker
;; ;;
esac *)
echo "无效的选择, 请重新选择."
;;
esac
}
main