github.com/pulumi/terraform@v1.4.0/pkg/earlyconfig/diagnostics.go (about)

     1  package earlyconfig
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/terraform-config-inspect/tfconfig"
     7  	"github.com/pulumi/terraform/pkg/tfdiags"
     8  )
     9  
    10  func wrapDiagnostics(diags tfconfig.Diagnostics) tfdiags.Diagnostics {
    11  	ret := make(tfdiags.Diagnostics, len(diags))
    12  	for i, diag := range diags {
    13  		ret[i] = wrapDiagnostic(diag)
    14  	}
    15  	return ret
    16  }
    17  
    18  func wrapDiagnostic(diag tfconfig.Diagnostic) tfdiags.Diagnostic {
    19  	return wrappedDiagnostic{
    20  		d: diag,
    21  	}
    22  }
    23  
    24  type wrappedDiagnostic struct {
    25  	d tfconfig.Diagnostic
    26  }
    27  
    28  func (d wrappedDiagnostic) Severity() tfdiags.Severity {
    29  	switch d.d.Severity {
    30  	case tfconfig.DiagError:
    31  		return tfdiags.Error
    32  	case tfconfig.DiagWarning:
    33  		return tfdiags.Warning
    34  	default:
    35  		// Should never happen since there are no other severities
    36  		return 0
    37  	}
    38  }
    39  
    40  func (d wrappedDiagnostic) Description() tfdiags.Description {
    41  	// Since the inspect library doesn't produce precise source locations,
    42  	// we include the position information as part of the error message text.
    43  	// See the comment inside method "Source" for more information.
    44  	switch {
    45  	case d.d.Pos == nil:
    46  		return tfdiags.Description{
    47  			Summary: d.d.Summary,
    48  			Detail:  d.d.Detail,
    49  		}
    50  	case d.d.Detail != "":
    51  		return tfdiags.Description{
    52  			Summary: d.d.Summary,
    53  			Detail:  fmt.Sprintf("On %s line %d: %s", d.d.Pos.Filename, d.d.Pos.Line, d.d.Detail),
    54  		}
    55  	default:
    56  		return tfdiags.Description{
    57  			Summary: fmt.Sprintf("%s (on %s line %d)", d.d.Summary, d.d.Pos.Filename, d.d.Pos.Line),
    58  		}
    59  	}
    60  }
    61  
    62  func (d wrappedDiagnostic) Source() tfdiags.Source {
    63  	// Since the inspect library is constrained by the lowest common denominator
    64  	// between legacy HCL and modern HCL, it only returns ranges at whole-line
    65  	// granularity, and that isn't sufficient to populate a tfdiags.Source
    66  	// and so we'll just omit ranges altogether and include the line number in
    67  	// the Description text.
    68  	//
    69  	// Callers that want to return nicer errors should consider reacting to
    70  	// earlyconfig errors by attempting a follow-up parse with the normal
    71  	// config loader, which can produce more precise source location
    72  	// information.
    73  	return tfdiags.Source{}
    74  }
    75  
    76  func (d wrappedDiagnostic) FromExpr() *tfdiags.FromExpr {
    77  	return nil
    78  }
    79  
    80  func (d wrappedDiagnostic) ExtraInfo() interface{} {
    81  	return nil
    82  }