-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9a6fd9
commit 0979eda
Showing
4 changed files
with
61 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package parser | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/hcl/v2/hclsyntax" | ||
"github.com/safedep/vet/pkg/models" | ||
"os" | ||
|
||
"github.com/hashicorp/hcl/v2/hclparse" | ||
) | ||
|
||
func parseTerraformLockfile(path string, config *ParserConfig) (*models.PackageManifest, error) { | ||
// Open the lockfile | ||
file, err := os.Open(path) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to open file: %s", err) | ||
} | ||
defer file.Close() | ||
|
||
// Parse the file using the HCL parser | ||
parser := hclparse.NewParser() | ||
hclFile, diags := parser.ParseHCLFile(path) | ||
if diags.HasErrors() { | ||
return nil, fmt.Errorf("failed to parse lockfile: %v", diags) | ||
} | ||
|
||
body, ok := hclFile.Body.(*hclsyntax.Body) | ||
if !ok { | ||
return nil, fmt.Errorf("failed to assert body as hclsyntax.Body") | ||
} | ||
manifest := models.NewPackageManifestFromLocal(path, models.EcosystemTerraform) | ||
|
||
for _, block := range body.Blocks { | ||
if block.Type != "provider" { | ||
continue | ||
} | ||
providerName := block.Labels[0] // The provider name is the first label | ||
providerVersion := "0.0.0" | ||
if versionAttr, exists := block.Body.Attributes["version"]; exists { | ||
versionVal, diags := versionAttr.Expr.Value(nil) | ||
if diags.HasErrors() { | ||
return nil, fmt.Errorf("failed to extract version: %v", diags) | ||
} | ||
providerVersion = versionVal.AsString() | ||
} | ||
pkgdetails := models.NewPackageDetail(models.EcosystemTerraform, providerName, providerVersion) | ||
packageModel := models.Package{ | ||
PackageDetails: pkgdetails, | ||
Depth: 0, | ||
} | ||
manifest.AddPackage(&packageModel) | ||
} | ||
|
||
return manifest, nil | ||
} |