这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 54 additions & 11 deletions cmd/apis/cloneapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ import (
"strings"

"internal/apiclient"
"internal/client/apis"

proxybundle "internal/bundlegen/proxybundle"

"internal/client/apis"

"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -179,14 +185,14 @@ func setParam(filePath string, paramType string) (err error) {

stringValue := string(byteValue)
replaceName := fmt.Sprintf("<APIProxy revision=\"1\" name=\"%s\">", name)
replaceBasePath := fmt.Sprintf("<BasePaths>%s</BasePaths>", basePath)
replaceBasePath := fmt.Sprintf("<Basepaths>%s</Basepaths>", basePath)

switch paramType {
case "proxy":
re := regexp.MustCompile(`\<APIProxy revision=\"\d+\" name=\"\S+\">`)
stringValue = re.ReplaceAllString(stringValue, replaceName)
case "basePath":
re := regexp.MustCompile(`\<Basepaths>\w+\<\\Basepaths>`)
re := regexp.MustCompile(`\<Basepaths>\/\w+\<\/Basepaths>`)
stringValue = re.ReplaceAllString(stringValue, replaceBasePath)
}

Expand All @@ -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("<BasePath>%s</BasePath>", 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(`\<BasePath>\/\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 {
Expand Down