-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelper.go
96 lines (85 loc) · 2.22 KB
/
helper.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
package main
import (
log "github.com/sirupsen/logrus"
"golang.org/x/text/encoding/simplifiedchinese"
"math"
"os/exec"
"strconv"
"strings"
"time"
"unicode/utf8"
)
type Charset string
const (
UTF8 = Charset("UTF-8")
GB18030 = Charset("GB18030")
)
// 处理终端中文显示乱码
func convertByte2String(byte []byte, charset Charset) string {
var str string
switch charset {
case GB18030:
var decodeBytes, _ = simplifiedchinese.GB18030.NewDecoder().Bytes(byte)
str = string(decodeBytes)
case UTF8:
fallthrough
default:
str = string(byte)
}
return str
}
// 按需切割、拼接字符串
func interceptStrFunc(str string, num int) string {
if num <= 0 {
return str
}
strLen := utf8.RuneCountInString(str)
if strLen <= num {
return str
}
// 换行符
symbol := "\n"
// 向上取整
float64Num := float64(num)
CeilNum := math.Ceil(float64(strLen) / float64Num)
intC := int(CeilNum)
// 初始值
s := 0
_num := num
var builder strings.Builder
for j := 1; j <= intC; j++ {
if j == intC {
num = strLen
}
builder.WriteString(string([]rune(str)[s:num]))
builder.WriteString(symbol)
s = s + _num
num = num + _num
}
return builder.String()
}
func execBash(Cmd string) {
// 执行时间标记
startTime := time.Now()
outputByte, outputErr := exec.Command("bash", "-c", Cmd).CombinedOutput()
checkExec(outputErr, Cmd, outputByte, startTime)
}
func execCmd(Cmd string) {
startTime := time.Now()
outputByte, outputErr := exec.Command("cmd", "/c", Cmd).CombinedOutput()
checkExec(outputErr, Cmd, outputByte, startTime)
}
// 检测bash 、cmd 的运行环境
func checkExec(outputErr error, Cmd string, outputByte []byte, startTime time.Time) {
if outputErr != nil {
// executable file not found
if strings.Contains(outputErr.Error(), "executable file not found") {
panic("请确认当前系统支持 bash 或 cmd 命令的执行环境,并且已添加至环境变量。错误:" + outputErr.Error())
}
log.Error(outputErr.Error())
}
// 结束时间标记
endTime := time.Since(startTime)
ExecSecondsS := strconv.FormatFloat(endTime.Seconds(), 'f', 2, 64)
log.Println("执行命令:", Cmd, "输出:", convertByte2String(outputByte, "GB18030"), "执行耗时:", ExecSecondsS+" s")
}