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