github.com/ferretdb/golangci-lint@v1.10.1/pkg/printers/json.go (about)

     1  package printers
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  
     8  	"github.com/golangci/golangci-lint/pkg/logutils"
     9  	"github.com/golangci/golangci-lint/pkg/report"
    10  	"github.com/golangci/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 <-chan result.Issue) error {
    29  	allIssues := []result.Issue{}
    30  	for i := range issues {
    31  		allIssues = append(allIssues, i)
    32  	}
    33  
    34  	res := JSONResult{
    35  		Issues: allIssues,
    36  		Report: p.rd,
    37  	}
    38  
    39  	outputJSON, err := json.Marshal(res)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	fmt.Fprint(logutils.StdOut, string(outputJSON))
    45  	return nil
    46  }