@@ -17,33 +17,14 @@ type ModuleContent struct {
1717 BaseUri string
1818}
1919
20- // Helper function to handle zip file extraction with proper path resolution
21- func handleZipFile (root string , location string , hash string , path string ) ([]byte , error ) {
22- // Resolve the path to the zip file if it's a local path
23- zipPath := location
24- if ! strings .HasPrefix (zipPath , "http://" ) && ! strings .HasPrefix (zipPath , "https://" ) {
25- // If it's a relative path, resolve it relative to the template's directory
26- if ! filepath .IsAbs (zipPath ) {
27- zipPath = filepath .Join (root , zipPath )
28- }
29- }
3020
31- // Check if the zip file exists if it's a local file
32- if ! strings .HasPrefix (zipPath , "http://" ) && ! strings .HasPrefix (zipPath , "https://" ) {
33- _ , err := os .Stat (zipPath )
34- if err != nil {
35- return nil , fmt .Errorf ("error accessing zip file %s: %v" , zipPath , err )
36- }
37- }
3821
39- // Unzip, verify hash if there is one, and put the files in memory
40- content , err := DownloadFromZip (zipPath , hash , path )
41- if err != nil {
42- config .Debugf ("ZIP: Error extracting from zip: %v" , err )
43- return nil , err
44- }
22+ func isHttpsUrl (uri string ) bool {
23+ return strings .HasPrefix (uri , "https://" )
24+ }
4525
46- return content , nil
26+ func isS3URI (uri string ) bool {
27+ return strings .HasPrefix (uri , "s3://" )
4728}
4829
4930// Get the module's content from a local file, memory, or a remote uri
@@ -54,6 +35,8 @@ func getModuleContent(
5435 baseUri string ,
5536 uri string ) (* ModuleContent , error ) {
5637
38+ config .Debugf ("getModuleContent root: %s, uri: %s" , root , uri )
39+
5740 var content []byte
5841 var err error
5942 var newRootDir string
@@ -71,7 +54,15 @@ func getModuleContent(
7154
7255 if strings .HasSuffix (packageAlias .Location , ".zip" ) {
7356 isZip = true
74- content , err = handleZipFile (root , packageAlias .Location , packageAlias .Hash , path )
57+
58+ // Use DownloadFromZip directly
59+ zipLocation := packageAlias .Location
60+ // For local files, resolve the path relative to the template's directory
61+ if ! isS3URI (zipLocation ) && ! isHttpsUrl (zipLocation ) && ! filepath .IsAbs (zipLocation ) {
62+ zipLocation = filepath .Join (root , zipLocation )
63+ }
64+
65+ content , err = DownloadFromZip (zipLocation , packageAlias .Hash , path )
7566 if err != nil {
7667 return nil , err
7768 }
@@ -92,12 +83,25 @@ func getModuleContent(
9283 // getModuleContent: root=cft/pkg/tmpl/awscli-modules, baseUri=, uri=package.zip/zip-module.yaml
9384 if strings .Contains (uri , ".zip/" ) {
9485 isZip = true
95- tokens := strings .Split (uri , "/" )
96- location := tokens [0 ]
97- path := strings .Join (tokens [1 :], "/" )
98- content , err = handleZipFile (root , location , "" , path )
99- if err != nil {
100- return nil , err
86+
87+ // Extract the zip location and path within the zip
88+ zipIndex := strings .Index (uri , ".zip/" )
89+ if zipIndex > 0 {
90+ zipLocation := uri [:zipIndex + 4 ] // Include the .zip part
91+ zipPath := uri [zipIndex + 5 :] // Skip the .zip/ part
92+
93+ // For local files, resolve the path relative to the template's directory
94+ if ! isS3URI (zipLocation ) && ! isHttpsUrl (zipLocation ) && ! filepath .IsAbs (zipLocation ) {
95+ zipLocation = filepath .Join (root , zipLocation )
96+ }
97+
98+ config .Debugf ("Extracting from zip: %s, path: %s" , zipLocation , zipPath )
99+
100+ // Use DownloadFromZip directly - it can handle S3, HTTPS, and local files
101+ content , err = DownloadFromZip (zipLocation , "" , zipPath )
102+ if err != nil {
103+ return nil , err
104+ }
101105 }
102106 }
103107
@@ -109,7 +113,15 @@ func getModuleContent(
109113 path := strings .Replace (uri , packageAlias .Alias + "/" , "" , 1 )
110114 if strings .HasSuffix (packageAlias .Location , ".zip" ) {
111115 isZip = true
112- content , err = handleZipFile (root , packageAlias .Location , packageAlias .Hash , path )
116+
117+ // Use DownloadFromZip directly
118+ zipLocation := packageAlias .Location
119+ // For local files, resolve the path relative to the template's directory
120+ if ! isS3URI (zipLocation ) && ! isHttpsUrl (zipLocation ) && ! filepath .IsAbs (zipLocation ) {
121+ zipLocation = filepath .Join (root , zipLocation )
122+ }
123+
124+ content , err = DownloadFromZip (zipLocation , packageAlias .Hash , path )
113125 if err != nil {
114126 return nil , err
115127 }
@@ -122,7 +134,7 @@ func getModuleContent(
122134 // Is this a local file or a URL or did we already unzip a package?
123135 if isZip {
124136 config .Debugf ("Using content from a zipped module package (length: %d bytes)" , len (content ))
125- } else if strings . HasPrefix (uri , "https://" ) {
137+ } else if isHttpsUrl (uri ) || isS3URI ( uri ) {
126138 config .Debugf ("Downloading from URL: %s" , uri )
127139 content , err = downloadModule (uri )
128140 if err != nil {
@@ -138,6 +150,7 @@ func getModuleContent(
138150 baseUri = strings .Join (urlParts [:len (urlParts )- 1 ], "/" )
139151
140152 } else {
153+ config .Debugf ("Downloading from a local file, baseUri=%s, uri=%s" , baseUri , uri )
141154 if baseUri != "" {
142155 // If we have a base URL, prepend it to the relative path
143156 uri = baseUri + "/" + uri
0 commit comments