@@ -17,33 +17,21 @@ 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- }
20+ func isHttpsUrl (uri string ) bool {
21+ return strings .HasPrefix (uri , "https://" )
22+ }
3023
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- }
24+ func isS3URI (uri string ) bool {
25+ return strings .HasPrefix (uri , "s3://" )
26+ }
3827
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
28+ // resolveZipLocation ensures that local zip paths are resolved relative to the template's directory
29+ func resolveZipLocation ( root string , zipLocation string ) string {
30+ // For local files, resolve the path relative to the template's directory
31+ if ! isS3URI ( zipLocation ) && ! isHttpsUrl ( zipLocation ) && ! filepath . IsAbs ( zipLocation ) {
32+ return filepath . Join ( root , zipLocation )
4433 }
45-
46- return content , nil
34+ return zipLocation
4735}
4836
4937// Get the module's content from a local file, memory, or a remote uri
@@ -54,6 +42,8 @@ func getModuleContent(
5442 baseUri string ,
5543 uri string ) (* ModuleContent , error ) {
5644
45+ config .Debugf ("getModuleContent root: %s, uri: %s" , root , uri )
46+
5747 var content []byte
5848 var err error
5949 var newRootDir string
@@ -71,7 +61,8 @@ func getModuleContent(
7161
7262 if strings .HasSuffix (packageAlias .Location , ".zip" ) {
7363 isZip = true
74- content , err = handleZipFile (root , packageAlias .Location , packageAlias .Hash , path )
64+ zipLocation := resolveZipLocation (root , packageAlias .Location )
65+ content , err = DownloadFromZip (zipLocation , packageAlias .Hash , path )
7566 if err != nil {
7667 return nil , err
7768 }
@@ -92,12 +83,21 @@ 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+ zipLocation = resolveZipLocation (root , zipLocation )
94+ config .Debugf ("Extracting from zip: %s, path: %s" , zipLocation , zipPath )
95+
96+ // Use DownloadFromZip directly - it can handle S3, HTTPS, and local files
97+ content , err = DownloadFromZip (zipLocation , "" , zipPath )
98+ if err != nil {
99+ return nil , err
100+ }
101101 }
102102 }
103103
@@ -109,7 +109,8 @@ func getModuleContent(
109109 path := strings .Replace (uri , packageAlias .Alias + "/" , "" , 1 )
110110 if strings .HasSuffix (packageAlias .Location , ".zip" ) {
111111 isZip = true
112- content , err = handleZipFile (root , packageAlias .Location , packageAlias .Hash , path )
112+ zipLocation := resolveZipLocation (root , packageAlias .Location )
113+ content , err = DownloadFromZip (zipLocation , packageAlias .Hash , path )
113114 if err != nil {
114115 return nil , err
115116 }
@@ -122,7 +123,7 @@ func getModuleContent(
122123 // Is this a local file or a URL or did we already unzip a package?
123124 if isZip {
124125 config .Debugf ("Using content from a zipped module package (length: %d bytes)" , len (content ))
125- } else if strings . HasPrefix (uri , "https://" ) {
126+ } else if isHttpsUrl (uri ) || isS3URI ( uri ) {
126127 config .Debugf ("Downloading from URL: %s" , uri )
127128 content , err = downloadModule (uri )
128129 if err != nil {
@@ -138,6 +139,7 @@ func getModuleContent(
138139 baseUri = strings .Join (urlParts [:len (urlParts )- 1 ], "/" )
139140
140141 } else {
142+ config .Debugf ("Downloading from a local file, baseUri=%s, uri=%s" , baseUri , uri )
141143 if baseUri != "" {
142144 // If we have a base URL, prepend it to the relative path
143145 uri = baseUri + "/" + uri
0 commit comments