Skip to content

Commit fbad5e8

Browse files
committed
Refactor zip function
1 parent 5e92fca commit fbad5e8

File tree

1 file changed

+12
-23
lines changed

1 file changed

+12
-23
lines changed

cft/pkg/content.go

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ type ModuleContent struct {
1717
BaseUri string
1818
}
1919

20-
21-
2220
func isHttpsUrl(uri string) bool {
2321
return strings.HasPrefix(uri, "https://")
2422
}
@@ -27,6 +25,15 @@ func isS3URI(uri string) bool {
2725
return strings.HasPrefix(uri, "s3://")
2826
}
2927

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)
33+
}
34+
return zipLocation
35+
}
36+
3037
// Get the module's content from a local file, memory, or a remote uri
3138
func getModuleContent(
3239
root string,
@@ -54,14 +61,7 @@ func getModuleContent(
5461

5562
if strings.HasSuffix(packageAlias.Location, ".zip") {
5663
isZip = true
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-
64+
zipLocation := resolveZipLocation(root, packageAlias.Location)
6565
content, err = DownloadFromZip(zipLocation, packageAlias.Hash, path)
6666
if err != nil {
6767
return nil, err
@@ -90,11 +90,7 @@ func getModuleContent(
9090
zipLocation := uri[:zipIndex+4] // Include the .zip part
9191
zipPath := uri[zipIndex+5:] // Skip the .zip/ part
9292

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-
93+
zipLocation = resolveZipLocation(root, zipLocation)
9894
config.Debugf("Extracting from zip: %s, path: %s", zipLocation, zipPath)
9995

10096
// Use DownloadFromZip directly - it can handle S3, HTTPS, and local files
@@ -113,14 +109,7 @@ func getModuleContent(
113109
path := strings.Replace(uri, packageAlias.Alias+"/", "", 1)
114110
if strings.HasSuffix(packageAlias.Location, ".zip") {
115111
isZip = true
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-
112+
zipLocation := resolveZipLocation(root, packageAlias.Location)
124113
content, err = DownloadFromZip(zipLocation, packageAlias.Hash, path)
125114
if err != nil {
126115
return nil, err

0 commit comments

Comments
 (0)