| 1 |
// Copyright (c) HashiCorp, Inc. |
| 2 |
// SPDX-License-Identifier: MPL-2.0 |
| 3 |
|
| 4 |
package tfconfig |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
|
| 9 |
legacyhclparser "github.com/hashicorp/hcl/hcl/parser" |
| 10 |
"github.com/hashicorp/hcl/v2" |
| 11 |
) |
| 12 |
|
| 13 |
// Diagnostic describes a problem (error or warning) encountered during |
| 14 |
// configuration loading. |
| 15 |
type Diagnostic struct { |
| 16 |
Severity DiagSeverity `json:"severity"` |
| 17 |
Summary string `json:"summary"` |
| 18 |
Detail string `json:"detail,omitempty"` |
| 19 |
|
| 20 |
// Pos is not populated for all diagnostics, but when populated should |
| 21 |
// indicate a particular line that the described problem relates to. |
| 22 |
Pos *SourcePos `json:"pos,omitempty"` |
| 23 |
} |
| 24 |
|
| 25 |
// Diagnostics represents a sequence of diagnostics. This is the type that |
| 26 |
// should be returned from a function that might generate diagnostics. |
| 27 |
type Diagnostics []Diagnostic |
| 28 |
|
| 29 |
// HasErrors returns true if there is at least one Diagnostic of severity |
| 30 |
// DiagError in the receiever. |
| 31 |
// |
| 32 |
// If a function returns a Diagnostics without errors then the result can |
| 33 |
// be assumed to be complete within the "best effort" constraints of this |
| 34 |
// library. If errors are present then the caller may wish to employ more |
| 35 |
// caution in relying on the result. |
| 36 |
func (diags Diagnostics) HasErrors() bool { |
| 37 |
for _, diag := range diags { |
| 38 |
if diag.Severity == DiagError { |
| 39 |
return true |
| 40 |
} |
| 41 |
} |
| 42 |
return false |
| 43 |
} |
| 44 |
|
| 45 |
func (diags Diagnostics) Error() string { |
| 46 |
switch len(diags) { |
| 47 |
case 0: |
| 48 |
return "no problems" |
| 49 |
case 1: |
| 50 |
return fmt.Sprintf("%s: %s", diags[0].Summary, diags[0].Detail) |
| 51 |
default: |
| 52 |
return fmt.Sprintf("%s: %s (and %d other messages)", diags[0].Summary, diags[0].Detail, len(diags)-1) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
// Err returns an error representing the receiver if the receiver HasErrors, or |
| 57 |
// nil otherwise. |
| 58 |
// |
| 59 |
// The returned error can be type-asserted back to a Diagnostics if needed. |
| 60 |
func (diags Diagnostics) Err() error { |
| 61 |
if diags.HasErrors() { |
| 62 |
return diags |
| 63 |
} |
| 64 |
return nil |
| 65 |
} |
| 66 |
|
| 67 |
// DiagSeverity describes the severity of a Diagnostic. |
| 68 |
type DiagSeverity rune |
| 69 |
|
| 70 |
// DiagError indicates a problem that prevented proper processing of the |
| 71 |
// configuration. In the precense of DiagError diagnostics the result is |
| 72 |
// likely to be incomplete. |
| 73 |
const DiagError DiagSeverity = 'E' |
| 74 |
|
| 75 |
// DiagWarning indicates a problem that the user may wish to consider but |
| 76 |
// that did not prevent proper processing of the configuration. |
| 77 |
const DiagWarning DiagSeverity = 'W' |
| 78 |
|
| 79 |
// MarshalJSON is an implementation of encoding/json.Marshaler |
| 80 |
func (s DiagSeverity) MarshalJSON() ([]byte, error) { |
| 81 |
switch s { |
| 82 |
case DiagError: |
| 83 |
return []byte(`"error"`), nil |
| 84 |
case DiagWarning: |
| 85 |
return []byte(`"warning"`), nil |
| 86 |
default: |
| 87 |
return []byte(`"invalid"`), nil |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
func diagnosticsHCL(diags hcl.Diagnostics) Diagnostics { |
| 92 |
if len(diags) == 0 { |
| 93 |
return nil |
| 94 |
} |
| 95 |
ret := make(Diagnostics, len(diags)) |
| 96 |
for i, diag := range diags { |
| 97 |
ret[i] = Diagnostic{ |
| 98 |
Summary: diag.Summary, |
| 99 |
Detail: diag.Detail, |
| 100 |
} |
| 101 |
switch diag.Severity { |
| 102 |
case hcl.DiagError: |
| 103 |
ret[i].Severity = DiagError |
| 104 |
case hcl.DiagWarning: |
| 105 |
ret[i].Severity = DiagWarning |
| 106 |
} |
| 107 |
if diag.Subject != nil { |
| 108 |
pos := sourcePosHCL(*diag.Subject) |
| 109 |
ret[i].Pos = &pos |
| 110 |
} |
| 111 |
} |
| 112 |
return ret |
| 113 |
} |
| 114 |
|
| 115 |
func diagnosticsError(err error) Diagnostics { |
| 116 |
if err == nil { |
| 117 |
return nil |
| 118 |
} |
| 119 |
|
| 120 |
if posErr, ok := err.(*legacyhclparser.PosError); ok { |
| 121 |
pos := sourcePosLegacyHCL(posErr.Pos, "") |
| 122 |
return Diagnostics{ |
| 123 |
Diagnostic{ |
| 124 |
Severity: DiagError, |
| 125 |
Summary: posErr.Err.Error(), |
| 126 |
Pos: &pos, |
| 127 |
}, |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
return Diagnostics{ |
| 132 |
Diagnostic{ |
| 133 |
Severity: DiagError, |
| 134 |
Summary: err.Error(), |
| 135 |
}, |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
func diagnosticsErrorf(format string, args ...interface{}) Diagnostics { |
| 140 |
return diagnosticsError(fmt.Errorf(format, args...)) |
| 141 |
} |