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 "", "" }