2017-10-05 20:59:08 +02:00
|
|
|
package format
|
|
|
|
|
|
|
|
import (
|
2018-03-01 02:06:21 +01:00
|
|
|
"bufio"
|
2017-10-05 20:59:08 +02:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2018-10-18 02:50:33 +02:00
|
|
|
"sort"
|
2018-03-01 02:06:21 +01:00
|
|
|
"strings"
|
2017-10-05 20:59:08 +02:00
|
|
|
|
2021-05-17 21:07:38 +02:00
|
|
|
viewsjson "github.com/hashicorp/terraform/internal/command/views/json"
|
2021-05-17 19:11:06 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
2021-03-11 18:08:47 +01:00
|
|
|
|
2017-10-05 20:59:08 +02:00
|
|
|
"github.com/mitchellh/colorstring"
|
|
|
|
wordwrap "github.com/mitchellh/go-wordwrap"
|
|
|
|
)
|
|
|
|
|
2021-01-26 19:13:32 +01:00
|
|
|
var disabledColorize = &colorstring.Colorize{
|
|
|
|
Colors: colorstring.DefaultColors,
|
|
|
|
Disable: true,
|
|
|
|
}
|
|
|
|
|
2017-10-05 20:59:08 +02:00
|
|
|
// Diagnostic formats a single diagnostic message.
|
|
|
|
//
|
|
|
|
// The width argument specifies at what column the diagnostic messages will
|
|
|
|
// be wrapped. If set to zero, messages will not be wrapped by this function
|
|
|
|
// at all. Although the long-form text parts of the message are wrapped,
|
|
|
|
// not all aspects of the message are guaranteed to fit within the specified
|
|
|
|
// terminal width.
|
2018-03-01 02:06:21 +01:00
|
|
|
func Diagnostic(diag tfdiags.Diagnostic, sources map[string][]byte, color *colorstring.Colorize, width int) string {
|
2021-03-11 18:08:47 +01:00
|
|
|
return DiagnosticFromJSON(viewsjson.NewDiagnostic(diag, sources), color, width)
|
|
|
|
}
|
|
|
|
|
|
|
|
func DiagnosticFromJSON(diag *viewsjson.Diagnostic, color *colorstring.Colorize, width int) string {
|
2017-10-05 20:59:08 +02:00
|
|
|
if diag == nil {
|
|
|
|
// No good reason to pass a nil diagnostic in here...
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
2020-12-19 03:01:19 +01:00
|
|
|
// these leftRule* variables are markers for the beginning of the lines
|
|
|
|
// containing the diagnostic that are intended to help sighted users
|
2021-02-05 15:11:38 +01:00
|
|
|
// better understand the information hierarchy when diagnostics appear
|
2020-12-19 03:01:19 +01:00
|
|
|
// alongside other information or alongside other diagnostics.
|
|
|
|
//
|
|
|
|
// Without this, it seems (based on folks sharing incomplete messages when
|
|
|
|
// asking questions, or including extra content that's not part of the
|
|
|
|
// diagnostic) that some readers have trouble easily identifying which
|
|
|
|
// text belongs to the diagnostic and which does not.
|
|
|
|
var leftRuleLine, leftRuleStart, leftRuleEnd string
|
|
|
|
var leftRuleWidth int // in visual character cells
|
|
|
|
|
2021-03-11 18:08:47 +01:00
|
|
|
switch diag.Severity {
|
|
|
|
case viewsjson.DiagnosticSeverityError:
|
2020-12-19 03:01:19 +01:00
|
|
|
buf.WriteString(color.Color("[bold][red]Error: [reset]"))
|
|
|
|
leftRuleLine = color.Color("[red]│[reset] ")
|
|
|
|
leftRuleStart = color.Color("[red]╷[reset]")
|
|
|
|
leftRuleEnd = color.Color("[red]╵[reset]")
|
|
|
|
leftRuleWidth = 2
|
2021-03-11 18:08:47 +01:00
|
|
|
case viewsjson.DiagnosticSeverityWarning:
|
2020-12-19 03:01:19 +01:00
|
|
|
buf.WriteString(color.Color("[bold][yellow]Warning: [reset]"))
|
|
|
|
leftRuleLine = color.Color("[yellow]│[reset] ")
|
|
|
|
leftRuleStart = color.Color("[yellow]╷[reset]")
|
|
|
|
leftRuleEnd = color.Color("[yellow]╵[reset]")
|
|
|
|
leftRuleWidth = 2
|
2017-10-05 20:59:08 +02:00
|
|
|
default:
|
|
|
|
// Clear out any coloring that might be applied by Terraform's UI helper,
|
|
|
|
// so our result is not context-sensitive.
|
|
|
|
buf.WriteString(color.Color("\n[reset]"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't wrap the summary, since we expect it to be terse, and since
|
|
|
|
// this is where we put the text of a native Go error it may not always
|
|
|
|
// be pure text that lends itself well to word-wrapping.
|
2021-03-11 18:08:47 +01:00
|
|
|
fmt.Fprintf(&buf, color.Color("[bold]%s[reset]\n\n"), diag.Summary)
|
2018-03-01 02:06:21 +01:00
|
|
|
|
2021-03-11 18:08:47 +01:00
|
|
|
appendSourceSnippets(&buf, diag, color)
|
2017-10-05 20:59:08 +02:00
|
|
|
|
2021-03-11 18:08:47 +01:00
|
|
|
if diag.Detail != "" {
|
2020-12-19 03:01:19 +01:00
|
|
|
paraWidth := width - leftRuleWidth - 1 // leave room for the left rule
|
|
|
|
if paraWidth > 0 {
|
2021-03-11 18:08:47 +01:00
|
|
|
lines := strings.Split(diag.Detail, "\n")
|
2020-09-01 19:58:23 +02:00
|
|
|
for _, line := range lines {
|
|
|
|
if !strings.HasPrefix(line, " ") {
|
2020-12-19 03:01:19 +01:00
|
|
|
line = wordwrap.WrapString(line, uint(paraWidth))
|
2020-09-01 19:58:23 +02:00
|
|
|
}
|
|
|
|
fmt.Fprintf(&buf, "%s\n", line)
|
|
|
|
}
|
|
|
|
} else {
|
2021-03-11 18:08:47 +01:00
|
|
|
fmt.Fprintf(&buf, "%s\n", diag.Detail)
|
2017-10-05 20:59:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-19 03:01:19 +01:00
|
|
|
// Before we return, we'll finally add the left rule prefixes to each
|
|
|
|
// line so that the overall message is visually delimited from what's
|
|
|
|
// around it. We'll do that by scanning over what we already generated
|
|
|
|
// and adding the prefix for each line.
|
|
|
|
var ruleBuf strings.Builder
|
|
|
|
sc := bufio.NewScanner(&buf)
|
|
|
|
ruleBuf.WriteString(leftRuleStart)
|
|
|
|
ruleBuf.WriteByte('\n')
|
|
|
|
for sc.Scan() {
|
|
|
|
line := sc.Text()
|
|
|
|
prefix := leftRuleLine
|
|
|
|
if line == "" {
|
|
|
|
// Don't print the space after the line if there would be nothing
|
|
|
|
// after it anyway.
|
|
|
|
prefix = strings.TrimSpace(prefix)
|
|
|
|
}
|
|
|
|
ruleBuf.WriteString(prefix)
|
|
|
|
ruleBuf.WriteString(line)
|
|
|
|
ruleBuf.WriteByte('\n')
|
|
|
|
}
|
|
|
|
ruleBuf.WriteString(leftRuleEnd)
|
|
|
|
ruleBuf.WriteByte('\n')
|
|
|
|
|
|
|
|
return ruleBuf.String()
|
2017-10-05 20:59:08 +02:00
|
|
|
}
|
2018-03-01 02:06:21 +01:00
|
|
|
|
2021-01-26 19:13:32 +01:00
|
|
|
// DiagnosticPlain is an alternative to Diagnostic which minimises the use of
|
|
|
|
// virtual terminal formatting sequences.
|
|
|
|
//
|
|
|
|
// It is intended for use in automation and other contexts in which diagnostic
|
|
|
|
// messages are parsed from the Terraform output.
|
|
|
|
func DiagnosticPlain(diag tfdiags.Diagnostic, sources map[string][]byte, width int) string {
|
2021-03-11 18:08:47 +01:00
|
|
|
return DiagnosticPlainFromJSON(viewsjson.NewDiagnostic(diag, sources), width)
|
|
|
|
}
|
|
|
|
|
|
|
|
func DiagnosticPlainFromJSON(diag *viewsjson.Diagnostic, width int) string {
|
2021-01-26 19:13:32 +01:00
|
|
|
if diag == nil {
|
|
|
|
// No good reason to pass a nil diagnostic in here...
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
2021-03-11 18:08:47 +01:00
|
|
|
switch diag.Severity {
|
|
|
|
case viewsjson.DiagnosticSeverityError:
|
2021-01-26 19:13:32 +01:00
|
|
|
buf.WriteString("\nError: ")
|
2021-03-11 18:08:47 +01:00
|
|
|
case viewsjson.DiagnosticSeverityWarning:
|
2021-01-26 19:13:32 +01:00
|
|
|
buf.WriteString("\nWarning: ")
|
|
|
|
default:
|
|
|
|
buf.WriteString("\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't wrap the summary, since we expect it to be terse, and since
|
|
|
|
// this is where we put the text of a native Go error it may not always
|
|
|
|
// be pure text that lends itself well to word-wrapping.
|
2021-03-11 18:08:47 +01:00
|
|
|
fmt.Fprintf(&buf, "%s\n\n", diag.Summary)
|
2021-01-26 19:13:32 +01:00
|
|
|
|
2021-03-11 18:08:47 +01:00
|
|
|
appendSourceSnippets(&buf, diag, disabledColorize)
|
2021-01-26 19:13:32 +01:00
|
|
|
|
2021-03-11 18:08:47 +01:00
|
|
|
if diag.Detail != "" {
|
2021-01-26 19:13:32 +01:00
|
|
|
if width > 1 {
|
2021-03-11 18:08:47 +01:00
|
|
|
lines := strings.Split(diag.Detail, "\n")
|
2021-01-26 19:13:32 +01:00
|
|
|
for _, line := range lines {
|
|
|
|
if !strings.HasPrefix(line, " ") {
|
|
|
|
line = wordwrap.WrapString(line, uint(width-1))
|
|
|
|
}
|
|
|
|
fmt.Fprintf(&buf, "%s\n", line)
|
|
|
|
}
|
|
|
|
} else {
|
2021-03-11 18:08:47 +01:00
|
|
|
fmt.Fprintf(&buf, "%s\n", diag.Detail)
|
2021-01-26 19:13:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2019-12-10 20:06:06 +01:00
|
|
|
// DiagnosticWarningsCompact is an alternative to Diagnostic for when all of
|
|
|
|
// the given diagnostics are warnings and we want to show them compactly,
|
|
|
|
// with only two lines per warning and excluding all of the detail information.
|
|
|
|
//
|
|
|
|
// The caller may optionally pre-process the given diagnostics with
|
|
|
|
// ConsolidateWarnings, in which case this function will recognize consolidated
|
|
|
|
// messages and include an indication that they are consolidated.
|
|
|
|
//
|
|
|
|
// Do not pass non-warning diagnostics to this function, or the result will
|
|
|
|
// be nonsense.
|
|
|
|
func DiagnosticWarningsCompact(diags tfdiags.Diagnostics, color *colorstring.Colorize) string {
|
|
|
|
var b strings.Builder
|
|
|
|
b.WriteString(color.Color("[bold][yellow]Warnings:[reset]\n\n"))
|
|
|
|
for _, diag := range diags {
|
|
|
|
sources := tfdiags.WarningGroupSourceRanges(diag)
|
|
|
|
b.WriteString(fmt.Sprintf("- %s\n", diag.Description().Summary))
|
|
|
|
if len(sources) > 0 {
|
|
|
|
mainSource := sources[0]
|
|
|
|
if mainSource.Subject != nil {
|
|
|
|
if len(sources) > 1 {
|
|
|
|
b.WriteString(fmt.Sprintf(
|
|
|
|
" on %s line %d (and %d more)\n",
|
|
|
|
mainSource.Subject.Filename,
|
|
|
|
mainSource.Subject.Start.Line,
|
|
|
|
len(sources)-1,
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
b.WriteString(fmt.Sprintf(
|
|
|
|
" on %s line %d\n",
|
|
|
|
mainSource.Subject.Filename,
|
|
|
|
mainSource.Subject.Start.Line,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
} else if len(sources) > 1 {
|
|
|
|
b.WriteString(fmt.Sprintf(
|
|
|
|
" (%d occurences of this warning)\n",
|
|
|
|
len(sources),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2021-03-11 18:08:47 +01:00
|
|
|
func appendSourceSnippets(buf *bytes.Buffer, diag *viewsjson.Diagnostic, color *colorstring.Colorize) {
|
2021-04-02 23:10:31 +02:00
|
|
|
if diag.Address != "" {
|
2021-04-06 21:50:30 +02:00
|
|
|
fmt.Fprintf(buf, " with %s,\n", diag.Address)
|
2021-04-02 23:10:31 +02:00
|
|
|
}
|
|
|
|
|
2021-03-11 18:08:47 +01:00
|
|
|
if diag.Range == nil {
|
|
|
|
return
|
2020-09-04 19:18:30 +02:00
|
|
|
}
|
|
|
|
|
2021-03-11 18:08:47 +01:00
|
|
|
if diag.Snippet == nil {
|
2021-01-18 16:49:03 +01:00
|
|
|
// This should generally not happen, as long as sources are always
|
|
|
|
// loaded through the main loader. We may load things in other
|
|
|
|
// ways in weird cases, so we'll tolerate it at the expense of
|
|
|
|
// a not-so-helpful error message.
|
2021-03-11 18:08:47 +01:00
|
|
|
fmt.Fprintf(buf, " on %s line %d:\n (source code not available)\n", diag.Range.Filename, diag.Range.Start.Line)
|
2021-01-18 16:49:03 +01:00
|
|
|
} else {
|
2021-03-11 18:08:47 +01:00
|
|
|
snippet := diag.Snippet
|
|
|
|
code := snippet.Code
|
2021-01-18 16:49:03 +01:00
|
|
|
|
2021-03-11 18:08:47 +01:00
|
|
|
var contextStr string
|
|
|
|
if snippet.Context != nil {
|
|
|
|
contextStr = fmt.Sprintf(", in %s", *snippet.Context)
|
2021-01-18 16:49:03 +01:00
|
|
|
}
|
2021-03-11 18:08:47 +01:00
|
|
|
fmt.Fprintf(buf, " on %s line %d%s:\n", diag.Range.Filename, diag.Range.Start.Line, contextStr)
|
|
|
|
|
|
|
|
// Split the snippet and render the highlighted section with underlines
|
|
|
|
start := snippet.HighlightStartOffset
|
|
|
|
end := snippet.HighlightEndOffset
|
2021-05-04 14:31:51 +02:00
|
|
|
|
|
|
|
// Only buggy diagnostics can have an end range before the start, but
|
|
|
|
// we need to ensure we don't crash here if that happens.
|
|
|
|
if end < start {
|
|
|
|
end = start + 1
|
|
|
|
if end > len(code) {
|
|
|
|
end = len(code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 18:08:47 +01:00
|
|
|
before, highlight, after := code[0:start], code[start:end], code[end:]
|
|
|
|
code = fmt.Sprintf(color.Color("%s[underline]%s[reset]%s"), before, highlight, after)
|
|
|
|
|
|
|
|
// Split the snippet into lines and render one at a time
|
|
|
|
lines := strings.Split(code, "\n")
|
|
|
|
for i, line := range lines {
|
|
|
|
fmt.Fprintf(
|
|
|
|
buf, "%4d: %s\n",
|
|
|
|
snippet.StartLine+i,
|
|
|
|
line,
|
|
|
|
)
|
2021-01-18 16:49:03 +01:00
|
|
|
}
|
|
|
|
|
2021-03-11 18:08:47 +01:00
|
|
|
if len(snippet.Values) > 0 {
|
|
|
|
// The diagnostic may also have information about the dynamic
|
|
|
|
// values of relevant variables at the point of evaluation.
|
|
|
|
// This is particularly useful for expressions that get evaluated
|
|
|
|
// multiple times with different values, such as blocks using
|
|
|
|
// "count" and "for_each", or within "for" expressions.
|
|
|
|
values := make([]viewsjson.DiagnosticExpressionValue, len(snippet.Values))
|
|
|
|
copy(values, snippet.Values)
|
|
|
|
sort.Slice(values, func(i, j int) bool {
|
|
|
|
return values[i].Traversal < values[j].Traversal
|
|
|
|
})
|
|
|
|
|
|
|
|
fmt.Fprint(buf, color.Color(" [dark_gray]├────────────────[reset]\n"))
|
|
|
|
for _, value := range values {
|
|
|
|
fmt.Fprintf(buf, color.Color(" [dark_gray]│[reset] [bold]%s[reset] %s\n"), value.Traversal, value.Statement)
|
2021-01-18 16:49:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
}
|