github.com/tetrafolium/tflint@v0.8.0/printer/checkstyle.go (about)

     1  package printer
     2  
     3  import (
     4  	"encoding/xml"
     5  	"fmt"
     6  
     7  	"sort"
     8  
     9  	"github.com/wata727/tflint/issue"
    10  )
    11  
    12  type Error struct {
    13  	Detector string `xml:"detector,attr"`
    14  	Line     int    `xml:"line,attr"`
    15  	Severity string `xml:"severity,attr"`
    16  	Message  string `xml:"message,attr"`
    17  	Link     string `xml:"link,attr"`
    18  }
    19  
    20  type File struct {
    21  	Name   string  `xml:"name,attr"`
    22  	Errors []Error `xml:"error"`
    23  }
    24  
    25  type Checkstyle struct {
    26  	XMLName xml.Name `xml:"checkstyle"`
    27  	Files   []File   `xml:"file"`
    28  }
    29  
    30  func (p *Printer) CheckstylePrint(issues []*issue.Issue) {
    31  	sort.Sort(issue.ByFile{Issues: issue.Issues(issues)})
    32  
    33  	v := &Checkstyle{}
    34  
    35  	for _, i := range issues {
    36  		if len(v.Files) == 0 || v.Files[len(v.Files)-1].Name != i.File {
    37  			v.Files = append(v.Files, File{Name: i.File})
    38  		}
    39  		v.Files[len(v.Files)-1].Errors = append(
    40  			v.Files[len(v.Files)-1].Errors,
    41  			Error{
    42  				Detector: i.Detector,
    43  				Line:     i.Line,
    44  				Severity: toSeverity(i.Type),
    45  				Message:  i.Message,
    46  				Link:     i.Link,
    47  			},
    48  		)
    49  	}
    50  
    51  	result, err := xml.MarshalIndent(v, "", "  ")
    52  	if err != nil {
    53  		fmt.Fprint(p.stderr, err)
    54  	}
    55  	fmt.Fprint(p.stdout, xml.Header)
    56  	fmt.Fprint(p.stdout, string(result))
    57  }
    58  
    59  func toSeverity(lintType string) string {
    60  	switch lintType {
    61  	case issue.ERROR:
    62  		return "error"
    63  	case issue.WARNING:
    64  		return "warning"
    65  	case issue.NOTICE:
    66  		return "info"
    67  	default:
    68  		return "unknown"
    69  	}
    70  }