-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (61 loc) · 1.15 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
package main
import (
"fmt"
"os"
)
const (
ExitCodeSuccess = iota
ExitCodeFailed
)
func main() {
args := os.Args
if len(args) == 1 {
args = append(args, "help")
}
path := args[1]
// if subcommand is 'help'
if path == "help" {
fmt.Printf("%v\n", helptext)
os.Exit(ExitCodeSuccess)
}
// check the correctness of file path
s, err := os.Stat(path)
if err != nil {
printfErr(err)
os.Exit(ExitCodeFailed)
}
opts, code, err := parseRawOptions(args[2:])
if err != nil {
printfErr(err)
os.Exit(code)
}
// get the instance of command
var cmd cmder
if s.IsDir() {
cmd = newDirCmd(path)
} else {
cmd = newPageCmd(path)
}
// run command
code, err = cmd.run(opts)
if err != nil {
printfErr(err)
}
os.Exit(code)
}
const helptext = `if you want count code file, like JavaScript file, you can input:
cloc ./demo.js -sort code
the cloc tool support these types of file:
----- JavaScript
----- JSON
----- TypeScript
----- HTML
----- SCSS
----- CSS
----- Golang
if you want count directory, you can input:
cloc ./dirdemo
`
type cmder interface {
run(opts map[string]string) (code int, err error)
}