-
Notifications
You must be signed in to change notification settings - Fork 18
/
filter.go
56 lines (48 loc) · 1.09 KB
/
filter.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
package compress
import (
"bytes"
"os"
"os/exec"
"strings"
)
var (
ClosureBin = "java -jar compiler.jar"
ClosureArgs = map[string]string{
"compilation_level": "SIMPLE_OPTIMIZATIONS",
"warning_level": "QUIET",
}
YuiBin = "java -jar yuicompressor.jar"
YuiArgs = map[string]string{
"type": "css",
}
)
func ClosureFilter(source string) string {
args := strings.Fields(ClosureBin)
for arg, value := range ClosureArgs {
args = append(args, "--"+arg)
args = append(args, value)
}
return runFilter(args[0], args[1:], source)
}
func YuiFilter(source string) string {
args := strings.Fields(YuiBin)
for arg, value := range YuiArgs {
args = append(args, "--"+arg)
args = append(args, value)
}
return runFilter(args[0], args[1:], source)
}
func runFilter(bin string, args []string, source string) string {
buf := bytes.NewBufferString(source)
out := bytes.NewBufferString("")
cmd := exec.Command(bin, args...)
cmd.Stdin = buf
cmd.Stderr = os.Stderr
cmd.Stdout = out
if err := cmd.Run(); err != nil {
logError(err.Error())
return source
} else {
return out.String()
}
}