Skip to content

Commit f4f7306

Browse files
author
V2RaySSR综合网
committed
代码优化:统一错误处理,修复GitHub Actions语法错误
1 parent 6ac1bb4 commit f4f7306

File tree

7 files changed

+72
-79
lines changed

7 files changed

+72
-79
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ jobs:
121121
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
122122

123123
- name: Upload artifacts (for non-tag builds)
124-
if: "!startsWith(github.ref, 'refs/tags/')"
124+
if: ${{ !startsWith(github.ref, 'refs/tags/') }}
125125
uses: actions/upload-artifact@v4
126126
with:
127127
name: reality-checker-builds

.gitignore

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
1-
# 编译后的可执行文件
21
reality-checker
3-
reality-checker.exe
4-
reality-checker-linux-amd64
5-
reality-checker-windows-amd64.exe
6-
reality-checker-darwin-amd64
7-
reality-checker-linux-arm64
8-
9-
# 项目状态文档(本地开发用)
102
PROJECT_STATUS.md
113

12-
# 子项目目录
13-
reality-checker-go/
14-
15-
# Cursor IDE配置
16-
.cursor/
17-
184
# 系统文件
195
.DS_Store
206
Thumbs.db
@@ -26,15 +12,7 @@ Thumbs.db
2612
*.swo
2713
*~
2814

29-
# IDE配置
30-
.vscode/
31-
.idea/
32-
*.iml
33-
3415
# Go相关
3516
vendor/
3617
*.test
3718
*.prof
38-
39-
# 配置文件(可选)
40-
config.yaml

