-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
123 lines (104 loc) · 2.71 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"crypto/md5"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
url2 "net/url"
"os"
"os/exec"
"regexp"
"strings"
)
// ANSI 颜色代码
const (
ColorReset = "\033[0m"
ColorGreen = "\033[32m" // 绿色
ColorRed = "\033[31m" // 红色
ColorBlue = "\033[34m" // 蓝色
)
const (
BaiduAPIURL = "https://fanyi-api.baidu.com/api/trans/vip/translate"
)
var AppID string
var SecretKey string
func translate(text string) (string, error) {
salt := "1435660288" // 随机数,可以是任意字符串
sign := generateSign(text, salt)
escape := url2.QueryEscape(text)
url := fmt.Sprintf("%s?q=%s&from=en&to=zh&appid=%s&salt=%s&sign=%s", BaiduAPIURL, escape, AppID, salt, sign)
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
var result struct {
TransResult []struct {
Dst string `json:"dst"`
} `json:"trans_result"`
}
if err := json.Unmarshal(body, &result); err != nil {
return "", err
}
if len(result.TransResult) > 0 {
return result.TransResult[0].Dst, nil
}
return "", fmt.Errorf("翻译结果为空")
}
func generateSign(text string, salt string) string {
str := fmt.Sprintf("%s%s%s%s", AppID, text, salt, SecretKey)
hash := md5.Sum([]byte(str))
return fmt.Sprintf("%x", hash)
}
func getCommandOutput(command string) (string, error) {
cmd := exec.Command("sh", "-c", command)
output, err := cmd.CombinedOutput()
return string(output), err
}
func printWithColor(line string) {
// 使用正则表达式匹配 -- 及其后所有内容
re := regexp.MustCompile(`(--\n)`)
line = re.ReplaceAllStringFunc(line, func(s string) string {
return fmt.Sprintf("%s%s%s", ColorBlue, s, ColorReset)
})
// 打印最终的行
fmt.Printf("%s%s%s\n", ColorGreen, line, ColorReset)
}
func main() {
args := os.Args[1:] // 获取命令行参数
if len(args) < 1 {
fmt.Println("请提供要执行的命令.")
return
}
AppID = os.Getenv("APPID")
SecretKey = os.Getenv("SECRETKEY")
if AppID == "" || SecretKey == "" {
fmt.Println("APPID和SECRETKEY未设置\n")
return
}
command := strings.Join(args, " ") // 组合成完整命令
output, err := getCommandOutput(command)
if err != nil {
fmt.Printf("命令执行错误: %s\n", err)
return
}
lines := strings.Split(output, "\n")
for _, line := range lines {
if strings.TrimSpace(line) != "" {
printWithColor(line)
leadingSpaces := strings.Repeat(" ", len(line)-len(strings.TrimLeft(line, " ")))
translated, err := translate(line)
if err != nil {
fmt.Println("翻译错误:", err)
continue
}
fmt.Printf("%s%s%s%s\n", leadingSpaces, ColorRed, translated, ColorReset)
fmt.Println("")
}
}
}