htmgo/framework/tooling/downloadtemplate/main.go

88 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-14 01:11:32 +00:00
commands := [][]string{
{"cp", "-vaR", fmt.Sprintf("%s/.", excludeDir), "."},
{"rm", "-rf", excludeDir},
{"go", "get", "github.com/maddalax/htmgo/framework"},
{"go", "get", "github.com/maddalax/htmgo/framework-ui"},
2024-09-13 20:56:03 +00:00
}
2024-09-14 01:11:32 +00:00
for _, command := range commands {
cmd := exec.Command(command[0], command[1:]...)
cmd.Dir = newDir
cmd.Stdout = nil
cmd.Stderr = nil
err = cmd.Run()
if err != nil {
println("Error executing command %s\n", err.Error())
return
}
2024-09-13 20:56:03 +00:00
}
2024-09-13 20:42:42 +00:00
}