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