-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
65 lines (55 loc) · 1.69 KB
/
main.go
File metadata and controls
65 lines (55 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"embed"
"fmt"
"os"
"os/exec"
"path/filepath"
"text/template"
)
//go:embed templates/*
var templateFS embed.FS
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: create-go-app <project-name>")
return
}
appName := os.Args[1]
projectPath := filepath.Join(".", appName)
dirs := []string{
"src", "utils", "logger",
}
for _, dir := range dirs {
os.MkdirAll(filepath.Join(projectPath, dir), 0755)
}
files := map[string]string{
"templates/main.go.tmpl": "src/main.go",
"templates/helper.go.tmpl": "utils/helper.go",
"templates/logger.go.tmpl": "logger/logger.go",
"templates/.gitignore.tmpl": ".gitignore",
"templates/.env.tmpl": ".env",
"templates/.env.example.tmpl": ".env.example",
"templates/README.md.tmpl": "README.md",
}
for tmplFile, outFile := range files {
tmplBytes, _ := templateFS.ReadFile(tmplFile)
t := template.Must(template.New("").Parse(string(tmplBytes)))
outPath := filepath.Join(projectPath, outFile)
outF, _ := os.Create(outPath)
t.Execute(outF, map[string]string{"AppName": appName})
outF.Close()
}
// Initialize go.mod
cmd := exec.Command("go", "mod", "init", appName)
cmd.Dir = projectPath
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
// Initialize git
gitInit := exec.Command("git", "init")
gitInit.Dir = projectPath
gitInit.Stdout = os.Stdout
gitInit.Stderr = os.Stderr
gitInit.Run()
fmt.Println("Project", appName, "created and initialized with git successfully!")
}