这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/google/go-github/v47 v47.1.0
github.com/hashicorp/go-version v1.7.0
github.com/hashicorp/hc-install v0.9.2
github.com/hashicorp/hcl/v2 v2.23.0
github.com/hashicorp/hcl/v2 v2.23.1-0.20250203194505-ba0759438da2
github.com/hashicorp/terraform-exec v0.23.0
github.com/jameswoolfenden/identity v0.0.5
github.com/rs/zerolog v1.34.0
Expand Down Expand Up @@ -52,7 +52,9 @@ require (
github.com/cyphar/filepath-securejoin v0.4.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fatih/color v1.18.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.6.2 // indirect
github.com/go-logr/logr v1.4.3 // indirect
Expand Down
11 changes: 6 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ github.com/elazarl/goproxy v1.7.2 h1:Y2o6urb7Eule09PjlhQRGNsqRfPmYI3KKQLFpCAV3+o
github.com/elazarl/goproxy v1.7.2/go.mod h1:82vkLNir0ALaW14Rc399OTTjyNREgmdL2cVoIbS6XaE=
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gliderlabs/ssh v0.3.8 h1:a4YXD1V7xMF9g5nTkdfnja3Sxy1PVDCj1Zg4Wb8vY6c=
github.com/gliderlabs/ssh v0.3.8/go.mod h1:xYoytBv1sV0aL3CavoDuJIQNURXkkfPA/wxQ1pL1fAU=
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI=
Expand Down Expand Up @@ -130,8 +131,8 @@ github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKe
github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/hc-install v0.9.2 h1:v80EtNX4fCVHqzL9Lg/2xkp62bbvQMnvPQ0G+OmtO24=
github.com/hashicorp/hc-install v0.9.2/go.mod h1:XUqBQNnuT4RsxoxiM9ZaUk0NX8hi2h+Lb6/c0OZnC/I=
github.com/hashicorp/hcl/v2 v2.23.0 h1:Fphj1/gCylPxHutVSEOf2fBOh1VE4AuLV7+kbJf3qos=
github.com/hashicorp/hcl/v2 v2.23.0/go.mod h1:62ZYHrXgPoX8xBnzl8QzbWq4dyDsDtfCRgIq1rbJEvA=
github.com/hashicorp/hcl/v2 v2.23.1-0.20250203194505-ba0759438da2 h1:JP8y98OtHTujECs4s/HxlKc5yql/RlC99Dt1Iz4R+lM=
github.com/hashicorp/hcl/v2 v2.23.1-0.20250203194505-ba0759438da2/go.mod h1:k+HgkLpoWu9OS81sy4j1XKDXaWm/rLysG33v5ibdDnc=
github.com/hashicorp/terraform-exec v0.23.0 h1:MUiBM1s0CNlRFsCLJuM5wXZrzA3MnPYEsiXmzATMW/I=
github.com/hashicorp/terraform-exec v0.23.0/go.mod h1:mA+qnx1R8eePycfwKkCRk3Wy65mwInvlpAeOwmA7vlY=
github.com/hashicorp/terraform-json v0.25.0 h1:rmNqc/CIfcWawGiwXmRuiXJKEiJu1ntGoxseG1hLhoQ=
Expand Down
20 changes: 16 additions & 4 deletions src/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func GetResources(file string, dirName string) ([]ResourceV2, error) {
return Resources, err
}

moduleJson, err := GetModuleJson(filepath.Join(dirName, "/", ".terraform", "modules"))
if err != nil {
return Resources, err
}

for _, block := range temp.Blocks {
var resource ResourceV2
resource.TypeName = block.Type
Expand All @@ -50,7 +55,7 @@ func GetResources(file string, dirName string) ([]ResourceV2, error) {
}
case module:
{
LocalResources, err := GetLocalModules(block, dirName)
LocalResources, err := GetLocalModules(block, dirName, moduleJson)
if err == nil {
Resources = append(LocalResources, Resources...)
} else {
Expand Down Expand Up @@ -147,7 +152,7 @@ func GetResourceBlocks(file string) (*hclsyntax.Body, error) {
}

// GetLocalModules return resource from a path.
func GetLocalModules(block *hclsyntax.Block, dirName string) ([]ResourceV2, error) {
func GetLocalModules(block *hclsyntax.Block, dirName string, listModulesJson ModuleJson) ([]ResourceV2, error) {
var Resources []ResourceV2

modulePath := GetModulePath(block)
Expand All @@ -157,9 +162,12 @@ func GetLocalModules(block *hclsyntax.Block, dirName string) ([]ResourceV2, erro
return nil, &gitReferenceError{modulePath}
}

// Module is coming from HCP Terraform Cloud
if strings.HasPrefix(modulePath, "app.terraform.io") {
modulePath = ReturnLocalAddrFromSource(modulePath, listModulesJson)
}
// have the path to the module
modulePath = filepath.Join(dirName, "/", modulePath)

modulePath = filepath.Join(dirName, modulePath)
// now process these extras
ExtraFiles, err := GetTF(modulePath)
if err != nil {
Expand Down Expand Up @@ -273,3 +281,7 @@ func GetPermission(result ResourceV2) (Sorted, error) {

return myPermission, err
}

func GetModuleJson(dir string) (ModuleJson, error) {
return ReadModuleJsonForDir(dir)
}
3 changes: 2 additions & 1 deletion src/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func Test_getLocalModules(t *testing.T) {
notLocal, _ := filepath.Abs("../testdata/modules/examples/notlocal")
notBlock := getInitialBlock(notLocal + "/module.local.tf")

moduleJson := make(pike.ModuleJson)
tests := []struct {
name string
args args
Expand All @@ -132,7 +133,7 @@ func Test_getLocalModules(t *testing.T) {
for _, tt := range tests {
// t.Parallel()
t.Run(tt.name, func(t *testing.T) {
got, err := pike.GetLocalModules(tt.args.block, tt.args.dirName)
got, err := pike.GetLocalModules(tt.args.block, tt.args.dirName, moduleJson)
if (err != nil) != tt.wantErr {
t.Errorf("GetResources() error = %v, wantErr %v", err, tt.wantErr)

Expand Down
101 changes: 101 additions & 0 deletions src/modules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package pike

import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"

"github.com/hashicorp/go-version"
)

const (
ManifestSnapshotFilename = "modules.json"
)

// Record represents some metadata about an installed module, as part
// of a module JSON.
type Record struct {
Key string `json:"Key"`
SourceAddr string `json:"Source"`
Version *version.Version `json:"-"`
VersionStr string `json:"Version,omitempty"`
Dir string `json:"Dir"`
}

type ModuleJson map[string]Record

type modulesJson struct {
Records []Record `json:"Modules"`
}

type invalidVersionError struct {
err error
key string
version string
}

func (m *invalidVersionError) Error() string {
return fmt.Sprintf("invalid version %q for %s: %s", m.version, m.key, m.err)
}

func ReadModuleJson(r io.Reader) (ModuleJson, error) {
src, err := io.ReadAll(r)

if err != nil {
return nil, err
}

if len(src) == 0 {
return make(ModuleJson), nil
}

var read modulesJson
err = json.Unmarshal(src, &read)

if err != nil {
return nil, &unmarshallJSONError{err, ""}
}

newModuleJson := make(ModuleJson)
for _, record := range read.Records {
if record.VersionStr != "" {
record.Version, err = version.NewVersion(record.VersionStr)
if err != nil {
return nil, &invalidVersionError{err, record.Key, record.VersionStr}
}
}
// Ensure Windows is using the proper modules path format after
// reading the module's manifest Dir records
record.Dir = filepath.FromSlash(record.Dir)

if _, exists := newModuleJson[record.Key]; exists {
return nil, fmt.Errorf("snapshot file contains two records for path %s", record.Key)
}
newModuleJson[record.Key] = record
}
return newModuleJson, nil
}

func ReadModuleJsonForDir(dir string) (ModuleJson, error) {
fn := filepath.Join(dir, ManifestSnapshotFilename)
r, err := os.Open(fn)
if err != nil {
if os.IsNotExist(err) {
return make(ModuleJson), nil
}
return nil, err
}
defer r.Close()
return ReadModuleJson(r)
}

func ReturnLocalAddrFromSource(source string, listModules ModuleJson) string {
for _, module := range listModules {
if module.SourceAddr == source {
return module.Dir
}
}
return ""
}
Loading