代码拉取完成,页面将自动刷新
package system
import (
"bytes"
"fmt"
"gitee.com/h79/goutils/common"
"io"
"os"
"os/exec"
"runtime"
"strings"
"time"
)
type Config struct {
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
EnableStd bool `json:"enableStd" yaml:"enableStd" xml:"enableStd"`
EnableEnv bool `json:"enableEnv" yaml:"enableEnv" xml:"enableEnv"`
}
type CmdResult struct {
stdout bytes.Buffer
stderr bytes.Buffer
Command string
Err error
StartTime time.Time
EndTime time.Time
}
func (r CmdResult) Result() string {
if r.Err != nil {
return r.stderr.String()
}
return r.stdout.String()
}
func (r CmdResult) Error() string {
if r.Err != nil {
return fmt.Sprintf("CmdResult: %s, err: %v", r.Command, r.Err)
}
return fmt.Sprintf("CmdResult: %s, Result: %s, StartTime:%v, EndTime: %v", r.Command, r.Result, r.StartTime, r.EndTime)
}
func StripArgs(args []string, arg string) []string {
ll := len(args)
for i := 0; i < ll; {
if args[i] == arg {
next := 1
if i+1 < ll && args[i+1][0] != '-' {
next = 2
}
args = append(args[:i], args[i+next:]...)
break
}
i++
}
return args
}
func initShell(args string) *exec.Cmd {
var args_ []string
var cmd *exec.Cmd
switch runtime.GOOS {
case "darwin":
fallthrough
case "linux":
args_ = append(args_, "-c")
args_ = append(args_, args)
cmd = exec.Command(os.Getenv("SHELL"), args_...)
break
case "windows":
args_ = append(args_, "/C")
args_ = append(args_, args)
cmd = exec.Command("cmd", args_...)
break
default:
os.Exit(1)
}
return cmd
}
func initCmd(cmd *exec.Cmd, conf *Config, env ...string) {
if conf.EnableEnv {
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, env...)
}
if conf.EnableStd {
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
} else {
cmd.Stdin = conf.Stdin
cmd.Stdout = conf.Stdout
cmd.Stderr = conf.Stderr
}
sysProcAttr(cmd)
}
func ShellStart(args []string, config Config, env ...string) (*exec.Cmd, error) {
cmd := initShell(strings.Join(args, " "))
return CmdStart(cmd, &config, env...)
}
func CmdStart(cmd *exec.Cmd, conf *Config, env ...string) (*exec.Cmd, error) {
initCmd(cmd, conf, env...)
err := cmd.Start()
if err != nil {
return nil, err
}
return cmd, nil
}
// CmdRun 超时同步执行, 等待结果返回
// Deprecated: this function simply calls SyncExec.
func CmdRun(cmd string, timeout time.Duration) CmdResult {
return SyncExec(cmd, timeout)
}
// ShellRun 阻塞式同步执行
func ShellRun(cmd string) CmdResult {
res := CmdResult{Command: cmd, StartTime: time.Now()}
conf := Config{
Stdout: &res.stdout,
Stderr: &res.stderr,
}
exeCmd := initShell(cmd)
initCmd(exeCmd, &conf)
res.Err = exeCmd.Run()
res.EndTime = time.Now()
return res
}
// SyncExec 超时同步执行
// timeout = 0, default 30 seconds
func SyncExec(cmd string, timeout time.Duration) CmdResult {
var (
exeCmd *exec.Cmd
res = CmdResult{Command: cmd, StartTime: time.Now()}
conf = Config{
Stdout: &res.stdout,
Stderr: &res.stderr,
}
tt = timeout * time.Second
)
exeCmd = initShell(cmd)
initCmd(exeCmd, &conf)
if tt <= 0 {
tt = 30 * time.Second
}
t := time.After(tt)
stop := make(chan struct{}, 1)
go func() {
defer common.Recover()
res.Err = exeCmd.Run()
res.EndTime = time.Now()
stop <- struct{}{}
}()
select {
case <-stop:
break
case <-t:
if exeCmd.Process != nil {
err := exeCmd.Process.Kill()
return CmdResult{Command: cmd, Err: fmt.Errorf("cmd time out, kill the process,%v", err)}
}
return CmdResult{Command: cmd, Err: fmt.Errorf("cmd time out")}
}
return res
}
// AsyExec 异步执行,返回chan
func AsyExec(cmd string) <-chan CmdResult {
resCh := make(chan CmdResult, 1)
go func() {
defer common.Recover()
res := ShellRun(cmd)
resCh <- res
}()
return resCh
}
// CreateTmpShellFile 创建临时的 shell 脚本文件
// content 创建的脚本内容
func CreateTmpShellFile(pattern, content string) (tmpFile string, err error) {
file, err := os.CreateTemp("", pattern)
if err != nil {
return
}
defer func() {
file.Close()
if runtime.GOOS == "windows" {
tmpFile = file.Name() + ".cmd"
} else {
tmpFile = file.Name() + ".sh"
}
err = os.Rename(file.Name(), tmpFile)
}()
file.Chmod(0777)
if runtime.GOOS == "windows" {
} else {
_, err = file.WriteString("#!/bin/bash\n")
if err != nil {
return
}
_, err = file.WriteString("set -e\n")
if err != nil {
return
}
}
_, err = file.WriteString(content)
return
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。