|
| 1 | +// Copyright 2020 Red Hat, Inc |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License.) |
| 14 | + |
| 15 | +// Package report allows butane to pretty print errors, the error format is as follows: |
| 16 | +package report |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "strings" |
| 21 | + "unicode" |
| 22 | + |
| 23 | + "github.com/coreos/vcontext/report" |
| 24 | +) |
| 25 | + |
| 26 | +const ( |
| 27 | + red = "\033[1;31m" |
| 28 | + yellow = "\033[1;33m" |
| 29 | + cyan = "\033[1;36m" |
| 30 | + blue = "\033[1;34m" |
| 31 | + reset = "\033[0m" |
| 32 | +) |
| 33 | + |
| 34 | +func FormatError(r report.Report, fileName string, source []byte, colorize, rawErrors bool) string { |
| 35 | + if rawErrors { |
| 36 | + return formatErrorSimple(r) |
| 37 | + } else { |
| 38 | + return formatErrorPretty(r, fileName, source, colorize) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func formatErrorSimple(r report.Report) string { |
| 43 | + return r.String() |
| 44 | +} |
| 45 | + |
| 46 | +func formatErrorPretty(r report.Report, fileName string, source []byte, colorize bool) string { |
| 47 | + lines := strings.Split(string(source), "\n") |
| 48 | + var buf strings.Builder |
| 49 | + for i, entry := range r.Entries { |
| 50 | + if i > 0 { |
| 51 | + buf.WriteString("\n") |
| 52 | + } |
| 53 | + buf.WriteString(formatErrorEntry(entry, fileName, lines, colorize)) |
| 54 | + } |
| 55 | + return buf.String() |
| 56 | +} |
| 57 | + |
| 58 | +func color(text, code string, colorize bool) string { |
| 59 | + if !colorize { |
| 60 | + return text |
| 61 | + } |
| 62 | + return code + text + reset |
| 63 | +} |
| 64 | + |
| 65 | +func severityColor(kind report.EntryKind) string { |
| 66 | + switch kind { |
| 67 | + case report.Error: |
| 68 | + return red |
| 69 | + case report.Info: |
| 70 | + return cyan |
| 71 | + case report.Warn: |
| 72 | + return yellow |
| 73 | + default: |
| 74 | + return reset |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func writeUnderline(buf *strings.Builder, col, gutterWidth int, message, line string, colorize bool) { |
| 79 | + underlineStart := col - 1 |
| 80 | + rest := line[underlineStart:] |
| 81 | + nextWhitespace := strings.IndexFunc(rest, unicode.IsSpace) |
| 82 | + if nextWhitespace == -1 { |
| 83 | + // If we didn't find whitespace then that means that we need to underline the entire string |
| 84 | + nextWhitespace = len(rest) |
| 85 | + } |
| 86 | + underlineEnd := underlineStart + nextWhitespace |
| 87 | + padding := strings.Repeat(" ", underlineStart) |
| 88 | + underline := strings.Repeat("^", underlineEnd-underlineStart) |
| 89 | + underline = color(underline, blue, colorize) |
| 90 | + |
| 91 | + fmt.Fprintf(buf, " %s | %s%s %s\n", |
| 92 | + strings.Repeat(" ", gutterWidth), padding, underline, message) |
| 93 | +} |
| 94 | + |
| 95 | +// formatErrorEntry will try to return the error as a pretty string in the following form |
| 96 | +// |
| 97 | +// error[$.boot_device.layout]: |
| 98 | +// |
| 99 | +// --> ../testing.bu:4:11 |
| 100 | +// | |
| 101 | +// 2 | version: 1.6.0 |
| 102 | +// 3 | boot_device: |
| 103 | +// 4 | layout: s390x-virt |
| 104 | +// | ^^^^^^^^^^ mirroring not supported on layouts: s390x-eckd, s390x-zfcp, s390x-virt |
| 105 | +// 5 | mirror: |
| 106 | +// 6 | devices: |
| 107 | +// | |
| 108 | +func formatErrorEntry(entry report.Entry, filename string, lines []string, colorize bool) string { |
| 109 | + if entry.Marker.StartP == nil { |
| 110 | + return entry.String() + "\n" |
| 111 | + } |
| 112 | + |
| 113 | + line := int(entry.Marker.StartP.Line) |
| 114 | + col := int(entry.Marker.StartP.Column) |
| 115 | + |
| 116 | + // this should never happen as lines and cols are 1 indexed, but we'll add a check in case the vcontext library ever changes |
| 117 | + if line < 1 || line > len(lines) || col < 1 || col > len(lines[line-1]) { |
| 118 | + return entry.String() + "\n" |
| 119 | + } |
| 120 | + |
| 121 | + var buf strings.Builder |
| 122 | + kindColor := severityColor(entry.Kind) |
| 123 | + kindStr := color(entry.Kind.String(), kindColor, colorize) |
| 124 | + |
| 125 | + path := "" |
| 126 | + if entry.Context.Len() > 0 { |
| 127 | + path = "[" + entry.Context.String() + "]" |
| 128 | + } |
| 129 | + fmt.Fprintf(&buf, "%s%s:\n", kindStr, path) |
| 130 | + // Add information about the location of the error in the following form: |
| 131 | + // |
| 132 | + // " --> testing.bu:10:4" |
| 133 | + arrow := color("-->", blue, colorize) |
| 134 | + fmt.Fprintf(&buf, " %s %s:%d:%d\n", arrow, filename, line, col) |
| 135 | + |
| 136 | + // Number of lines to show before and after the error location |
| 137 | + contextLines := 2 |
| 138 | + contextLineInit := max(1, line-contextLines) |
| 139 | + contextLineEnd := min(len(lines), line+contextLines) |
| 140 | + |
| 141 | + // width of the largest line number |
| 142 | + gutterWidth := len(fmt.Sprintf("%d", contextLineEnd)) |
| 143 | + fmt.Fprintf(&buf, " %s |\n", strings.Repeat(" ", gutterWidth)) |
| 144 | + for lineNumber := contextLineInit; lineNumber <= contextLineEnd; lineNumber++ { |
| 145 | + lineNum := color(fmt.Sprintf("%*d", gutterWidth, lineNumber), blue, colorize) |
| 146 | + |
| 147 | + fmt.Fprintf(&buf, " %s | %s\n", lineNum, lines[lineNumber-1]) |
| 148 | + // Underline the error and write the error message |
| 149 | + if lineNumber == line { |
| 150 | + writeUnderline(&buf, col, gutterWidth, entry.Message, lines[lineNumber-1], colorize) |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + // Empty line at the end |
| 155 | + fmt.Fprintf(&buf, " %s |\n", strings.Repeat(" ", gutterWidth)) |
| 156 | + return buf.String() |
| 157 | +} |
0 commit comments