-
Notifications
You must be signed in to change notification settings - Fork 271
/
main.go
166 lines (146 loc) · 4.99 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
// Copyright 2017 The GoReporter Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// GoReporter is a Golang tool that does static analysis, unit testing, code
// review and generate code quality report.
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"runtime"
"strings"
"sync"
"time"
"github.com/360EntSecGroup-Skylar/goreporter/engine"
"github.com/360EntSecGroup-Skylar/goreporter/engine/processbar"
"github.com/facebookgo/inject"
)
// Received parameters, you can control some features using:
//
// -p:Specify the relative path of your project(Must Be Relative path),
// by default, the current path is used
// -r:Specifies the save path for the generated report,
// by default, the current path is used
// -e:Ignored detection of packages and multiple packages separated by commas.
// -t:Customize the path of the report template, not necessarily using the
// default report template
// -f:Set the format to generate reports, support text, html and json,not
// necessarily using the default formate-html.
const VERSION = "v3.0.0"
var (
version = flag.Bool("version", false, "print GoReporter version.")
projectPath = flag.String("p", "", "path of project.")
reportPath = flag.String("r", "", "path of report.")
exceptPackages = flag.String("e", "", "except packages.")
templatePath = flag.String("t", "", "report html template path.")
reportFormat = flag.String("f", "", "project report format(text/json/html).")
coresOfCPU = flag.Int("c", -1, "cores of CPU.")
)
func main() {
flag.Parse()
if *coresOfCPU != -1 && *coresOfCPU <= runtime.NumCPU() {
runtime.GOMAXPROCS(*coresOfCPU)
}
if *version {
fmt.Printf("GoReporter %s\r\n", VERSION)
os.Exit(0)
}
if *projectPath == "" {
log.Fatal("The project path is not specified")
} else {
_, err := os.Stat(*projectPath)
if err != nil {
log.Fatal("project path is invalid")
}
}
var templateHtml string
if *templatePath == "" {
templateHtml = engine.DefaultTpl
log.Println("The template path is not specified,and will use the default template")
} else {
if !strings.HasSuffix(*templatePath, ".html") {
log.Println("The template file is not a html template")
}
fileData, err := ioutil.ReadFile(*templatePath)
if err != nil {
log.Fatal(err)
} else {
templateHtml = string(fileData)
}
}
if *reportPath == "" {
log.Println("The report path is not specified, and the current path is used by default")
} else {
_, err := os.Stat(*reportPath)
if err != nil {
log.Fatal("report path is invalid:", err)
}
}
if *exceptPackages == "" {
log.Println("There are no packages that are excepted, review all items of the package")
}
synchronizer := &engine.Synchronizer{
LintersProcessChans: make(chan int64, 20),
LintersFinishedSignal: make(chan string, 10),
}
syncRW := &sync.RWMutex{}
waitGW := &engine.WaitGroupWrapper{}
reporter := engine.NewReporter(*projectPath, *reportPath, *reportFormat, templateHtml)
strategyCountCode := &engine.StrategyCountCode{}
strategyCyclo := &engine.StrategyCyclo{}
strategyDeadCode := &engine.StrategyDeadCode{}
strategyDependGraph := &engine.StrategyDependGraph{}
strategyDepth := &engine.StrategyDepth{}
strategyImportPackages := &engine.StrategyImportPackages{}
strategyInterfacer := &engine.StrategyInterfacer{}
strategySimpleCode := &engine.StrategySimpleCode{}
strategySpellCheck := &engine.StrategySpellCheck{}
strategyUnitTest := &engine.StrategyUnitTest{}
strategyLint := &engine.StrategyLint{}
strategyGoVet := &engine.StrategyGoVet{}
strategyGoFmt := &engine.StrategyGoFmt{}
if err := inject.Populate(
reporter,
synchronizer,
strategyCountCode,
strategyCyclo,
strategyDeadCode,
strategyDependGraph,
strategyDepth,
strategyImportPackages,
strategyInterfacer,
strategySimpleCode,
strategySpellCheck,
strategyUnitTest,
strategyLint,
strategyGoVet,
strategyGoFmt,
syncRW,
waitGW,
); err != nil {
log.Fatal(err)
}
reporter.AddLinters(strategyCountCode, strategyCyclo, strategyDeadCode, strategyDependGraph,
strategyDepth, strategyImportPackages, strategyInterfacer, strategySimpleCode,
strategySpellCheck, strategyUnitTest, strategyLint, strategyGoVet, strategyGoFmt)
go processbar.LinterProcessBar(synchronizer.LintersProcessChans, synchronizer.LintersFinishedSignal)
if err := reporter.Report(); err != nil {
log.Fatal(err)
}
if err := reporter.Render(); err != nil {
log.Fatal(err)
}
log.Println(fmt.Sprintf("GoReporter Finished,time consuming %vs", time.Since(reporter.StartTime).Seconds()))
}