github.com/elek/golangci-lint@v1.42.2-0.20211208090441-c05b7fcb3a9a/pkg/printers/json.go (about)

     1  package printers
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  
     8  	"github.com/elek/golangci-lint/pkg/logutils"
     9  	"github.com/elek/golangci-lint/pkg/report"
    10  	"github.com/elek/golangci-lint/pkg/result"
    11  )
    12  
    13  type JSON struct {
    14  	rd *report.Data
    15  }
    16  
    17  func NewJSON(rd *report.Data) *JSON {
    18  	return &JSON{
    19  		rd: rd,
    20  	}
    21  }
    22  
    23  type JSONResult struct {
    24  	Issues []result.Issue
    25  	Report *report.Data
    26  }
    27  
    28  func (p JSON) Print(ctx context.Context, issues []result.Issue) error {
    29  	res := JSONResult{
    30  		Issues: issues,
    31  		Report: p.rd,
    32  	}
    33  
    34  	outputJSON, err := json.Marshal(res)
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	fmt.Fprint(logutils.StdOut, string(outputJSON))
    40  	return nil
    41  }