-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathterragrunt.go
43 lines (38 loc) · 1.41 KB
/
terragrunt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package param_parsing
import (
"fmt"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/hcl/v2/hclparse"
"github.com/warrensbox/terraform-switcher/lib"
"path/filepath"
)
const terraGruntFileName = "terragrunt.hcl"
type terragruntVersionConstraints struct {
TerraformVersionConstraint string `hcl:"terraform_version_constraint"`
}
func GetVersionFromTerragrunt(params Params) (Params, error) {
filePath := filepath.Join(params.ChDirPath, terraGruntFileName)
if lib.CheckFileExist(filePath) {
logger.Infof("Reading configuration from %q", filePath)
parser := hclparse.NewParser()
hclFile, diagnostics := parser.ParseHCLFile(filePath)
if diagnostics.HasErrors() {
return params, fmt.Errorf("unable to parse HCL file %q", filePath)
}
var versionFromTerragrunt terragruntVersionConstraints
diagnostics = gohcl.DecodeBody(hclFile.Body, nil, &versionFromTerragrunt)
if diagnostics.HasErrors() {
return params, fmt.Errorf("could not decode body of HCL file %q", filePath)
}
version, err := lib.GetSemver(versionFromTerragrunt.TerraformVersionConstraint, params.MirrorURL)
if err != nil {
return params, fmt.Errorf("no version found matching %q", versionFromTerragrunt.TerraformVersionConstraint)
}
params.Version = version
}
return params, nil
}
func terraGruntFileExists(params Params) bool {
filePath := filepath.Join(params.ChDirPath, terraGruntFileName)
return lib.CheckFileExist(filePath)
}