internal/cmd/batch.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ func (r *RootCmd) executeBatch(domainsStr string) {
1313
domains, invalidDomains, duplicateDomains := parseDomains(domainsStr)
1414

1515
if len(domains) == 0 {
16-
fmt.Println()
17-
fmt.Println("错误:没有有效的域名可以检测")
18-
fmt.Println("提示:请检查域名格式,例如:apple.com, google.com")
19-
fmt.Println()
16+
ui.PrintErrorWithDetails(
17+
"错误:没有有效的域名可以检测",
18+
"提示:请检查域名格式,例如:apple.com, google.com",
19+
)
2020
return
2121
}
2222

internal/cmd/check.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ func (r *RootCmd) executeCheck(domain string) {
1616
// 验证域名格式
1717
domain = strings.TrimSpace(domain)
1818
if !isValidDomain(domain) {
19-
fmt.Println()
20-
fmt.Printf("错误:域名格式无效 '%s'\n", domain)
21-
fmt.Println("提示:请检查域名格式,例如:apple.com, google.com")
22-
fmt.Println("域名要求:")
23-
fmt.Println(" - 只能包含字母、数字、连字符和点")
24-
fmt.Println(" - 不能以点开头或结尾")
25-
fmt.Println(" - 不能包含连续的点")
26-
fmt.Println(" - 长度不超过253个字符")
27-
fmt.Println()
19+
ui.PrintErrorWithDetails(
20+
fmt.Sprintf("错误:域名格式无效 '%s'", domain),
21+
"提示:请检查域名格式,例如:apple.com, google.com",
22+
"域名要求:",
23+
" - 只能包含字母、数字、连字符和点",
24+
" - 不能以点开头或结尾",
25+
" - 不能包含连续的点",
26+
" - 长度不超过253个字符",
27+
)
2828
return
2929
}
3030

internal/cmd/csv.go

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,19 @@ import (
1313
func (r *RootCmd) executeCSV(csvFile string) {
1414
// 检查文件是否存在
1515
if _, err := os.Stat(csvFile); os.IsNotExist(err) {
16-
fmt.Println()
17-
fmt.Printf("错误:CSV文件不存在 '%s'\n", csvFile)
18-
fmt.Println("请使用 RealiTLScanner 工具扫描,得到 CSV 文件")
19-
fmt.Println("命令:./RealiTLScanner -addr <VPS IP> -port 443 -thread 50 -timeout 5 -out file.csv")
20-
fmt.Println("(提示:RealiTLScanner 不要在VPS上面运行)")
21-
fmt.Println()
16+
ui.PrintErrorWithDetails(
17+
fmt.Sprintf("错误:CSV文件不存在 '%s'", csvFile),
18+
"请使用 RealiTLScanner 工具扫描,得到 CSV 文件",
19+
"命令:./RealiTLScanner -addr <VPS IP> -port 443 -thread 50 -timeout 5 -out file.csv",
20+
"(提示:RealiTLScanner 不要在VPS上面运行)",
21+
)
2222
return
2323
}
2424

2525
// 读取CSV文件
2626
file, err := os.Open(csvFile)
2727
if err != nil {
28-
fmt.Println()
29-
fmt.Printf("错误:无法打开CSV文件 '%s': %v\n", csvFile, err)
30-
fmt.Println()
28+
ui.PrintError(fmt.Sprintf("错误:无法打开CSV文件 '%s': %v", csvFile, err))
3129
return
3230
}
3331
defer file.Close()
@@ -36,31 +34,31 @@ func (r *RootCmd) executeCSV(csvFile string) {
3634
reader := csv.NewReader(file)
3735
records, err := reader.ReadAll()
3836
if err != nil {
39-
fmt.Println()
40-
fmt.Printf("错误:解析CSV文件失败: %v\n", err)
41-
fmt.Println("请使用 RealiTLScanner 工具扫描,得到 CSV 文件")
42-
fmt.Println("命令:./RealiTLScanner -addr <VPS IP> -port 443 -thread 50 -timeout 5 -out file.csv")
43-
fmt.Println()
37+
ui.PrintErrorWithDetails(
38+
fmt.Sprintf("错误:解析CSV文件失败: %v", err),
39+
"请使用 RealiTLScanner 工具扫描,得到 CSV 文件",
40+
"命令:./RealiTLScanner -addr <VPS IP> -port 443 -thread 50 -timeout 5 -out file.csv",
41+
)
4442
return
4543
}
4644

4745
if len(records) < 2 {
48-
fmt.Println()
49-
fmt.Println("错误:CSV文件格式错误或为空")
50-
fmt.Println("请使用 RealiTLScanner 工具扫描,得到 CSV 文件")
51-
fmt.Println("命令:./RealiTLScanner -addr <VPS IP> -port 443 -thread 50 -timeout 5 -out file.csv")
52-
fmt.Println()
46+
ui.PrintErrorWithDetails(
47+
"错误:CSV文件格式错误或为空",
48+
"请使用 RealiTLScanner 工具扫描,得到 CSV 文件",
49+
"命令:./RealiTLScanner -addr <VPS IP> -port 443 -thread 50 -timeout 5 -out file.csv",
50+
)
5351
return
5452
}
5553

5654
// 提取域名(从CERT_DOMAIN列)
5755
domains := extractDomainsFromCSV(records)
5856
if len(domains) == 0 {
59-
fmt.Println()
60-
fmt.Println("错误:未找到有效的域名")
61-
fmt.Println("请使用 RealiTLScanner 工具扫描,得到 CSV 文件")
62-
fmt.Println("命令:./RealiTLScanner -addr <VPS IP> -port 443 -thread 50 -timeout 5 -out file.csv")
63-
fmt.Println()
57+
ui.PrintErrorWithDetails(
58+
"错误:未找到有效的域名",
59+
"请使用 RealiTLScanner 工具扫描,得到 CSV 文件",
60+
"命令:./RealiTLScanner -addr <VPS IP> -port 443 -thread 50 -timeout 5 -out file.csv",
61+
)
6462
return
6563
}
6664

internal/cmd/root.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,43 +73,43 @@ func (r *RootCmd) Execute() {
7373
switch os.Args[1] {
7474
case "check":
7575
if len(os.Args) < 3 {
76-
fmt.Println()
77-
fmt.Println("错误:缺少域名参数")
78-
fmt.Println("用法: reality-checker check <domain>")
79-
fmt.Println("示例: reality-checker check apple.com")
80-
fmt.Println()
76+
ui.PrintErrorWithDetails(
77+
"错误:缺少域名参数",
78+
"用法: reality-checker check <domain>",
79+
"示例: reality-checker check apple.com",
80+
)
8181
os.Exit(1)
8282
}
8383
r.executeCheck(os.Args[2])
8484
case "batch":
8585
if len(os.Args) < 3 {
86-
fmt.Println()
87-
fmt.Println("错误:缺少域名参数")
88-
fmt.Println("用法: reality-checker batch <domain1> <domain2> <domain3> ...")
89-
fmt.Println("示例: reality-checker batch apple.com google.com microsoft.com")
90-
fmt.Println()
86+
ui.PrintErrorWithDetails(
87+
"错误:缺少域名参数",
88+
"用法: reality-checker batch <domain1> <domain2> <domain3> ...",
89+
"示例: reality-checker batch apple.com google.com microsoft.com",
90+
)
9191
os.Exit(1)
9292
}
9393
// 将所有参数(除了命令名)合并为空格分隔的字符串
9494
domainsStr := strings.Join(os.Args[2:], " ")
9595
r.executeBatch(domainsStr)
9696
case "csv":
9797
if len(os.Args) < 3 {
98-
fmt.Println()
99-
fmt.Println("错误:缺少CSV文件参数")
100-
fmt.Println("用法: reality-checker csv <csv_file>")
101-
fmt.Println("示例: reality-checker csv domains.csv")
102-
fmt.Println()
98+
ui.PrintErrorWithDetails(
99+
"错误:缺少CSV文件参数",
100+
"用法: reality-checker csv <csv_file>",
101+
"示例: reality-checker csv domains.csv",
102+
)
103103
os.Exit(1)
104104
}
105105
r.executeCSV(os.Args[2])
106106
case "version", "-v", "--version":
107107
r.showVersion()
108108
default:
109-
fmt.Println()
110-
fmt.Printf("错误:未知命令 '%s'\n", os.Args[1])
111-
fmt.Println("可用命令: check, batch, csv, version")
112-
fmt.Println()
109+
ui.PrintErrorWithDetails(
110+
fmt.Sprintf("错误:未知命令 '%s'", os.Args[1]),
111+
"可用命令: check, batch, csv, version",
112+
)
113113
os.Exit(1)
114114
}
115115
}

internal/ui/display.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@ func PrintTimestampedMessage(format string, args ...interface{}) {
3737
fmt.Printf("[%s] %s\n", timestamp, message)
3838
}
3939

40+
// PrintError 打印错误信息(带空行间距)
41+
func PrintError(message string) {
42+
fmt.Println()
43+
fmt.Println(message)
44+
fmt.Println()
45+
}
46+
47+
// PrintErrorWithDetails 打印错误信息和详细信息
48+
func PrintErrorWithDetails(message string, details ...string) {
49+
fmt.Println()
50+
fmt.Println(message)
51+
for _, detail := range details {
52+
fmt.Println(detail)
53+
}
54+
fmt.Println()
55+
}
56+
4057
// getLatestVersion 获取GitHub最新版本号
4158
func getLatestVersion() string {
4259
// 设置超时时间

0 commit comments

Comments
 (0)