Skip to content

Commit

Permalink
fixed paths
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesWoolfenden committed Jul 26, 2022
1 parent 026eb29 commit b177c31
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
20 changes: 15 additions & 5 deletions src/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,32 @@ import (
"io/ioutil"
"log"
"strings"

"os"
"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast"
)

// GetResources retrieves all the resources in a tf file
func GetResources(file fs.FileInfo, dirname string) []Resource {
func GetResources(file fs.FileInfo, dirname string) ([]Resource, error ){

var results []Resource

src, err := ioutil.ReadFile(dirname + file.Name())
fullfile:=dirname + string(os.PathSeparator)+ file.Name()

src, err := ioutil.ReadFile(fullfile)

if err != nil {
log.Fatal(err)
}

myCode, _ := hcl.Parse(string(src))
myCode, err := hcl.Parse(string(src))

if err != nil {
log.Printf("failed to parse %s", fullfile )
log.Print(err)
return nil, err
}

Tree := myCode.Node.(*ast.ObjectList)

for _, item := range Tree.Items {
Expand All @@ -32,7 +42,7 @@ func GetResources(file fs.FileInfo, dirname string) []Resource {
}

// resources, filename, code
return results
return results, nil
}

// GetProvider retrieves the provider from the resource
Expand Down
20 changes: 18 additions & 2 deletions src/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,25 @@ package pike
import (
"io/fs"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)

// Scan looks for resources in a given directory
func Scan(dirname string) error {

files, err2 := GetTF(dirname)
fulldir, err:=filepath.Abs(dirname)
fulldir=strings.TrimSuffix(fulldir, string(os.PathSeparator))

if err != nil {
return err
}

log.Print(fulldir)

files, err2 := GetTF(fulldir)
if err2 != nil {
return err2
}
Expand All @@ -18,7 +30,11 @@ func Scan(dirname string) error {

for _, file := range files {

resources := GetResources(file, dirname)
resources, err := GetResources(file, fulldir)

if err != nil {
continue
}

for _, resource := range resources {
hcltype := GetHCLType(resource)
Expand Down
3 changes: 3 additions & 0 deletions todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ nat_gateway

handle
2022/07/24 07:00:37 data aws_caller_identity not found

parse/ignore variables hcl.parse the correct parser?
https://discuss.hashicorp.com/t/parse-hcl-treating-variables-or-functions-as-raw-strings-hashicorp-hcl/5859/2

0 comments on commit b177c31

Please sign in to comment.