github.com/alexey-mercari/reviewdog@v0.10.1-0.20200514053941-928943b10766/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.Result.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.Result.Path 47 if c.Result.Lnum > 0 { 48 s += fmt.Sprintf(":%d", c.Result.Lnum) 49 if c.Result.Col > 0 { 50 s += fmt.Sprintf(":%d", c.Result.Col) 51 } 52 } 53 s += fmt.Sprintf(": [%s] %s", c.ToolName, c.Body) 54 _, err := fmt.Fprintln(mc.w, s) 55 return err 56 }