/
main.go
80 lines (65 loc) · 1.39 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
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
// command phpl is a wrapper for php's native linter which adds support for
// linting many files concurrently.
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
func main() {
err := exec.Command("which", "php").Run()
if err != nil {
log.Fatal("command not found: php")
}
files := getFiles(".")
length := len(files)
jobs := make(chan string, length)
results := make(chan bool, length)
for w := 1; w <= 10; w++ {
go worker(w, jobs, results)
}
for _, file := range files {
jobs <- file
}
close(jobs)
for a := 1; a <= length; a++ {
<-results
}
}
func getFiles(root string) []string {
var files []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
path = strings.TrimSpace(path)
if isLintable(path) {
files = append(files, path)
}
return err
})
if err != nil {
log.Fatal(err)
}
return files
}
func isLintable(path string) bool {
if path == "" || strings.HasPrefix(path, "vendor") || strings.HasPrefix(path, "node_modules") {
return false
}
return strings.HasSuffix(path, ".php")
}
func worker(id int, jobs <-chan string, results chan<- bool) {
for j := range jobs {
lint(j)
results <- true
}
}
func lint(file string) {
stdout, err := exec.Command("php", "-l", file).Output()
if err != nil {
message := string(stdout)
message = strings.TrimSpace(message)
fmt.Println(message)
}
}