-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd.go
More file actions
102 lines (89 loc) · 1.92 KB
/
cmd.go
File metadata and controls
102 lines (89 loc) · 1.92 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
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
package mageutil
import (
"context"
"os"
"os/exec"
"strings"
)
func Command(ctx context.Context, command string) *exec.Cmd {
cmd := create(ctx, command)
ConnectStd(cmd)
return cmd
}
func CommandDir(ctx context.Context, dir, command string) *exec.Cmd {
cmd := create(ctx, command)
cmd.Dir = dir
ConnectStd(cmd)
return cmd
}
func Run(ctx context.Context, commands ...string) error {
for _, command := range commands {
if err := Command(ctx, command).Run(); err != nil {
return err
}
}
return nil
}
func RunDir(ctx context.Context, dir string, commands ...string) error {
for _, command := range commands {
if err := CommandDir(ctx, dir, command).Run(); err != nil {
return err
}
}
return nil
}
func Out(ctx context.Context, command string) ([]byte, error) {
cmd := create(ctx, command)
return cmd.Output()
}
func OutDir(ctx context.Context, dir, command string) ([]byte, error) {
cmd := create(ctx, command)
cmd.Dir = dir
return cmd.Output()
}
func Pipe(first, second string) error {
ctx := context.Background()
c1 := create(ctx, first)
c1.Stderr = os.Stderr
p, err := c1.StdoutPipe()
if err != nil {
return err
}
c2 := create(ctx, second)
c2.Stdin = p
c2.Stdout = os.Stdout
c2.Stderr = os.Stderr
if err = c1.Start(); err != nil {
return err
}
if err = c2.Start(); err != nil {
return err
}
if err = c1.Wait(); err != nil {
return err
}
if err = c2.Wait(); err != nil {
return err
}
return nil
}
func ConnectStd(cmd *exec.Cmd) {
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
func create(ctx context.Context, command string) *exec.Cmd {
args := make([]string, 0)
a := strings.Split(command, `"`)
for i, arg := range a {
if i%2 == 0 {
trimmed := strings.TrimSpace(arg)
if len(trimmed) > 0 {
args = append(args, strings.Split(trimmed, ` `)...)
}
} else {
args = append(args, arg)
}
}
return exec.CommandContext(ctx, args[0], args[1:]...)
}