gitee.com/lonely0422/gometalinter.git@v3.0.1-0.20190307123442-32416ab75314+incompatible/checkstyle.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/xml"
     5  	"fmt"
     6  
     7  	kingpin "gopkg.in/alecthomas/kingpin.v3-unstable"
     8  )
     9  
    10  type checkstyleOutput struct {
    11  	XMLName xml.Name          `xml:"checkstyle"`
    12  	Version string            `xml:"version,attr"`
    13  	Files   []*checkstyleFile `xml:"file"`
    14  }
    15  
    16  type checkstyleFile struct {
    17  	Name   string             `xml:"name,attr"`
    18  	Errors []*checkstyleError `xml:"error"`
    19  }
    20  
    21  type checkstyleError struct {
    22  	Column   int    `xml:"column,attr"`
    23  	Line     int    `xml:"line,attr"`
    24  	Message  string `xml:"message,attr"`
    25  	Severity string `xml:"severity,attr"`
    26  	Source   string `xml:"source,attr"`
    27  }
    28  
    29  func outputToCheckstyle(issues chan *Issue) int {
    30  	var lastFile *checkstyleFile
    31  	out := checkstyleOutput{
    32  		Version: "5.0",
    33  	}
    34  	status := 0
    35  	for issue := range issues {
    36  		path := issue.Path.Relative()
    37  		if lastFile != nil && lastFile.Name != path {
    38  			out.Files = append(out.Files, lastFile)
    39  			lastFile = nil
    40  		}
    41  		if lastFile == nil {
    42  			lastFile = &checkstyleFile{Name: path}
    43  		}
    44  
    45  		if config.Errors && issue.Severity != Error {
    46  			continue
    47  		}
    48  
    49  		lastFile.Errors = append(lastFile.Errors, &checkstyleError{
    50  			Column:   issue.Col,
    51  			Line:     issue.Line,
    52  			Message:  issue.Message,
    53  			Severity: string(issue.Severity),
    54  			Source:   issue.Linter,
    55  		})
    56  		status = 1
    57  	}
    58  	if lastFile != nil {
    59  		out.Files = append(out.Files, lastFile)
    60  	}
    61  	d, err := xml.Marshal(&out)
    62  	kingpin.FatalIfError(err, "")
    63  	fmt.Printf("%s%s\n", xml.Header, d)
    64  	return status
    65  }