/
main.go
54 lines (47 loc) · 1.05 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"exec"
"path"
)
func Exitf(format string, args ...interface{}) {
fmt.Printf(format, args...)
os.Exit(1)
}
type Visitor chan<- os.Error
func (v Visitor) VisitDir(path string, f *os.FileInfo) bool {
return true
}
func (v Visitor) VisitFile(name string, file *os.FileInfo) {
fmt.Printf("path: %s, file: %#v\n", name, file)
cmd, err := exec.Run(name, []string { name }, os.Environ(), "", exec.PassThrough, exec.PassThrough, exec.PassThrough)
if err != nil {
Exitf("Unable to execute %s: %s\n", file.Name, err)
}
msg, err := cmd.Wait(0)
if err != nil {
Exitf("Unable to wait on %s: %s\n", cmd, err)
}
defer cmd.Close()
if msg.ExitStatus() != 0 {
v <- fmt.Errorf("%s exited with status %d", file.Name, msg.ExitStatus())
}
}
func errChan() chan<- os.Error {
c := make(chan os.Error)
go func() {
for err := range c {
fmt.Println(err)
}
}()
return c
}
func main() {
for _, dir := range flag.Args() {
fmt.Printf("dir: %s\n", dir)
errChan := errChan()
path.Walk(dir, Visitor(errChan), errChan);
}
}