htmgo/framework/tooling/downloadtemplate/main.go

93 lines
2 KiB
Go
Raw Normal View History

2024-09-13 20:42:42 +00:00
package main
import (
2024-09-13 21:11:37 +00:00
"bufio"
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 {
fmt.Printf("Skipping directory: %s\n", file.Name())
continue
}
// Get full path
fullPath := filepath.Join(outPath, file.Name())
// Remove the file or directory
fmt.Printf("Removing: %s\n", fullPath)
err := os.RemoveAll(fullPath)
if err != nil {
fmt.Printf("Error removing %s: %v\n", fullPath, err)
} else {
fmt.Printf("Successfully removed %s\n", fullPath)
}
}
}
func main() {
2024-09-13 21:11:37 +00:00
cwd, _ := os.Getwd()
2024-09-13 20:56:03 +00:00
var outPath string
2024-09-13 21:11:37 +00:00
reader := bufio.NewReader(os.Stdin)
fmt.Print("What should we call your new app? Enter name: ")
outPath, _ = reader.ReadString('\n')
outPath = strings.ReplaceAll(outPath, "\n", "")
outPath = strings.ReplaceAll(outPath, " ", "-")
outPath = strings.ToLower(outPath)
2024-09-13 20:56:03 +00:00
if outPath == "" {
fmt.Println("Please provide a name for your app.")
return
}
2024-09-13 20:42:42 +00:00
excludeDir := "starter-template"
install := exec.Command("git", "clone", "https://github.com/maddalax/mhtml", "--depth=1", outPath)
install.Stdout = os.Stdout
install.Stderr = os.Stderr
err := install.Run()
if err != nil {
println("Error downloading template %v\n", err)
return
}
deleteAllExceptTemplate(outPath, excludeDir)
2024-09-13 21:11:37 +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
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
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!")
}