Fixed: path join

This commit is contained in:
zhenorzz 2024-03-25 16:18:23 +08:00
parent f34a36aba8
commit faa2e7d608
5 changed files with 40 additions and 18 deletions

View File

@ -641,8 +641,8 @@ func (Deploy) Rebuild(gp *server.Goploy) server.Response {
for _, projectServer := range projectServers {
go func(projectServer model.ProjectServer) {
var dockerScript docker.Script
destDir := path.Join(project.SymlinkPath, project.LastPublishToken)
cmdEntity := cmd.New(projectServer.Server.OS)
destDir := cmd.Join(project.SymlinkPath, project.LastPublishToken)
scriptName := fmt.Sprintf("goploy-after-deploy-p%d-s%d.%s", project.ID, projectServer.ServerID, pkg.GetScriptExt(project.Script.AfterDeploy.Mode))
if project.Script.AfterDeploy.Content != "" {
@ -732,7 +732,7 @@ func (Deploy) Rebuild(gp *server.Goploy) server.Response {
}
} else {
afterDeployCommands := []string{cmdEntity.Symlink(destDir, project.Path), cmdEntity.ChangeDirTime(destDir)}
afterDeployScriptPath := path.Join(project.Path, scriptName)
afterDeployScriptPath := cmd.Join(project.Path, scriptName)
afterDeployCommands = append(afterDeployCommands, cmdEntity.Script(project.Script.AfterDeploy.Mode, afterDeployScriptPath))
afterDeployCommands = append(afterDeployCommands, cmdEntity.Remove(afterDeployScriptPath))

View File

@ -408,12 +408,12 @@ func (gsync *Gsync) serverStage() error {
var afterDeployCommands []string
cmdEntity := cmd.New(projectServer.Server.OS)
if len(project.SymlinkPath) != 0 {
destDir := path.Join(project.SymlinkPath, project.LastPublishToken)
destDir := cmd.Join(project.SymlinkPath, project.LastPublishToken)
afterDeployCommands = append(afterDeployCommands, cmdEntity.Symlink(destDir, project.Path))
}
if project.Script.AfterDeploy.Content != "" {
afterDeployScriptPath := path.Join(project.Path, scriptName)
afterDeployScriptPath := cmd.Join(project.Path, scriptName)
afterDeployCommands = append(afterDeployCommands, cmdEntity.Script(project.Script.AfterDeploy.Mode, afterDeployScriptPath))
afterDeployCommands = append(afterDeployCommands, cmdEntity.Remove(afterDeployScriptPath))
}

View File

@ -4,10 +4,13 @@
package cmd
import (
"strings"
)
type Cmd interface {
Symlink(src, target string) string
Remove(file string) string
Path(file string) string
ChangeDirTime(dir string) string
Script(mode, file string) string
}
@ -19,3 +22,31 @@ func New(os string) Cmd {
return LinuxCmd{}
}
}
func ExtractSeparator(path string) byte {
if strings.Contains(path, "\\") {
return '\\'
}
return '/'
}
func Join(elem ...string) string {
size := 0
for _, e := range elem {
size += len(e)
}
if size == 0 {
return ""
}
sep := ExtractSeparator(elem[0])
buf := make([]byte, 0, size+len(elem)-1)
for _, e := range elem {
if len(buf) > 0 || e != "" {
if len(buf) > 0 {
buf = append(buf, sep)
}
buf = append(buf, e...)
}
}
return string(buf)
}

View File

@ -23,10 +23,6 @@ func (c LinuxCmd) ChangeDirTime(dir string) string {
return fmt.Sprintf("touch -m %s", dir)
}
func (c LinuxCmd) Path(file string) string {
return file
}
func (LinuxCmd) Symlink(src, target string) string {
// use relative path to fix docker symlink
relativeSrc := strings.Replace(src, path.Dir(target), ".", 1)

View File

@ -6,7 +6,6 @@ package cmd
import (
"fmt"
"strings"
)
type WindowsCmd struct{}
@ -15,22 +14,18 @@ func (w WindowsCmd) Script(mode, file string) string {
if mode == "" || mode == "cmd" {
mode = "cmd /C"
}
return fmt.Sprintf("%s %s", mode, w.Path(file))
return fmt.Sprintf("%s %s", mode, file)
}
func (w WindowsCmd) ChangeDirTime(dir string) string {
tmpFile := w.Path(dir + "/goploy.tmp")
tmpFile := Join(dir, "goploy.tmp")
return fmt.Sprintf("type nul > %s && del %[1]s", tmpFile)
}
func (WindowsCmd) Path(file string) string {
return strings.ReplaceAll(file, "/", "\\")
}
func (w WindowsCmd) Symlink(src, target string) string {
return fmt.Sprintf("mklink /D %s %s", w.Path(target), w.Path(src))
return fmt.Sprintf("mklink /D %s %s", target, src)
}
func (w WindowsCmd) Remove(file string) string {
return fmt.Sprintf("del /Q %s", w.Path(file))
return fmt.Sprintf("del /Q %s", file)
}