github.com/mattbailey/reviewdog@v0.10.0/comment_iowriter.go (about)

     1  package reviewdog
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"strings"
     8  )
     9  
    10  var _ CommentService = &RawCommentWriter{}
    11  
    12  // RawCommentWriter is comment writer which writes results to given writer
    13  // without any formatting.
    14  type RawCommentWriter struct {
    15  	w io.Writer
    16  }
    17  
    18  func NewRawCommentWriter(w io.Writer) *RawCommentWriter {
    19  	return &RawCommentWriter{w: w}
    20  }
    21  
    22  func (s *RawCommentWriter) Post(_ context.Context, c *Comment) error {
    23  	_, err := fmt.Fprintln(s.w, strings.Join(c.CheckResult.Lines, "\n"))
    24  	return err
    25  }
    26  
    27  var _ CommentService = &UnifiedCommentWriter{}
    28  
    29  // UnifiedCommentWriter is comment writer which writes results to given writer
    30  // in one of following unified formats.
    31  //
    32  // Format:
    33  //   - <file>: [<tool name>] <message>
    34  //   - <file>:<lnum>: [<tool name>] <message>
    35  //   - <file>:<lnum>:<col>: [<tool name>] <message>
    36  // where <message> can be multiple lines.
    37  type UnifiedCommentWriter struct {
    38  	w io.Writer
    39  }
    40  
    41  func NewUnifiedCommentWriter(w io.Writer) *UnifiedCommentWriter {
    42  	return &UnifiedCommentWriter{w: w}
    43  }
    44  
    45  func (mc *UnifiedCommentWriter) Post(_ context.Context, c *Comment) error {
    46  	s := c.Path
    47  	if c.Lnum > 0 {
    48  		s += fmt.Sprintf(":%d", c.Lnum)
    49  		if c.Col > 0 {
    50  			s += fmt.Sprintf(":%d", c.Col)
    51  		}
    52  	}
    53  	s += fmt.Sprintf(": [%s] %s", c.ToolName, c.Body)
    54  	_, err := fmt.Fprintln(mc.w, s)
    55  	return err
    56  }