Improved Logging
This commit is contained in:
+21
-40
@@ -6,7 +6,6 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"gitea.westgate.pw/ShadowsOverWestgate/sow-tools/internal/pipeline"
|
||||
@@ -235,19 +234,11 @@ func runValidate(ctx context) error {
|
||||
}
|
||||
|
||||
validationReport := validator.ValidateProject(p)
|
||||
emitValidatorReport(ctx.stderr, validationReport)
|
||||
if validationReport.HasErrors() {
|
||||
emitValidationWarnings(ctx.stderr, validationReport.Diagnostics)
|
||||
for _, diagnostic := range validationReport.Diagnostics {
|
||||
if diagnostic.Severity == validator.SeverityWarning {
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(ctx.stderr, "error: %s: %s\n", diagnostic.Path, diagnostic.Message)
|
||||
}
|
||||
return fmt.Errorf("validation failed with %d error(s)", validationReport.ErrorCount())
|
||||
}
|
||||
|
||||
emitValidationWarnings(ctx.stderr, validationReport.Diagnostics)
|
||||
|
||||
inventoryReport := p.Inventory.Report()
|
||||
fmt.Fprintf(ctx.stdout, "project: %s\n", p.Config.Module.Name)
|
||||
fmt.Fprintf(ctx.stdout, "root: %s\n", p.Root)
|
||||
@@ -262,34 +253,30 @@ func runValidate(ctx context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func emitValidationWarnings(w io.Writer, diagnostics []validator.Diagnostic) {
|
||||
const lowercaseWarning = "resource filenames should be lowercase"
|
||||
|
||||
lowercasePaths := make([]string, 0)
|
||||
for _, diagnostic := range diagnostics {
|
||||
if diagnostic.Severity != validator.SeverityWarning {
|
||||
continue
|
||||
}
|
||||
if diagnostic.Message == lowercaseWarning {
|
||||
lowercasePaths = append(lowercasePaths, diagnostic.Path)
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(w, "warning: %s: %s\n", diagnostic.Path, diagnostic.Message)
|
||||
func emitValidatorReport(w io.Writer, report validator.Report) {
|
||||
lines := report.SummaryLines(5)
|
||||
emitSummaryLines(w, lines, 20, "validation diagnostics")
|
||||
if line := report.DecompiledSummaryLine(12); line != "" {
|
||||
fmt.Fprintln(w, line)
|
||||
}
|
||||
}
|
||||
|
||||
if len(lowercasePaths) == 0 {
|
||||
func emitTopdataValidationReport(w io.Writer, report topdata.ValidationReport) {
|
||||
lines := report.SummaryLines(5)
|
||||
emitSummaryLines(w, lines, 20, "topdata validation diagnostics")
|
||||
}
|
||||
|
||||
func emitSummaryLines(w io.Writer, lines []string, maxLines int, label string) {
|
||||
if maxLines <= 0 || len(lines) <= maxLines {
|
||||
for _, line := range lines {
|
||||
fmt.Fprintln(w, line)
|
||||
}
|
||||
return
|
||||
}
|
||||
slices.Sort(lowercasePaths)
|
||||
examples := lowercasePaths
|
||||
if len(examples) > 5 {
|
||||
examples = examples[:5]
|
||||
for _, line := range lines[:maxLines] {
|
||||
fmt.Fprintln(w, line)
|
||||
}
|
||||
message := strings.Join(examples, ", ")
|
||||
if len(lowercasePaths) > len(examples) {
|
||||
message += fmt.Sprintf(", and %d more", len(lowercasePaths)-len(examples))
|
||||
}
|
||||
fmt.Fprintf(w, "warning: %d resource filenames are not lowercase: %s\n", len(lowercasePaths), message)
|
||||
fmt.Fprintf(w, "info: %d additional %s omitted\n", len(lines)-maxLines, label)
|
||||
}
|
||||
|
||||
func runCompare(ctx context) error {
|
||||
@@ -346,13 +333,7 @@ func runValidateTopData(ctx context) error {
|
||||
}
|
||||
|
||||
report := topdata.ValidateProject(p)
|
||||
for _, diagnostic := range report.Diagnostics {
|
||||
if diagnostic.Severity == topdata.SeverityWarning {
|
||||
fmt.Fprintf(ctx.stderr, "warning: %s: %s\n", diagnostic.Path, diagnostic.Message)
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(ctx.stderr, "error: %s: %s\n", diagnostic.Path, diagnostic.Message)
|
||||
}
|
||||
emitTopdataValidationReport(ctx.stderr, report)
|
||||
if report.HasErrors() {
|
||||
return fmt.Errorf("topdata validation failed with %d error(s)", report.ErrorCount())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user