fix templating download issue

This commit is contained in:
maddalax 2024-09-23 11:56:44 -05:00
parent 8a42ea0c80
commit b7d9477263
2 changed files with 11 additions and 66 deletions

View file

@ -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)
}

View file

@ -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"},