From d4cbd0772a0e8587494d2cb364455c4fbf5ba09d Mon Sep 17 00:00:00 2001 From: spiritlhl Date: Fri, 3 Jan 2025 17:44:22 +0800 Subject: [PATCH] =?UTF-8?q?v0.1.4=20-=20=E4=BF=AE=E5=A4=8D=E4=B8=AD?= =?UTF-8?q?=E9=80=94=E7=BB=88=E6=AD=A2=E6=97=B6=E4=B8=8A=E4=BC=A0=E6=88=90?= =?UTF-8?q?=E5=8A=9F=E4=BD=86=E5=88=86=E4=BA=AB=E9=93=BE=E6=8E=A5=E6=9C=AA?= =?UTF-8?q?=E6=89=93=E5=8D=B0=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- goecs.go | 36 ++++++++++++++++++++++++++++++------ utils/utils.go | 17 +++++++++-------- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/goecs.go b/goecs.go index b45a6071..1555bedf 100644 --- a/goecs.go +++ b/goecs.go @@ -39,7 +39,7 @@ import ( ) var ( - ecsVersion = "v0.1.3" + ecsVersion = "v0.1.4" menuMode bool onlyChinaTest bool input, choice string @@ -323,7 +323,6 @@ func main() { minutes := int(duration.Minutes()) seconds := int(duration.Seconds()) % 60 currentTime := time.Now().Format("Mon Jan 2 15:04:05 MST 2006") - // 使用互斥锁保护output的写入 var mu sync.Mutex mu.Lock() output = utils.PrintAndCapture(func() { @@ -333,14 +332,32 @@ func main() { utils.PrintCenteredTitle("", width) }, tempOutput, output) mu.Unlock() - // 启动新的goroutine处理上传 + // 创建一个通道来传递上传结果 + resultChan := make(chan struct { + httpURL string + httpsURL string + }, 1) // 使用带缓冲的通道,避免可能的阻塞 + // 启动上传 go func() { - utils.ProcessAndUpload(output, filePath, enabelUpload) + httpURL, httpsURL := utils.ProcessAndUpload(output, filePath, enabelUpload) + resultChan <- struct { + httpURL string + httpsURL string + }{httpURL, httpsURL} uploadDone <- true }() // 等待上传完成或超时 select { - case <-uploadDone: + case result := <-resultChan: + if result.httpURL != "" || result.httpsURL != "" { + if language == "en" { + fmt.Printf("Upload successfully!\nHttp URL: %s\nHttps URL: %s\n", result.httpURL, result.httpsURL) + } else { + fmt.Printf("上传成功!\nHttp URL: %s\nHttps URL: %s\n", result.httpURL, result.httpsURL) + } + } + // 给打印操作一些时间完成 + time.Sleep(100 * time.Millisecond) os.Exit(0) case <-time.After(30 * time.Second): fmt.Println("上传超时,程序退出") @@ -592,7 +609,14 @@ func main() { default: fmt.Println("Unsupported language") } - utils.ProcessAndUpload(output, filePath, enabelUpload) + httpURL, httpsURL := utils.ProcessAndUpload(output, filePath, enabelUpload) + if httpURL != "" || httpsURL != "" { + if language == "en" { + fmt.Printf("Upload successfully!\nHttp URL: %s\nHttps URL: %s\n", httpURL, httpsURL) + } else { + fmt.Printf("上传成功!\nHttp URL: %s\nHttps URL: %s\n", httpURL, httpsURL) + } + } finish = true if runtime.GOOS == "windows" || runtime.GOOS == "darwin" { fmt.Println("Press Enter to exit...") diff --git a/utils/utils.go b/utils/utils.go index 5470f83a..30725c9d 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -299,7 +299,7 @@ func UploadText(absPath string) (string, string, error) { } // ProcessAndUpload 创建结果文件并上传文件 -func ProcessAndUpload(output string, filePath string, enableUplaod bool) { +func ProcessAndUpload(output string, filePath string, enableUplaod bool) (string, string) { // 使用 defer 来处理 panic defer func() { if r := recover(); r != nil { @@ -312,14 +312,14 @@ func ProcessAndUpload(output string, filePath string, enableUplaod bool) { err = os.Remove(filePath) if err != nil { fmt.Println("无法删除文件:", err) - return + return "", "" } } // 创建文件 file, err := os.Create(filePath) if err != nil { fmt.Println("无法创建文件:", err) - return + return "", "" } defer file.Close() // 匹配 ANSI 转义序列 @@ -331,13 +331,13 @@ func ProcessAndUpload(output string, filePath string, enableUplaod bool) { _, err = writer.WriteString(cleanedOutput) if err != nil { fmt.Println("无法写入文件:", err) - return + return "", "" } // 确保写入缓冲区的数据都刷新到文件中 err = writer.Flush() if err != nil { fmt.Println("无法刷新文件缓冲:", err) - return + return "", "" } fmt.Printf("测试结果已写入 %s\n", filePath) if enableUplaod { @@ -345,15 +345,16 @@ func ProcessAndUpload(output string, filePath string, enableUplaod bool) { absPath, err := filepath.Abs(filePath) if err != nil { fmt.Println("无法获取文件绝对路径:", err) - return + return "", "" } // 上传文件并生成短链接 http_url, https_url, err := UploadText(absPath) if err != nil { fmt.Println("上传失败,无法生成链接") fmt.Println(err.Error()) - return + return "", "" } - fmt.Printf("上传成功!\nHttp URL: %s\nHttps URL: %s\n", http_url, https_url) + return http_url, https_url } + return "", "" }