From b7d947726344556d64d6b5799630217f07989f0d Mon Sep 17 00:00:00 2001 From: maddalax Date: Mon, 23 Sep 2024 11:56:44 -0500 Subject: [PATCH] fix templating download issue --- cli/htmgo/internal/dirutil/dir.go | 63 +----------------------- cli/htmgo/tasks/downloadtemplate/main.go | 14 ++++-- 2 files changed, 11 insertions(+), 66 deletions(-) diff --git a/cli/htmgo/internal/dirutil/dir.go b/cli/htmgo/internal/dirutil/dir.go index 30bd77b..7ebd838 100644 --- a/cli/htmgo/internal/dirutil/dir.go +++ b/cli/htmgo/internal/dirutil/dir.go @@ -8,7 +8,6 @@ import ( "log/slog" "os" "path/filepath" - "strings" ) func HasFileFromRoot(file string) bool { @@ -75,64 +74,6 @@ func CopyFile(src, dst string) error { return nil } -func DeleteAllExcept(path string, keepDir string) error { - // Get the info of the path - info, err := os.Stat(path) - if err != nil { - return err - } - - if !info.IsDir() { - // We only care about directories - return nil - } - - // If path is the keepDir, do nothing - if path == keepDir { - return nil - } - - // Check if keepDir is under path (path is an ancestor of keepDir) - relToKeepDir, err := filepath.Rel(path, keepDir) - if err != nil { - return err - } - - if !strings.HasPrefix(relToKeepDir, "..") { - // keepDir is under path; recurse into subdirectories - entries, err := os.ReadDir(path) - if err != nil { - return err - } - - for _, entry := range entries { - entryPath := filepath.Join(path, entry.Name()) - err := DeleteAllExcept(entryPath, keepDir) - if err != nil { - return err - } - } - } else { - // Check if path is under keepDir (path is a descendant of keepDir) - relFromKeepDir, err := filepath.Rel(keepDir, path) - if err != nil { - return err - } - - if !strings.HasPrefix(relFromKeepDir, "..") { - // path is under keepDir; do nothing - return nil - } else { - // path is neither keepDir nor related; delete it - slog.Debug("Deleting directory:", slog.String("path", path)) - err = os.RemoveAll(path) - if err != nil { - return err - } - // Skip further traversal since the directory is deleted - return nil - } - } - - return nil +func DeleteDir(dir string) error { + return os.RemoveAll(dir) } diff --git a/cli/htmgo/tasks/downloadtemplate/main.go b/cli/htmgo/tasks/downloadtemplate/main.go index 8239baa..b0fecec 100644 --- a/cli/htmgo/tasks/downloadtemplate/main.go +++ b/cli/htmgo/tasks/downloadtemplate/main.go @@ -13,6 +13,7 @@ import ( "path/filepath" "regexp" "strings" + "time" ) func DownloadTemplate(outPath string) { @@ -36,7 +37,9 @@ func DownloadTemplate(outPath string) { // Replace all non-alphabetic characters with an empty string newModuleName := re.ReplaceAllString(outPath, "") - install := exec.Command("git", "clone", "https://github.com/maddalax/htmgo", "--depth=1", outPath) + tempOut := newModuleName + "_temp_" + time.Now().String() + + install := exec.Command("git", "clone", "https://github.com/maddalax/htmgo", "--depth=1", tempOut) install.Stdout = os.Stdout install.Stderr = os.Stderr err := install.Run() @@ -46,16 +49,17 @@ func DownloadTemplate(outPath string) { return } - dirutil.DeleteAllExcept(outPath, templatePath) - newDir := filepath.Join(cwd, outPath) - dirutil.CopyDir(templatePath, newDir, func(path string, exists bool) bool { + dirutil.CopyDir(filepath.Join(tempOut, templatePath), newDir, func(path string, exists bool) bool { return true }) + dirutil.DeleteDir(tempOut) + + process.SetWorkingDir(newDir) + commands := [][]string{ - {"rm", "-rf", templatePath}, {"go", "get", "github.com/maddalax/htmgo/framework"}, {"go", "get", "github.com/maddalax/htmgo/framework-ui"}, {"git", "init"},