-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcmd.go
105 lines (89 loc) · 1.88 KB
/
cmd.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package compress
import (
"flag"
"fmt"
"os"
)
type commander interface {
Parse([]string)
Run(*Settings) error
}
var (
commands = make(map[string]commander)
)
func (s *Settings) RunCommand(params ...string) {
cmdName := "compress"
if len(params) > 0 {
cmdName = params[0]
}
if len(os.Args) < 2 || os.Args[1] != cmdName {
return
}
args := argString(os.Args[2:])
name := args.Get(0)
if name == "help" {
printHelp()
}
if cmd, ok := commands[name]; ok {
cmd.Parse(os.Args[3:])
cmd.Run(s)
os.Exit(0)
} else {
if name == "" {
printHelp()
} else {
printHelp(fmt.Sprintf("unknown command %s", name))
}
}
}
func (s *Settings) RunCompress(force, skip, verbose bool) {
compressJsFiles(s, force, skip, verbose)
compressCssFiles(s, force, skip, verbose)
}
func printHelp(errs ...string) {
content := `compress command usage:
js - compress all js files
css - compress all css files
all - compress all files
`
if len(errs) > 0 {
fmt.Println(errs[0])
}
fmt.Println(content)
os.Exit(2)
}
type compressAll struct {
js, css bool
force bool
skip bool
verbose bool
}
func (d *compressAll) Parse(args []string) {
var name string
if d.js && d.css {
name = "all"
} else if d.js {
name = "js"
} else {
name = "css"
}
flagSet := flag.NewFlagSet("compress command: "+name, flag.ExitOnError)
flagSet.BoolVar(&d.force, "force", false, "force compress file")
flagSet.BoolVar(&d.skip, "skip", false, "skip all cached file")
flagSet.BoolVar(&d.verbose, "v", false, "verbose info")
flagSet.Parse(args)
}
func (d *compressAll) Run(s *Settings) error {
if d.js {
compressJsFiles(s, d.force, d.skip, d.verbose)
}
if d.css {
compressCssFiles(s, d.force, d.skip, d.verbose)
}
return nil
}
func init() {
commands["js"] = &compressAll{js: true}
commands["css"] = &compressAll{css: true}
commands["all"] = &compressAll{js: true, css: true}
}