github.com/hashicorp/packer@v1.14.3/hcl2template/version_required.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package hcl2template
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/hashicorp/go-version"
    10  	"github.com/hashicorp/hcl/v2"
    11  )
    12  
    13  // CheckCoreVersionRequirements visits each of the block in the given
    14  // configuration and verifies that any given Core version constraints match
    15  // with the version of Packer Core that is being used.
    16  //
    17  // The returned diagnostics will contain errors if any constraints do not match.
    18  // The returned diagnostics might also return warnings, which should be
    19  // displayed to the user.
    20  func (cfg *PackerConfig) CheckCoreVersionRequirements(coreVersion *version.Version) hcl.Diagnostics {
    21  	if cfg == nil {
    22  		return nil
    23  	}
    24  
    25  	var diags hcl.Diagnostics
    26  
    27  	for _, constraint := range cfg.Packer.VersionConstraints {
    28  		if !constraint.Required.Check(coreVersion) {
    29  			diags = diags.Append(&hcl.Diagnostic{
    30  				Severity: hcl.DiagError,
    31  				Summary:  "Unsupported Packer Core version",
    32  				Detail: fmt.Sprintf(
    33  					"This configuration does not support Packer version %s. To proceed, either choose another supported Packer version or update this version constraint. Version constraints are normally set for good reason, so updating the constraint may lead to other errors or unexpected behavior.",
    34  					coreVersion.String(),
    35  				),
    36  				Subject: constraint.DeclRange.Ptr(),
    37  			})
    38  		}
    39  	}
    40  
    41  	return diags
    42  }