forked from hwaf/hwaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall_test.go
64 lines (54 loc) · 1.04 KB
/
all_test.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
package main_test
import (
"io"
"os"
"os/exec"
"strings"
)
type logcmd struct {
f *os.File
cmds []string
}
func newlogger(fname string) (*logcmd, error) {
f, err := os.Create(fname)
if err != nil {
return nil, err
}
return &logcmd{f: f, cmds: nil}, nil
}
func (cmd *logcmd) LastCmd() string {
if len(cmd.cmds) <= 0 {
return ""
}
return cmd.cmds[len(cmd.cmds)-1]
}
func (cmd *logcmd) Run(bin string, args ...string) error {
cmd_line := ""
{
cargs := make([]string, 1, len(args)+1)
cargs[0] = bin
cargs = append(cargs, args...)
cmd_line = strings.Join(cargs, " ")
}
cmd.cmds = append(cmd.cmds, cmd_line)
c := exec.Command(bin, args...)
c.Stdout = cmd.f
c.Stderr = cmd.f
_, err := cmd.f.WriteString("\n" + strings.Repeat("#", 80) + "\n")
if err != nil {
return err
}
_, err = cmd.f.WriteString("## " + cmd_line + "\n")
if err != nil {
return err
}
return c.Run()
}
func (cmd *logcmd) Close() error {
return cmd.f.Close()
}
func (cmd *logcmd) Display() {
cmd.f.Seek(0, 0)
io.Copy(os.Stderr, cmd.f)
}
// EOF