htmgo/cli/tasks/downloadtemplate/main.go

94 lines
2.1 KiB
Go
Raw Normal View History

package downloadtemplate
2024-09-13 20:42:42 +00:00
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 DownloadTemplate(outPath string) {
2024-09-13 21:11:37 +00:00
cwd, _ := os.Getwd()
2024-09-13 21:24:56 +00:00
flag.Parse()
outPath = strings.ReplaceAll(outPath, "\n", "")
outPath = strings.ReplaceAll(outPath, " ", "-")
outPath = strings.ToLower(outPath)
2024-09-13 21:24:56 +00:00
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"
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
}
deleteAllExceptTemplate(outPath, excludeDir)
2024-09-13 20:42:42 +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-14 01:22:26 +00:00
{"git", "init"},
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-14 01:20:52 +00:00
fmt.Println("Template downloaded successfully.")
fmt.Println("To start the development server, run the following commands:")
fmt.Printf("cd %s && htmgo run\n", outPath)
2024-09-14 01:20:52 +00:00
fmt.Printf("To build the project, run the following command:\n")
fmt.Printf("cd %s && htmgo build\n", outPath)
2024-09-13 20:42:42 +00:00
}