From bd6a828d6a01561d37e0e81abed80303a6989277 Mon Sep 17 00:00:00 2001 From: srinandan <13950006+srinandan@users.noreply.github.com> Date: Tue, 19 Sep 2023 16:29:43 -0700 Subject: [PATCH] bug: fixes bundle errors with relative paths #294 --- cmd/apis/bundlecrtapis.go | 2 +- cmd/apis/cloneapi.go | 2 +- cmd/env/crtarchive.go | 2 +- cmd/sharedflows/bundlecrtsf.go | 2 +- internal/bundlegen/proxybundle/proxybundle.go | 89 ++++++++++++++++--- 5 files changed, 83 insertions(+), 14 deletions(-) diff --git a/cmd/apis/bundlecrtapis.go b/cmd/apis/bundlecrtapis.go index 523ec5bb7..14e4a42af 100644 --- a/cmd/apis/bundlecrtapis.go +++ b/cmd/apis/bundlecrtapis.go @@ -65,7 +65,7 @@ var BundleCreateCmd = &cobra.Command{ proxyBundlePath := path.Join(tmpDir, name+".zip") - if err = proxybundle.GenerateArchiveBundle(proxyFolder, proxyBundlePath); err != nil { + if err = proxybundle.GenerateArchiveBundle(proxyFolder, proxyBundlePath, false); err != nil { return err } if _, err = apis.CreateProxy(name, proxyBundlePath); err != nil { diff --git a/cmd/apis/cloneapi.go b/cmd/apis/cloneapi.go index 0a5ac7ee6..158c43d0c 100644 --- a/cmd/apis/cloneapi.go +++ b/cmd/apis/cloneapi.go @@ -81,7 +81,7 @@ var CloneCmd = &cobra.Command{ proxyBundlePath := path.Join(tmpDir, name+".zip") - if err = proxybundle.GenerateArchiveBundle(path.Join(tmpDir, "apiproxy"), proxyBundlePath); err != nil { + if err = proxybundle.GenerateArchiveBundle(path.Join(tmpDir, "apiproxy"), proxyBundlePath, false); err != nil { return err } if _, err = apis.CreateProxy(name, proxyBundlePath); err != nil { diff --git a/cmd/env/crtarchive.go b/cmd/env/crtarchive.go index 92e30a6fd..afe9627b6 100644 --- a/cmd/env/crtarchive.go +++ b/cmd/env/crtarchive.go @@ -65,7 +65,7 @@ var CreateArchiveCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { if folder != "" { zipfile = name + ".zip" - if err = proxybundle.GenerateArchiveBundle(folder, zipfile); err != nil { + if err = proxybundle.GenerateArchive(folder, zipfile); err != nil { return err } } diff --git a/cmd/sharedflows/bundlecrtsf.go b/cmd/sharedflows/bundlecrtsf.go index 021b8b0b5..e606ac313 100644 --- a/cmd/sharedflows/bundlecrtsf.go +++ b/cmd/sharedflows/bundlecrtsf.go @@ -67,7 +67,7 @@ var BundleCreateCmd = &cobra.Command{ sfBundlePath := path.Join(tmpDir, name+".zip") - if err = proxybundle.GenerateArchiveBundle(sfFolder, sfBundlePath); err != nil { + if err = proxybundle.GenerateArchiveBundle(sfFolder, sfBundlePath, true); err != nil { return err } if _, err = sharedflows.Create(name, sfBundlePath); err != nil { diff --git a/internal/bundlegen/proxybundle/proxybundle.go b/internal/bundlegen/proxybundle/proxybundle.go index 9f1c87536..f524e7769 100644 --- a/internal/bundlegen/proxybundle/proxybundle.go +++ b/internal/bundlegen/proxybundle/proxybundle.go @@ -204,7 +204,7 @@ func GenerateAPIProxyBundleFromOAS(name string, } } - if err = archiveBundle(proxyRootDir, name+".zip"); err != nil { + if err = archiveBundle(proxyRootDir, name+".zip", false); err != nil { return err } @@ -322,7 +322,7 @@ func GenerateAPIProxyBundleFromGQL(name string, } } - if err = archiveBundle(proxyRootDir, name+".zip"); err != nil { + if err = archiveBundle(proxyRootDir, name+".zip", false); err != nil { return err } @@ -390,7 +390,7 @@ func GenerateIntegrationAPIProxyBundle(name string, integration string, apitrigg return err } - if err = archiveBundle(proxyRootDir, name+".zip"); err != nil { + if err = archiveBundle(proxyRootDir, name+".zip", false); err != nil { return err } @@ -543,7 +543,7 @@ func GenerateAPIProxyBundleFromSwagger(name string, } } - if err = archiveBundle(proxyRootDir, name+".zip"); err != nil { + if err = archiveBundle(proxyRootDir, name+".zip", false); err != nil { return err } @@ -566,11 +566,7 @@ func writeXMLData(fileName string, data string) error { return nil } -func GenerateArchiveBundle(pathToZip, destinationPath string) error { - return archiveBundle(pathToZip, destinationPath) -} - -func archiveBundle(pathToZip, destinationPath string) (err error) { +func GenerateArchive(pathToZip, destinationPath string) (err error) { var destinationFile *os.File pathSep := `/` // For archives/zip the path separator is always / @@ -622,6 +618,79 @@ func archiveBundle(pathToZip, destinationPath string) (err error) { return nil } +func GenerateArchiveBundle(pathToZip, destinationPath string, sharedflow bool) error { + return archiveBundle(pathToZip, destinationPath, sharedflow) +} + +func archiveBundle(pathToZip, destinationPath string, sharedflow bool) (err error) { + var destinationFile *os.File + var zipEntry string + parentFolder := true + + clilog.Debug.Printf("Compressing folder %s to zipfile %s\n", pathToZip, destinationPath) + + pathSep := `/` // For archives/zip the path separator is always / + + if destinationFile, err = os.Create(destinationPath); err != nil { + return err + } + + myZip := zip.NewWriter(destinationFile) + err = filepath.Walk(pathToZip, func(filePath string, info os.FileInfo, err error) error { + if info.IsDir() { + // for the first time, add the rootDir as the zipEntry + if parentFolder { + parentFolder = false + if sharedflow { + zipEntry = sfRootDir + pathSep + } + zipEntry = proxyRootDir + pathSep + } else { + relPath := filepath.ToSlash(strings.TrimPrefix(filePath, filepath.Dir(pathToZip))) + zipEntry = strings.TrimPrefix(relPath, pathSep) + pathSep + } + clilog.Debug.Printf("Found directory %s\n", filePath) + clilog.Debug.Printf("Creating directory zipEntry %s\n", zipEntry) + _, err = myZip.Create(zipEntry) + return err + } + clilog.Debug.Printf("Found file %s\n", filePath) + if err != nil { + return err + } + if strings.HasSuffix(filePath, "~") { + return nil + } + relPath := filepath.ToSlash(strings.TrimPrefix(filePath, filepath.Dir(pathToZip))) + zipEntry = strings.TrimPrefix(relPath, pathSep) + clilog.Debug.Printf("Creating file zipEntry %s\n", zipEntry) + zipFile, err := myZip.Create(zipEntry) + if err != nil { + return err + } + fsFile, err := os.Open(filePath) + if err != nil { + return err + } + _, err = io.Copy(zipFile, fsFile) + if err != nil { + return err + } + return nil + }) + if err != nil { + _ = destinationFile.Close() + return err + } + if err = myZip.Close(); err != nil { + return err + } + if err = destinationFile.Close(); err != nil { + return err + } + return nil +} + func GitHubImportBundle(owner string, repo string, repopath string, sharedflow bool) (err error) { var rootDir string @@ -670,7 +739,7 @@ func GitHubImportBundle(owner string, repo string, repopath string, sharedflow b // 2. compress the proxy folder curDir, _ := os.Getwd() - if err := archiveBundle(path.Join(curDir, rootDir), path.Join(curDir, rootDir+".zip")); err != nil { + if err := archiveBundle(path.Join(curDir, rootDir), path.Join(curDir, rootDir+".zip"), sharedflow); err != nil { return err }