修复终止命令在选项选择时不可用的问题
展示分享链接时同时展示http和https协议的链接
This commit is contained in:
spiritsoul
2024-10-04 23:30:06 +08:00
parent bdd00d46f8
commit 681da08f01
2 changed files with 163 additions and 111 deletions

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"bufio" "bufio"
"context"
"flag" "flag"
"fmt" "fmt"
"github.com/oneclickvirt/CommonMediaTests/commediatests" "github.com/oneclickvirt/CommonMediaTests/commediatests"
@@ -38,7 +39,7 @@ import (
) )
var ( var (
ecsVersion = "v0.0.74" ecsVersion = "v0.0.75"
menuMode bool menuMode bool
onlyChinaTest bool onlyChinaTest bool
input, choice string input, choice string
@@ -62,6 +63,68 @@ var (
goecsFlag = flag.NewFlagSet("goecs", flag.ContinueOnError) goecsFlag = flag.NewFlagSet("goecs", flag.ContinueOnError)
) )
func getMenuChoice(language string) string {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
defer signal.Stop(sigChan)
inputChan := make(chan string, 1)
go func() {
select {
case <-sigChan:
fmt.Println("\n程序在选择过程中被用户中断")
os.Exit(1)
case <-ctx.Done():
return
}
}()
for {
go func() {
var input string
fmt.Print("请输入选项 / Please enter your choice: ")
fmt.Scanln(&input)
input = strings.TrimSpace(input)
input = strings.TrimRight(input, "\n")
select {
case inputChan <- input:
case <-ctx.Done():
return
}
}()
select {
case input := <-inputChan:
re := regexp.MustCompile(`^\d+$`) // 正则表达式匹配纯数字
if re.MatchString(input) {
choice := input
switch choice {
case "1", "2", "3", "4", "5", "6", "7", "8", "9", "10":
return choice
default:
if language == "zh" {
fmt.Println("无效的选项")
} else {
fmt.Println("Invalid choice")
}
}
} else {
if language == "zh" {
fmt.Println("输入错误,请输入一个纯数字")
} else {
fmt.Println("Invalid input, please enter a number")
}
}
case <-ctx.Done():
return ""
}
}
}
func main() { func main() {
goecsFlag.BoolVar(&help, "h", false, "Show help information") goecsFlag.BoolVar(&help, "h", false, "Show help information")
goecsFlag.BoolVar(&showVersion, "v", false, "Display version information") goecsFlag.BoolVar(&showVersion, "v", false, "Display version information")
@@ -95,8 +158,6 @@ func main() {
goecsFlag.PrintDefaults() goecsFlag.PrintDefaults()
return return
} }
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
if showVersion { if showVersion {
fmt.Println(ecsVersion) fmt.Println(ecsVersion)
return return
@@ -149,13 +210,7 @@ func main() {
} }
Loop: Loop:
for { for {
fmt.Print("请输入选项 / Please enter your choice: ") choice := getMenuChoice(language)
fmt.Scanln(&input)
input = strings.TrimSpace(input)
input = strings.TrimRight(input, "\n")
re := regexp.MustCompile(`^\d+$`) // 正则表达式匹配纯数字
if re.MatchString(input) {
choice = input
switch choice { switch choice {
case "1": case "1":
basicStatus = true basicStatus = true
@@ -241,13 +296,6 @@ func main() {
fmt.Println("Invalid choice") fmt.Println("Invalid choice")
} }
} }
} else {
if language == "zh" {
fmt.Println("输入错误,请输入一个纯数字")
} else {
fmt.Println("Invalid input, please enter a number")
}
}
} }
} }
if language == "en" { if language == "en" {
@@ -263,6 +311,9 @@ func main() {
basicInfo, securityInfo, emailInfo, mediaInfo, ptInfo string basicInfo, securityInfo, emailInfo, mediaInfo, ptInfo string
output, tempOutput string output, tempOutput string
) )
// 设置主程序的信号处理
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
// 启动一个goroutine来等待信号内置计时器 // 启动一个goroutine来等待信号内置计时器
go func() { go func() {
startTime = time.Now() startTime = time.Now()

View File

@@ -238,7 +238,7 @@ func PrintAndCapture(f func(), tempOutput, output string) string {
} }
// UploadText 上传文本内容到指定URL // UploadText 上传文本内容到指定URL
func UploadText(absPath string) (string, error) { func UploadText(absPath string) (string, string, error) {
primaryURL := "http://hpaste.spiritlhl.net/api/upload" primaryURL := "http://hpaste.spiritlhl.net/api/upload"
backupURL := "https://paste.spiritlhl.net/api/upload" backupURL := "https://paste.spiritlhl.net/api/upload"
token := network.SecurityUploadToken token := network.SecurityUploadToken
@@ -250,10 +250,10 @@ func UploadText(absPath string) (string, error) {
SetRetryFixedInterval(2 * time.Second) SetRetryFixedInterval(2 * time.Second)
file, err := os.Open(absPath) file, err := os.Open(absPath)
if err != nil { if err != nil {
return "", err return "", "", err
} }
defer file.Close() defer file.Close()
upload := func(url string) (string, error) { upload := func(url string) (string, string, error) {
resp, err := client.R(). resp, err := client.R().
SetHeader("Authorization", token). SetHeader("Authorization", token).
SetHeader("Format", "RANDOM"). SetHeader("Format", "RANDOM").
@@ -264,23 +264,24 @@ func UploadText(absPath string) (string, error) {
SetFileReader("file", "goecs.txt", file). SetFileReader("file", "goecs.txt", file).
Post(url) Post(url)
if err != nil { if err != nil {
return "", err return "", "", err
} }
if resp.StatusCode >= 200 && resp.StatusCode <= 299 { if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
return strings.ReplaceAll(resp.String(), "https://paste.spiritlhl.net/", "http://hpaste.spiritlhl.net/"), nil return strings.ReplaceAll(resp.String(), "https://paste.spiritlhl.net/", "http://hpaste.spiritlhl.net/"),
strings.ReplaceAll(resp.String(), "http://hpaste.spiritlhl.net/", "https://paste.spiritlhl.net/"), nil
} else { } else {
return "", fmt.Errorf("upload failed with status code: %d", resp.StatusCode) return "", "", fmt.Errorf("upload failed with status code: %d", resp.StatusCode)
} }
} }
result, err := upload(primaryURL) http_url, https_url, err := upload(primaryURL)
if err == nil { if err == nil {
return result, nil return http_url, https_url, nil
} }
result, err = upload(backupURL) http_url, https_url, err = upload(backupURL)
if err != nil { if err != nil {
return "", err return "", "", err
} }
return result, nil return http_url, https_url, nil
} }
// ProcessAndUpload 创建结果文件并上传文件 // ProcessAndUpload 创建结果文件并上传文件
@@ -321,12 +322,12 @@ func ProcessAndUpload(output string, filePath string, enableUplaod bool) {
return return
} }
// 上传文件并生成短链接 // 上传文件并生成短链接
shorturl, err3 := UploadText(absPath) http_url, https_url, err3 := UploadText(absPath)
if err3 != nil { if err3 != nil {
fmt.Println("Upload failed, cannot generate short URL.") fmt.Println("Upload failed, cannot generate short URL.")
fmt.Println(err3.Error()) fmt.Println(err3.Error())
return return
} }
fmt.Println("Upload successful, short URL:", shorturl) fmt.Println("Upload successful, short URL:", http_url, https_url)
} }
} }