-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
222 lines (203 loc) · 4.73 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"github.com/urfave/cli/v2"
)
const Version = "v0.1.1"
type CmdArgs struct {
Markdown bool
Html bool
LatestVersion bool
CurrentVersion bool
Path []string
Exclude []string
}
func parseArgs() CmdArgs {
cmdArgs := CmdArgs{}
app := &cli.App{
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "markdown",
Aliases: []string{"m"},
Value: false,
Usage: "Output in markdown format",
Destination: &cmdArgs.Markdown,
},
&cli.BoolFlag{
Name: "html",
Value: false,
Usage: "Output in HTML format",
Destination: &cmdArgs.Html,
},
&cli.BoolFlag{
Name: "latest-version",
Aliases: []string{"lv"},
Value: false,
Usage: "Prints the latest version and exit",
Destination: &cmdArgs.LatestVersion,
},
&cli.BoolFlag{
Name: "version",
Aliases: []string{"v"},
Value: false,
Usage: "Prints the current version and exit",
Destination: &cmdArgs.CurrentVersion,
},
&cli.StringSliceFlag{
Name: "path",
Aliases: []string{"p"},
Usage: "Paths to analyze",
},
&cli.StringSliceFlag{
Name: "exclude",
Aliases: []string{"e"},
Usage: "Comma separated list of directories to exclude",
},
},
Action: func(c *cli.Context) error {
cmdArgs.Path = c.StringSlice("path")
cmdArgs.Exclude = c.StringSlice("exclude")
return nil
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
return cmdArgs
}
func main() {
args := parseArgs()
if args.LatestVersion {
printLatestVersion()
return
}
if args.CurrentVersion {
fmt.Println(Version)
return
}
if len(args.Path) == 0 {
fmt.Println("Please provide a path to analyze")
return
}
if err := checkForConflictsInFlags(args); err != nil {
fmt.Println("Error parsing flags:", err)
return
}
fileCounts := make(map[string]*FileAnalysis)
files := make(chan string, 100)
wg := sync.WaitGroup{}
results := make(chan *FileResult, 100)
workers := runtime.NumCPU()
excludeMap := make(map[string]bool)
for _, exclude := range args.Exclude {
excludeMap[exclude] = true
}
go func() {
for _, path := range args.Path {
log.Println("Walking path", path)
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if excludeMap[path] {
return filepath.SkipDir
}
if !info.IsDir() && strings.HasSuffix(path, ".go") && !strings.HasSuffix(path, "_test.go") {
files <- path
}
return nil
})
if err != nil {
fmt.Println("Error walking the path", err)
return
}
close(files)
}
}()
for i := 0; i < workers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for path := range files {
analysis, err := analyzeFile(path)
if err != nil {
fmt.Println("Error analyzing file", err)
continue
}
results <- &FileResult{Path: path, Analysis: analysis}
}
}()
}
go func() {
wg.Wait()
close(results)
}()
for result := range results {
fileCounts[result.Path] = result.Analysis
}
switch {
case args.Markdown:
fmt.Println(convertToMarkdown(fileCounts))
case args.Html:
fmt.Println(convertToHtmlTable(fileCounts))
default:
printCounts(fileCounts)
}
}
func printLatestVersion() {
url := "https://api.github.com/repos/radurobot/go-code-test-analyzer/releases/latest"
resp, err := http.Get(url)
if resp.StatusCode != http.StatusOK || err != nil {
fmt.Println("Error getting version from github")
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response body")
return
}
var release struct {
TagName string `json:"tag_name"`
}
err = json.Unmarshal(body, &release)
if err != nil {
fmt.Println("Error parsing json")
return
}
fmt.Println(release.TagName)
}
func checkForConflictsInFlags(args CmdArgs) error {
if args.Markdown && args.Html {
return fmt.Errorf("cannot use both markdown and html flags")
}
// Create a map of excluded paths for faster lookup
excludedPaths := make(map[string]bool)
for _, exclude := range args.Exclude {
// check if exclude is valid
if _, err := os.Stat(exclude); err != nil {
return fmt.Errorf("exclude path %s is not valid", exclude)
}
excludedPaths[filepath.Clean(exclude)] = true
}
// Check if any paths are also in excludedPaths
for _, path := range args.Path {
// check if path is valid
if _, err := os.Stat(path); err != nil {
return fmt.Errorf("path %s is not valid", path)
}
if excludedPaths[filepath.Clean(path)] {
return fmt.Errorf("path %s is also in exclude list", path)
}
}
return nil
}