FormatDiagnostics returns a nicely formatted string containing all of the error details within the tfconfig.Diagnostics. We need to use this because the default format doesn't provide much useful information.
(baseDir string, diags tfconfig.Diagnostics)
| 50 | // error details within the tfconfig.Diagnostics. We need to use this because |
| 51 | // the default format doesn't provide much useful information. |
| 52 | func formatDiagnostics(baseDir string, diags tfconfig.Diagnostics) string { |
| 53 | var msgs strings.Builder |
| 54 | for _, d := range diags { |
| 55 | // Convert severity. |
| 56 | severity := "UNKNOWN SEVERITY" |
| 57 | switch { |
| 58 | case d.Severity == tfconfig.DiagError: |
| 59 | severity = "ERROR" |
| 60 | case d.Severity == tfconfig.DiagWarning: |
| 61 | severity = "WARN" |
| 62 | } |
| 63 | |
| 64 | // Determine filepath and line |
| 65 | location := "unknown location" |
| 66 | if d.Pos != nil { |
| 67 | filename, err := filepath.Rel(baseDir, d.Pos.Filename) |
| 68 | if err != nil { |
| 69 | filename = d.Pos.Filename |
| 70 | } |
| 71 | location = fmt.Sprintf("%s:%d", filename, d.Pos.Line) |
| 72 | } |
| 73 | |
| 74 | _, _ = msgs.WriteString(fmt.Sprintf("\n%s: %s (%s)\n", severity, d.Summary, location)) |
| 75 | |
| 76 | // Wrap the details to 80 characters and indent them. |
| 77 | if d.Detail != "" { |
| 78 | wrapped := wordwrap.WrapString(d.Detail, 78) |
| 79 | for _, line := range strings.Split(wrapped, "\n") { |
| 80 | _, _ = msgs.WriteString(fmt.Sprintf("> %s\n", line)) |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | spacer := " " |
| 86 | if len(diags) > 1 { |
| 87 | spacer = "\n\n" |
| 88 | } |
| 89 | |
| 90 | return spacer + strings.TrimSpace(msgs.String()) |
| 91 | } |
no test coverage detected