diff options
| author | Fuwn <[email protected]> | 2026-02-27 07:13:17 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-02-27 07:13:17 +0000 |
| commit | 856e2994722e2e7f67b47d55b8e673ddabcebe83 (patch) | |
| tree | 5a4e108384038eaa072d8e6c5f71ab68901fb431 /internal/report/report.go | |
| download | kivia-main.tar.xz kivia-main.zip | |
Diffstat (limited to 'internal/report/report.go')
| -rw-r--r-- | internal/report/report.go | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/internal/report/report.go b/internal/report/report.go new file mode 100644 index 0000000..a97039e --- /dev/null +++ b/internal/report/report.go @@ -0,0 +1,80 @@ +package report + +import ( + "encoding/json" + "fmt" + "github.com/Fuwn/kivia/internal/analyze" + "github.com/Fuwn/kivia/internal/collect" + "io" + "strings" +) + +func Render(writer io.Writer, result analyze.Result, format string, includeContext bool) error { + switch strings.ToLower(format) { + case "json": + return renderJSON(writer, result, includeContext) + case "text", "": + return renderText(writer, result, includeContext) + default: + return fmt.Errorf("Unsupported output format %q. Use \"text\" or \"json\".", format) + } +} + +func renderText(writer io.Writer, result analyze.Result, includeContext bool) error { + if len(result.Violations) == 0 { + _, err := fmt.Fprintln(writer, "No naming violations found.") + + return err + } + + for _, violation := range result.Violations { + if _, err := fmt.Fprintf(writer, "%s:%d:%d %s %q: %s\n", + violation.Identifier.File, + violation.Identifier.Line, + violation.Identifier.Column, + violation.Identifier.Kind, + violation.Identifier.Name, + violation.Reason, + ); err != nil { + return err + } + + if includeContext { + contextParts := make([]string, 0, 3) + + if violation.Identifier.Context.Type != "" { + contextParts = append(contextParts, "type="+violation.Identifier.Context.Type) + } + + if violation.Identifier.Context.ValueExpression != "" { + contextParts = append(contextParts, "value="+violation.Identifier.Context.ValueExpression) + } + + if violation.Identifier.Context.EnclosingFunction != "" { + contextParts = append(contextParts, "function="+violation.Identifier.Context.EnclosingFunction) + } + + if len(contextParts) > 0 { + if _, err := fmt.Fprintf(writer, " context: %s\n", strings.Join(contextParts, ", ")); err != nil { + return err + } + } + } + } + + return nil +} + +func renderJSON(writer io.Writer, result analyze.Result, includeContext bool) error { + if !includeContext { + for index := range result.Violations { + result.Violations[index].Identifier.Context = collect.Context{} + } + } + + encoder := json.NewEncoder(writer) + + encoder.SetIndent("", " ") + + return encoder.Encode(result) +} |