Skip to content

Commit 00b59ad

Browse files
committed
installer.sh use is_zh
1 parent 460f71b commit 00b59ad

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

tool/installer.sh

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# Contributors : Aoran Zeng <[email protected]>
88
# |
99
# Created On : <2024-10-25>
10-
# Last Modified : <2025-03-19>
10+
# Last Modified : <2025-03-25>
1111
#
1212
# chsrc installer for Linux & macOS
1313
# ---------------------------------------------------------------
@@ -39,8 +39,18 @@ error() {
3939
exit 1
4040
}
4141

42-
help() {
42+
43+
is_zh() {
4344
if [ "$userOpt_lang" = "zh" ]; then
45+
return 0
46+
else
47+
return 1
48+
fi
49+
}
50+
51+
52+
help() {
53+
if is_zh; then
4454
echo "chsrc-installer: 在类Unix操作系统上安装 chsrc"
4555
echo
4656
echo "使用: install.sh [选项]"
@@ -88,7 +98,7 @@ set_arch() {
8898
riscv64) arch="riscv64" ;;
8999
armv7*) arch="armv7" ;;
90100
*)
91-
if [ "$userOpt_lang" = "zh" ]; then
101+
if is_zh; then
92102
error "不支持的架构: ${arch}"
93103
else
94104
error "Unsupported arch: ${arch}"
@@ -106,7 +116,7 @@ set_platform() {
106116
platform="linux"
107117
whatos=$(get_os)
108118
if [ "$whatos" = "android" ]; then
109-
if [ "$userOpt_lang" = "zh" ]; then
119+
if is_zh; then
110120
info "抱歉, 暂无预编译二进制文件供安卓使用。请自行编译:"
111121
else
112122
info "Sorry, No precompiled binaries for Android! Please compile it on your own:"
@@ -118,7 +128,7 @@ set_platform() {
118128
darwin) platform="macos" ;;
119129
bsd|dragonfly)
120130
platform="bsd"
121-
if [ "$userOpt_lang" = "zh" ]; then
131+
if is_zh; then
122132
info "抱歉, 暂无预编译二进制文件供BSD使用。请自行编译:"
123133
else
124134
info "Sorry, No precompiled binaries for BSD! Please compile it on your own:"
@@ -128,7 +138,7 @@ set_platform() {
128138
exit 1
129139
;;
130140
*)
131-
if [ "$userOpt_lang" = "zh" ]; then
141+
if is_zh; then
132142
error "不支持的平台: ${platform}"
133143
else
134144
error "Unsupported platform: ${platform}"
@@ -141,7 +151,7 @@ set_platform() {
141151
set_binary_version() {
142152
if [[ ! "$userOpt_version" =~ ^(pre|0\.([1-9])\.([0-9]))$ ]]; then
143153
# version 不符合条件,报错
144-
if [ "$userOpt_lang" = "zh" ]; then
154+
if is_zh; then
145155
error "不支持的版本: ${userOpt_version},版本号必须为 0.x.y (>=0.1.4) 或 'pre'"
146156
else
147157
error "Unsupported version: ${userOpt_version}. Version number must be 0.x.y (>=0.1.4) or 'pre'"
@@ -175,14 +185,14 @@ set_install_dir() {
175185
# 检查目录是否存在,如果不存在则创建该目录
176186
if [ ! -d "$userOpt_install_dir" ]; then
177187

178-
if [ "$userOpt_lang" = "zh" ]; then
188+
if is_zh; then
179189
echo "目录 $userOpt_install_dir 不存在,正在创建..."
180190
else
181191
echo "Directory $userOpt_install_dir does not exist. Creating..."
182192
fi
183193

184194
if ! mkdir -p "$userOpt_install_dir"; then
185-
if [ "$userOpt_lang" = "zh" ]; then
195+
if is_zh; then
186196
echo "创建目录失败,请重试"
187197
else
188198
echo "Failed to create directory, please try again"
@@ -195,7 +205,7 @@ set_install_dir() {
195205

196206
elif existing_path=$(command -v "$binary_name" 2>/dev/null); then
197207

198-
if [ "$userOpt_lang" = "zh" ]; then
208+
if is_zh; then
199209
info "$binary_name 已安装,更新路径: ${existing_path}"
200210
else
201211
info "$binary_name is already installed, updating path: ${existing_path}"
@@ -209,7 +219,7 @@ set_install_dir() {
209219
elif [ -d "$default_install_dir_4nonroot" ] && [ -w "$default_install_dir_4nonroot" ]; then
210220
userOpt_install_dir="$default_install_dir_4nonroot"
211221
else
212-
if [ "$userOpt_lang" = "zh" ]; then
222+
if is_zh; then
213223
error "默认安装目录 $default_install_dir_4root$default_install_dir_4nonroot 不可写,请使用 sudo 命令运行脚本;或通过 -d 参数指定其它目录安装"
214224
else
215225
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."
@@ -234,7 +244,7 @@ download() {
234244
return $?
235245
fi
236246

237-
if [ "$userOpt_lang" = "zh" ]; then
247+
if is_zh; then
238248
error "缺乏必要的下载工具(curl或wget),无法下载文件"
239249
else
240250
error "Missing necessary download tools (curl or wget) to download the file!"
@@ -256,38 +266,34 @@ install() {
256266

257267
path_to_executable="${userOpt_install_dir}/${binary_name}"
258268

259-
if [ "$userOpt_lang" = "zh" ]; then
269+
if is_zh; then
260270
info "下载 ${binary_name} (架构: ${arch}, 平台: ${platform}, 版本: ${binary_version}) 到 ${path_to_executable}"
261271
else
262272
info "Downloading ${binary_name} (arch: ${arch}, platform: ${platform}, version: ${binary_version}) to ${path_to_executable}"
263273
fi
264274

265-
download $url "$path_to_executable"
266-
267-
if $?; then
275+
if download $url "$path_to_executable"; then
268276
chmod +x "$path_to_executable"
269277

270-
if [ "$userOpt_lang" = "zh" ]; then
278+
if is_zh; then
271279
info "🎉 安装完成,路径: $path_to_executable"
272280
else
273281
info "🎉 Installation completed, path: $path_to_executable"
274282
fi
275-
276283
else
277-
if [ "$userOpt_lang" = "zh" ]; then
284+
if is_zh; then
278285
error "下载失败,请检查您的网络连接和代理设置: ${url}"
279286
else
280287
error "Download failed, please check your network connection and proxy settings: ${url}"
281288
fi
282-
283289
fi
284290
}
285291

286292

287293
cleanup() {
288294
if [ -n "$tmp_created_install_dir" ] && [ -d "$tmp_created_install_dir" ]; then
289295

290-
if [ "$userOpt_lang" = "zh" ]; then
296+
if is_zh; then
291297
echo "清理创建的目录: $tmp_created_install_dir"
292298
else
293299
echo "Cleaning up created directory: $tmp_created_install_dir"

0 commit comments

Comments
 (0)