refactor: reorganize menu options and add config validation checks

- Extracted common config file validation into a new `check_config` function
- Renumbered menu options for logical order, with exit as the last option
- Added config checks before viewing share links, updating port, and changing Reality destination
- Removed trailing backslash and added final newline for proper formatting

This update improves code reusability, prevents errors from missing configs, and enhances menu usability.
This commit is contained in:
Olia Lisa 2025-11-23 14:15:50 +08:00
parent f1f54ce1b5
commit 9a3e20082c

View File

@ -7,13 +7,18 @@ restart_docker(){
} }
change_reality_dest(){ check_config(){
local config_dir="./conf"
if [ ! -e "$config_dir/config.json" ] || [ ! -s "$config_dir/config.json" ];then if [ ! -e "$config_dir/config.json" ] || [ ! -s "$config_dir/config.json" ];then
echo "请先'创建配置'" echo "请先'创建配置'"
exit 0 exit 0
fi fi
}
change_reality_dest(){
local config_dir="./conf"
check_config
source ./bin/utils/jq_util.sh source ./bin/utils/jq_util.sh
@ -34,54 +39,57 @@ change_reality_dest(){
main(){ main(){
# 显示菜单 # 显示菜单
echo "请选择一个操作:" echo "请选择一个操作:"
echo "0. 创建配置 / 重置配置" echo "1. 创建配置 / 重置配置"
echo "1. 启动容器" echo "2. 启动容器"
echo "2. 查看分享链接" echo "3. 查看分享链接"
echo "3. 停止容器" echo "4. 停止容器"
echo "4. 更新镜像" echo "5. 更新镜像"
echo "5. 修改端口" echo "6. 修改端口"
echo "6. 修改Reality目标域名" echo "7. 修改Reality目标域名"
echo "0. 退出" echo "8. 退出"
# 读取用户选择 # 读取用户选择
read -p "输入您的选择: " choice read -p "输入您的选择: " choice
# 根据用户选择执行相应的操作 # 根据用户选择执行相应的操作
case $choice in case $choice in
0) 1)
# 创建配置 / 重置配置 # 创建配置 / 重置配置
bash ./bin/create_config.sh bash ./bin/create_config.sh
;; ;;
1) 2)
# 启动容器 # 启动容器
echo "启动容器.." echo "启动容器.."
bash ./bin/run.sh bash ./bin/run.sh
;; ;;
2) 3)
# 查看分享链接 # 查看分享链接
echo "查看分享链接.." echo "查看分享链接.."
check_config
bash ./bin/print_share_link.sh bash ./bin/print_share_link.sh
;; ;;
3) 4)
# 停止容器 # 停止容器
echo "正在停止容器.." echo "正在停止容器.."
docker-compose -f ./docker-compose.yml down docker-compose -f ./docker-compose.yml down
;; ;;
4) 5)
# 更新镜像 # 更新镜像
bash ./bin/update_docker_images.sh bash ./bin/update_docker_images.sh
;; ;;
5) 6)
# 修改端口 # 修改端口
check_config
bash ./bin/update_port.sh bash ./bin/update_port.sh
restart_docker restart_docker
;; ;;
6) 7)
# 修改Reality目标域名 # 修改Reality目标域名
check_config
change_reality_dest change_reality_dest
restart_docker restart_docker
;; ;;
0) 8)
# 退出 # 退出
echo "退出程序." echo "退出程序."
;; ;;