htmgo/framework/tooling/downloadtemplate/main.go

93 lines
1.9 KiB
Go
Raw Normal View History

2024-09-13 20:42:42 +00:00
package main
import (
2024-09-13 21:24:56 +00:00
"flag"
2024-09-13 20:42:42 +00:00
"fmt"
"os"
"os/exec"
"path/filepath"
2024-09-13 21:11:37 +00:00
"strings"
2024-09-13 20:42:42 +00:00
)
func deleteAllExceptTemplate(outPath string, excludeDir string) {
// List all files and directories in the root folder
files, err := os.ReadDir(outPath)
if err != nil {
fmt.Printf("Error reading directory: %v\n", err)
return
}
// Iterate through each item in the root folder
for _, file := range files {
// Skip the excluded directory
if file.Name() == excludeDir {
continue
}
// Get full path
fullPath := filepath.Join(outPath, file.Name())
err := os.RemoveAll(fullPath)
if err != nil {
fmt.Printf("Error removing %s: %v\n", fullPath, err)
}
}
}
func main() {
2024-09-13 21:11:37 +00:00
cwd, _ := os.Getwd()
2024-09-13 21:24:56 +00:00
outPath := flag.String("out", "", "Specify the output path for the new app")
flag.Parse()
*outPath = strings.ReplaceAll(*outPath, "\n", "")
*outPath = strings.ReplaceAll(*outPath, " ", "-")
*outPath = strings.ToLower(*outPath)
if *outPath == "" {
2024-09-13 20:56:03 +00:00
fmt.Println("Please provide a name for your app.")
return
}
2024-09-13 20:42:42 +00:00
excludeDir := "starter-template"
2024-09-14 00:05:55 +00:00
install := exec.Command("git", "clone", "https://github.com/maddalax/htmgo", "--depth=1", *outPath)
2024-09-13 20:42:42 +00:00
install.Stdout = os.Stdout
install.Stderr = os.Stderr
err := install.Run()
if err != nil {
println("Error downloading template %v\n", err)
return
}
2024-09-13 21:24:56 +00:00
deleteAllExceptTemplate(*outPath, excludeDir)
2024-09-13 20:42:42 +00:00
2024-09-13 21:24:56 +00:00
newDir := filepath.Join(cwd, *outPath)
2024-09-13 20:56:03 +00:00
2024-09-13 21:13:37 +00:00
mvCmd := exec.Command("cp", "-vaR", fmt.Sprintf("%s/.", excludeDir), ".")
2024-09-13 21:11:37 +00:00
mvCmd.Dir = newDir
mvCmd.Stdout = os.DevNull
mvCmd.Stderr = os.DevNull
2024-09-13 21:11:37 +00:00
err = mvCmd.Run()
2024-09-13 20:56:03 +00:00
if err != nil {
println("Error moving files %v\n", err)
return
}
2024-09-13 21:11:37 +00:00
rmCmd := exec.Command("rm", "-rf", "starter-template")
rmCmd.Dir = newDir
mvCmd.Stdout = os.DevNull
mvCmd.Stderr = os.DevNull
2024-09-13 21:11:37 +00:00
err = rmCmd.Run()
2024-09-13 20:56:03 +00:00
if err != nil {
2024-09-13 21:11:37 +00:00
println("Error removing starter-template %v\n", err)
2024-09-13 20:56:03 +00:00
return
}
2024-09-13 20:42:42 +00:00
println("Template downloaded successfully!")
}