-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathshell_118.go
More file actions
67 lines (57 loc) · 2.05 KB
/
Copy pathshell_118.go
File metadata and controls
67 lines (57 loc) · 2.05 KB
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
//go:build go1.18
// +build go1.18
package zshell
import (
"context"
"errors"
"fmt"
"io"
"os/exec"
)
func toCommand[T string | []string](command T) []string {
var commandArgs []string
switch v := any(command).(type) {
case []string:
commandArgs = v
case string:
commandArgs = fixCommand(v)
}
return commandArgs
}
func CallbackRunContext[T string | []string](ctx context.Context, command T, callback func(str string, isStdout bool), opt ...func(o *Options)) (<-chan int, func(string), error) {
return callbackRunContext(ctx, toCommand(command), callback, opt...)
}
func CallbackRun[T string | []string](command T, callback func(out string, isBasic bool), opt ...func(o *Options)) (<-chan int, func(string), error) {
return CallbackRunContext(context.Background(), command, callback, opt...)
}
func Run[T string | []string](command T, opt ...func(o *Options)) (code int, outStr, errStr string, err error) {
return RunContext(context.Background(), command, opt...)
}
func RunContext[T string | []string](ctx context.Context, command T, opt ...func(o *Options)) (code int, outStr, errStr string, err error) {
return ExecCommand(ctx, toCommand(command), nil, nil, nil, opt...)
}
func BgRun[T string | []string](command T, opt ...func(o *Options)) (err error) {
return BgRunContext(context.Background(), command, opt...)
}
func BgRunContext[T string | []string](ctx context.Context, command T, opt ...func(o *Options)) (err error) {
commandArgs := toCommand(command)
if len(commandArgs) == 0 {
return errors.New("no such command")
}
cmd := exec.CommandContext(ctx, commandArgs[0], commandArgs[1:]...)
wrapOptions(cmd, opt...)
err = cmd.Start()
if Debug {
fmt.Println("[Command]: ", command)
if err != nil {
fmt.Println("[Fail]", err.Error())
}
}
go func() {
_ = cmd.Wait()
}()
return err
}
func OutRun[T string | []string](command T, stdIn io.Reader, stdOut io.Writer, stdErr io.Writer, opt ...func(o Options) Options) (code int, outStr, errStr string, err error) {
return ExecCommand(context.Background(), toCommand(command), stdIn, stdOut, stdErr)
}