-
Notifications
You must be signed in to change notification settings - Fork 1
/
gosloc.go
148 lines (125 loc) · 3.23 KB
/
gosloc.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
var reComment = regexp.MustCompile(`^\s*?\/\/.*$`)
var reWhitespace = regexp.MustCompile(`^\s*$`)
var reGobindata = regexp.MustCompile(`^// Code generated by go-bindata.$`)
func exitWithError(err error) {
os.Stderr.WriteString(err.Error())
os.Exit(1)
}
func parseArgs() (path string, vendor bool) {
// This is a slightly janky but it lets us put -vendor before or after the
// path and things will still work. Also path is optional so we'll infer if
// the user doesn't supply one.
args := os.Args[1:]
skip := map[int]struct{}{}
for key, val := range args {
if val == "-vendor" {
vendor = true
skip[key] = struct{}{}
}
}
for key, val := range args {
if _, ok := skip[key]; ok {
continue
}
// Add a / so it works with symlinks
path = filepath.Clean(val)+"/"
}
if path == "" {
var err error
path, err = os.Getwd()
if err != nil {
exitWithError(err)
}
}
_, err := os.Stat(path)
if os.IsNotExist(err) {
exitWithError(err)
}
return path, vendor
}
func main() {
files, code, tests, blanks, comments := 0, 0, 0, 0, 0
var byteSize int64
path, vendorFlag := parseArgs()
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
name := info.Name()
if info.IsDir() {
if info.Name() == ".git" {
return filepath.SkipDir
}
if info.Name() == "testdata" {
return filepath.SkipDir
}
if info.Name() == "vendor" && !vendorFlag {
fmt.Printf("- Skipping %s (use -vendor to include)\n", path)
return filepath.SkipDir
}
}
if filepath.Ext(name) != ".go" {
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
fileCode, fileTests, fileBlanks, fileComments := 0, 0, 0, 0
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Bytes()
if reGobindata.Match(line) {
fmt.Printf("- Skipping %s (go-bindata)\n", path)
return nil
}
isComment := reComment.Match(line)
isTest := strings.HasSuffix(name, "_test.go")
if isTest {
if !isComment && !reWhitespace.Match(line) {
fileTests++
}
} else {
if reWhitespace.Match(line) {
fileBlanks++
} else if isComment {
fileComments++
} else {
fileCode++
}
}
}
if scanner.Err() != nil && scanner.Err() == bufio.ErrTooLong{
fmt.Printf("- Skipping %s because of unusually long lines\n", path)
return nil
}
files++
byteSize += info.Size()
code += fileCode
tests += fileTests
blanks += fileBlanks
comments += fileComments
return scanner.Err()
})
if err != nil {
exitWithError(err)
}
if files == 0 {
fmt.Printf("No go source files found in %s\n", path)
} else {
total := float64(code+comments+blanks)
fmt.Printf("Scanned %d go files in %s:\n", files, path)
fmt.Printf(" Source lines: %d (%.1f%%)\n", code, float64(code)/total*100)
fmt.Printf(" Comment lines: %d (%.1f%%)\n", comments, float64(comments)/total*100)
fmt.Printf(" Blank lines: %d (%.1f%%)\n", blanks, float64(blanks)/total*100)
fmt.Printf(" Test lines: %d (%.2f per line of code)\n", tests, float64(tests)/float64(code))
fmt.Printf(" Total file size: %.2f Mb\n", float64(byteSize)/(1024.0*1024.0))
}
}