@@ -24,12 +24,8 @@ type modulesFile struct {
24
24
Modules []* module `json:"Modules"`
25
25
}
26
26
27
- func getModulesDirectory (workdir string )string {
28
- return filepath .Join (workdir ,".terraform" ,"modules" )
29
- }
30
-
31
27
func getModulesFilePath (workdir string )string {
32
- return filepath .Join (getModulesDirectory ( workdir ) ,"modules.json" )
28
+ return filepath .Join (workdir , ".terraform" , "modules" ,"modules.json" )
33
29
}
34
30
35
31
func parseModulesFile (filePath string ) ([]* proto.Module ,error ) {
@@ -72,51 +68,59 @@ func getModules(workdir string) ([]*proto.Module, error) {
72
68
}
73
69
74
70
func getModulesArchive (workdir string ) ([]byte ,error ) {
75
- modulesDir := getModulesDirectory (workdir )
76
- if _ ,err := os .ReadDir (modulesDir );err != nil {
71
+ modulesFile := getModulesFilePath (workdir )
72
+ modulesFileBytes ,err := os .ReadFile (modulesFile )
73
+ if err != nil {
77
74
if os .IsNotExist (err ) {
78
75
return []byte {},nil
79
76
}
80
-
81
- return nil ,err
77
+ return nil ,xerrors .Errorf ("failed to read modules.json: %w" ,err )
82
78
}
79
+ var modules []* proto.Module
80
+ err = json .Unmarshal (modulesFileBytes ,& modules )
81
+ if err != nil {
82
+ return nil ,xerrors .Errorf ("failed to parse modules.json: %w" ,err )
83
+ }
84
+
83
85
empty := true
84
86
var b bytes.Buffer
85
87
w := tar .NewWriter (& b )
86
- err := filepath .WalkDir (modulesDir ,func (filePath string ,info fs.DirEntry ,err error )error {
87
- if err != nil {
88
- return xerrors .Errorf ("failed to create modules archive: %w" ,err )
89
- }
90
- if info .IsDir () {
91
- return nil
92
- }
93
- archivePath ,found := strings .CutPrefix (filePath ,workdir + string (os .PathSeparator ))
94
- if ! found {
95
- return xerrors .Errorf ("walked invalid file path: %q" ,filePath )
96
- }
88
+ for _ ,module := range modules {
89
+ err := filepath .WalkDir (module .Dir ,func (filePath string ,info fs.DirEntry ,err error )error {
90
+ if err != nil {
91
+ return xerrors .Errorf ("failed to create modules archive: %w" ,err )
92
+ }
93
+ if info .IsDir () {
94
+ return nil
95
+ }
96
+ archivePath ,found := strings .CutPrefix (filePath ,workdir + string (os .PathSeparator ))
97
+ if ! found {
98
+ return xerrors .Errorf ("walked invalid file path: %q" ,filePath )
99
+ }
97
100
98
- content ,err := os .ReadFile (filePath )
99
- if err != nil {
100
- return xerrors .Errorf ("failed to read module file while archiving: %w" ,err )
101
- }
102
- empty = false
103
- err = w .WriteHeader (& tar.Header {
104
- Name :archivePath ,
105
- Size :int64 (len (content )),
106
- Mode :0o644 ,
107
- Uid :1000 ,
108
- Gid :1000 ,
101
+ content ,err := os .ReadFile (filePath )
102
+ if err != nil {
103
+ return xerrors .Errorf ("failed to read module file while archiving: %w" ,err )
104
+ }
105
+ empty = false
106
+ err = w .WriteHeader (& tar.Header {
107
+ Name :archivePath ,
108
+ Size :int64 (len (content )),
109
+ Mode :0o644 ,
110
+ Uid :1000 ,
111
+ Gid :1000 ,
112
+ })
113
+ if err != nil {
114
+ return xerrors .Errorf ("failed to add module file to archive: %w" ,err )
115
+ }
116
+ if _ ,err = w .Write (content );err != nil {
117
+ return xerrors .Errorf ("failed to write module file to archive: %w" ,err )
118
+ }
119
+ return nil
109
120
})
110
121
if err != nil {
111
- return xerrors . Errorf ( "failed to add module file to archive: %w" ,err )
122
+ return nil ,err
112
123
}
113
- if _ ,err = w .Write (content );err != nil {
114
- return xerrors .Errorf ("failed to write module file to archive: %w" ,err )
115
- }
116
- return nil
117
- })
118
- if err != nil {
119
- return nil ,err
120
124
}
121
125
err = w .Close ()
122
126
if err != nil {