1 Star 0 Fork 0

kzangv/gsf-fof

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
common.go 753 Bytes
Copy Edit Raw Blame History
kzangv authored 2023-03-21 13:53 . fixed
package shell
import (
"context"
)
type Result struct {
output string
err error
}
type Cmd interface {
New(command string)
Exec() (string, error)
Destroy() error
}
// ExecShell 执行shell命令,可设置执行超时时间
func ExecShell(ctx context.Context, command string) (string, error) {
return ExecShellEx(ctx, newCmd(), command)
}
// ExecShellEx 执行shell命令,可设置执行超时时间
func ExecShellEx(ctx context.Context, cmd Cmd, command string) (string, error) {
cmd.New(command)
receiver := make(chan Result)
go func() {
output, err := cmd.Exec()
receiver <- Result{output, err}
}()
select {
case <-ctx.Done():
return "", cmd.Destroy()
case result := <-receiver:
return result.output, result.err
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/kzangv/gsf-fof.git
git@gitee.com:kzangv/gsf-fof.git
kzangv
gsf-fof
gsf-fof
v0.4.2

Search