diff --git a/cmd/apis/cloneapi.go b/cmd/apis/cloneapi.go index 6327e80c1..0a5ac7ee6 100644 --- a/cmd/apis/cloneapi.go +++ b/cmd/apis/cloneapi.go @@ -27,11 +27,10 @@ import ( "strings" "internal/apiclient" + "internal/client/apis" proxybundle "internal/bundlegen/proxybundle" - "internal/client/apis" - "github.com/spf13/cobra" ) @@ -67,26 +66,29 @@ var CloneCmd = &cobra.Command{ if proxyFolder != "" { var tmpDir string - if tmpDir, err = copyDirectory(); err != nil { - return err + if proxyZip != "" { + tmpDir = proxyFolder + } else { + if tmpDir, err = copyDirectory(); err != nil { + return err + } + defer os.RemoveAll(tmpDir) } - defer os.RemoveAll(tmpDir) - if err = renameProxy(tmpDir); err != nil { return err } proxyBundlePath := path.Join(tmpDir, name+".zip") - if err = proxybundle.GenerateArchiveBundle(proxyFolder, proxyBundlePath); err != nil { + if err = proxybundle.GenerateArchiveBundle(path.Join(tmpDir, "apiproxy"), proxyBundlePath); err != nil { return err } if _, err = apis.CreateProxy(name, proxyBundlePath); err != nil { return err } - return os.Remove(proxyBundlePath) + return err } return err @@ -159,7 +161,11 @@ func renameProxy(tmpDir string) (err error) { return nil }) - return err + if err != nil { + return err + } + + return setBasePath(path.Join(tmpDir, "apiproxy", "proxies", "default.xml")) } func setParam(filePath string, paramType string) (err error) { @@ -179,14 +185,14 @@ func setParam(filePath string, paramType string) (err error) { stringValue := string(byteValue) replaceName := fmt.Sprintf("", name) - replaceBasePath := fmt.Sprintf("%s", basePath) + replaceBasePath := fmt.Sprintf("%s", basePath) switch paramType { case "proxy": re := regexp.MustCompile(`\`) stringValue = re.ReplaceAllString(stringValue, replaceName) case "basePath": - re := regexp.MustCompile(`\\w+\<\\Basepaths>`) + re := regexp.MustCompile(`\\/\w+\<\/Basepaths>`) stringValue = re.ReplaceAllString(stringValue, replaceBasePath) } @@ -205,6 +211,43 @@ func setParam(filePath string, paramType string) (err error) { return nil } +func setBasePath(filePath string) (err error) { + var proxyFile *os.File + + replaceBasePath := fmt.Sprintf("%s", basePath) + + proxyFile, err = os.OpenFile(filePath, os.O_RDONLY, 0o644) + if err != nil { + return err + } + + byteValue, err := io.ReadAll(proxyFile) + if err != nil { + return err + } + + proxyFile.Close() + + stringValue := string(byteValue) + + re := regexp.MustCompile(`\\/\w+\<\/BasePath>`) + stringValue = re.ReplaceAllString(stringValue, replaceBasePath) + + proxyFile, err = os.OpenFile(filePath, os.O_WRONLY|os.O_TRUNC, 0o644) + if err != nil { + return err + } + + defer proxyFile.Close() + + _, err = proxyFile.Write([]byte(stringValue)) + if err != nil { + return err + } + + return nil +} + func unzipBundle() (tmpDir string, err error) { tmpDir, err = os.MkdirTemp("", "proxy") if err != nil {