|
1 | 1 | package chartsconfig |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
4 | 6 | "errors" |
| 7 | + "fmt" |
5 | 8 | "html/template" |
6 | 9 | "io/ioutil" |
7 | 10 | "os" |
8 | 11 | "path/filepath" |
| 12 | + "strconv" |
9 | 13 | "strings" |
10 | 14 |
|
11 | 15 | "github.com/Masterminds/semver" |
@@ -53,7 +57,7 @@ func NewChartsConfiguration(input []byte, tpath string) (*ChartsConfiguration, e |
53 | 57 | out := []byte(tpls["kubecrt/charts.yml"]) |
54 | 58 |
|
55 | 59 | if err = yaml.Unmarshal(out, m); err != nil { |
56 | | - return nil, err |
| 60 | + return nil, wrapError(out, err) |
57 | 61 | } |
58 | 62 |
|
59 | 63 | for _, a := range m.ChartsMap { |
@@ -164,3 +168,65 @@ func loadTemplates(b []byte, partialPath string) ([]*hchart.Template, error) { |
164 | 168 |
|
165 | 169 | return tpls, err |
166 | 170 | } |
| 171 | + |
| 172 | +func wrapError(b []byte, err error) error { |
| 173 | + var lines []string |
| 174 | + shortLines := []string{"\n"} |
| 175 | + |
| 176 | + el, str := errorLine(err) |
| 177 | + |
| 178 | + scanner := bufio.NewScanner(bytes.NewReader(b)) |
| 179 | + l := 0 |
| 180 | + for scanner.Scan() { |
| 181 | + l++ |
| 182 | + } |
| 183 | + ll := len(strconv.Itoa(l)) |
| 184 | + |
| 185 | + i := 1 |
| 186 | + scanner = bufio.NewScanner(bytes.NewReader(b)) |
| 187 | + for scanner.Scan() { |
| 188 | + line := fmt.Sprintf("%*d: %s", ll, i, scanner.Text()) |
| 189 | + |
| 190 | + // if we know the error line, we create an extra summary of the context |
| 191 | + // surrounding the error itself, starting 3 lines before, ending 3 after. |
| 192 | + if el != 0 { |
| 193 | + if i == el { |
| 194 | + line = "\x1b[31;1m" + line + "\x1b[0m" |
| 195 | + } |
| 196 | + |
| 197 | + if (i >= el-3) && (i <= el+3) { |
| 198 | + shortLines = append(shortLines, line) |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + lines = append(lines, line) |
| 203 | + i++ |
| 204 | + } |
| 205 | + |
| 206 | + lines = append(lines, shortLines...) |
| 207 | + lines = append(lines, "\n"+str) |
| 208 | + |
| 209 | + return errors.New(strings.Join(lines, "\n")) |
| 210 | +} |
| 211 | + |
| 212 | +func errorLine(err error) (int, string) { |
| 213 | + var i int |
| 214 | + var p []string |
| 215 | + str := err.Error() |
| 216 | + |
| 217 | + println(str) |
| 218 | + |
| 219 | + if strings.HasPrefix(str, "yaml: ") { |
| 220 | + p = strings.SplitN(str, ":", 3) |
| 221 | + i, _ = strconv.Atoi(strings.Replace(p[1], " line ", "", -1)) |
| 222 | + str = strings.TrimSpace(p[2]) |
| 223 | + } |
| 224 | + |
| 225 | + if strings.HasPrefix(str, "template: test:") { |
| 226 | + p = strings.SplitN(str, ":", 4) |
| 227 | + i, _ = strconv.Atoi(p[2]) |
| 228 | + str = strings.TrimSpace(p[3]) |
| 229 | + } |
| 230 | + |
| 231 | + return i, "Templating error: " + str |
| 232 | +} |
0 commit comments