Skip to content

Commit

Permalink
Merge pull request #20 from vincenthou/feat-text-format
Browse files Browse the repository at this point in the history
feat(format): support text format in console with color
  • Loading branch information
wgliang authored Jun 16, 2017
2 parents c5b15b8 + 18331c9 commit 15d8f82
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ _testmain.go
*.exe
*.test
*.prof
*.json
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ $ goreporter -p [projectRelativePath] -r [reportPath] -e [exceptPackagesName] -f
- -p Must be a valid Golang project path.
- -r Save the path to the report.
- -e Exceptional packages (multiple separated by commas, for example: "linters/aligncheck,linters/cyclo" ).
- -f report format json OR html.
- -f report format json, html OR text.
- -t Template path,if not specified, the default template will be used.

By default, the default template is used to generate reports in html format.
Expand Down
2 changes: 1 addition & 1 deletion engine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Summary struct {
}

// Metric as template of report and will save all linters result
// data.But may have some diffreence in diffrent linter.
// data.But may have some difference in different linter.
type Metric struct {
Name string `json:"name"`
Description string `json:"description"`
Expand Down
2 changes: 1 addition & 1 deletion engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (w *WaitGroupWrapper) Wrap(cb func()) {
}

// NewReporter will return Reporter.
func NewReporter(templateHtml string) *Reporter {
func NewReporter() *Reporter {
return &Reporter{
Metrics: make(map[string]Metric, 0),
syncRW: new(sync.RWMutex),
Expand Down
2 changes: 1 addition & 1 deletion engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import (
)

func Test_Engine(t *testing.T) {
report := NewReporter("")
report := NewReporter()
report.Engine("../../logcool", "")
}
4 changes: 2 additions & 2 deletions linters/countcode/countcode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import (
"testing"
)

func Test_CountLines(t *testing.T) {
CountLines("../../../goreporter")
func Test_CountCode(t *testing.T) {
CountCode("../../../goreporter")
}
18 changes: 11 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,20 @@ func main() {
}

startTime := strconv.FormatInt(time.Now().Unix(), 10)
reporter := engine.NewReporter(templateHtml)
reporter := engine.NewReporter()
reporter.Engine(*project, *except)
htmlData, err := tools.Json2Html(reporter.FormateReport2Json())
if err != nil {
glog.Errorln("Json2Html error")
return
}
jsonData := reporter.FormateReport2Json()

if *formate == "json" {
tools.SaveAsJson(reporter.FormateReport2Json(), *project, *report, startTime)
tools.SaveAsJson(jsonData, *project, *report, startTime)
} else if *formate == "text" {
tools.DisplayAsText(jsonData)
} else {
htmlData, err := tools.Json2Html(jsonData)
if err != nil {
glog.Errorln("Json2Html error")
return
}
tools.SaveAsHtml(htmlData, *project, *report, startTime, templateHtml)
}
}
4 changes: 4 additions & 0 deletions tools/report2html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1029,3 +1029,7 @@ func Test_SaveAsJson(t *testing.T) {
func Test_SaveAsJson_NoPath(t *testing.T) {
SaveAsJson([]byte(structJson), `github.com/wgliang/logcool`, "/Users/xxxxxx/Documents/xxx/src/github.com/wgliang/logcool", "221242124214")
}

func Test_DisplayAsText(t *testing.T) {
DisplayAsText([]byte(structJson))
}
65 changes: 65 additions & 0 deletions tools/report2text.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package tools

import (
"os"
"encoding/json"

"github.com/fatih/color"
"github.com/wgliang/goreporter/engine"
)

const (
headerTpl = `
_/
_/_/_/ _/_/ _/ _/_/ _/_/ _/_/_/ _/_/ _/ _/_/ _/_/_/_/ _/_/ _/ _/_/
_/ _/ _/ _/ _/_/ _/_/_/_/ _/ _/ _/ _/ _/_/ _/ _/_/_/_/ _/_/
_/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/
_/_/_/ _/_/ _/ _/_/_/ _/_/_/ _/_/ _/ _/_/ _/_/_/ _/
_/ _/
_/_/ _/
Project: %s
Score: %d
Grade: %d
Time: %s
Issues: %d
`
metricsHeaderTpl = `>> %s Linter %s find:`
summaryHeaderTpl = ` %s: %s`
errorInfoTpl = ` %s at line %d`
)

// DisplayAsText will display the json data to console
func DisplayAsText(jsonData []byte) {
var structData engine.Reporter
json.Unmarshal(jsonData, &structData)

color.Magenta(
headerTpl,
structData.Project,
structData.Score,
structData.Grade,
structData.TimeStamp,
structData.Issues,
)
for _, metric := range structData.Metrics {
if metric.Name == "DependGraph" || 0 == len(metric.Summaries) {
continue
}
color.Cyan(metricsHeaderTpl, metric.Name, metric.Description)
for _, summary := range metric.Summaries {
color.Blue(summaryHeaderTpl, summary.Name, summary.Description)
for _, errorInfo := range summary.Errors {
color.Red(errorInfoTpl, errorInfo.ErrorString, errorInfo.LineNumber)
}
}
}

if structData.Issues > 0 {
os.Exit(1)
} else {
os.Exit(0)
}
}

0 comments on commit 15d8f82

Please sign in to comment.