Skip to content

Commit 1ea0cb5

Browse files
committed
add: added templates features in the project
Signed-off-by: upsaurav12 <sauravup041103@gmail.com>
1 parent 005963c commit 1ea0cb5

File tree

8 files changed

+90
-50
lines changed

8 files changed

+90
-50
lines changed

bootstrap

18.7 KB
Binary file not shown.

bruh1/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module bruh1
2+
3+
go 1.20

bruh1/handler/user.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package handlers
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
func UserHandler(w http.ResponseWriter, r *http.Request) {
9+
fmt.Fprintln(w, "👤 Hello from the User Handler!")
10+
}

bruh1/main.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net/http"
6+
7+
"bruh1/handlers"
8+
)
9+
10+
func main() {
11+
http.HandleFunc("/user", handlers.UserHandler)
12+
13+
log.Println("🚀 Server started on http://localhost:8080")
14+
if err := http.ListenAndServe(":8080", nil); err != nil {
15+
log.Fatal(err)
16+
}
17+
}

cmd/new.go

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"os"
1010
"path/filepath"
11+
"strings"
1112
"text/template"
1213

1314
"github.com/spf13/cobra"
@@ -55,66 +56,45 @@ func createNewProject(projectName string, template string, out io.Writer) {
5556

5657
// Print the template that was passed
5758

58-
createGoTemplate(projectName, out)
59+
renderTemplateDir("templates/"+template, projectName, TemplateData{
60+
ModuleName: projectName,
61+
})
5962

6063
// Print success message
6164
fmt.Fprintf(out, "Created '%s' successfully\n", projectName)
6265
}
6366

64-
func createGoTemplate(projectName string, out io.Writer) {
65-
// Define the go.mod template content
66-
goModTemplate := `module {{.ModuleName}}
67-
68-
go 1.18
69-
`
70-
71-
// Define the main.go content
72-
mainGoFile := filepath.Join(projectName, "main.go")
73-
mainGoContent := `package main
74-
75-
import "fmt"
76-
77-
func main() {
78-
fmt.Println("Hello, world!")
67+
type TemplateData struct {
68+
ModuleName string
7969
}
80-
`
8170

82-
// Create go.mod file and apply the template
83-
goModFile := filepath.Join(projectName, "go.mod")
84-
tmpl, err := template.New("go.mod").Parse(goModTemplate)
85-
if err != nil {
86-
fmt.Fprintf(out, "Error creating go.mod template: %v\n", err)
87-
return
88-
}
71+
func renderTemplateDir(templatePath, destinationPath string, data TemplateData) error {
72+
return filepath.Walk(templatePath, func(path string, info os.FileInfo, err error) error {
73+
if err != nil {
74+
return err
75+
}
8976

90-
// Create a file to write the go.mod content
91-
goModFileContent := struct {
92-
ModuleName string
93-
}{
94-
ModuleName: projectName, // Use project name as module name
95-
}
77+
relPath, _ := filepath.Rel(templatePath, path)
78+
targetPath := filepath.Join(destinationPath, strings.TrimSuffix(relPath, ".tmpl"))
9679

97-
// Write go.mod file with parsed template
98-
goModFileHandle, err := os.Create(goModFile)
99-
if err != nil {
100-
fmt.Fprintf(out, "Error creating go.mod file: %v\n", err)
101-
return
102-
}
103-
defer goModFileHandle.Close()
80+
if info.IsDir() {
81+
return os.MkdirAll(targetPath, 0755)
82+
}
10483

105-
err = tmpl.Execute(goModFileHandle, goModFileContent)
106-
if err != nil {
107-
fmt.Fprintf(out, "Error executing go.mod template: %v\n", err)
108-
return
109-
}
84+
// Read and parse template
85+
tmpl, err := template.ParseFiles(path)
86+
if err != nil {
87+
return err
88+
}
11089

111-
// Create main.go file
112-
err = os.WriteFile(mainGoFile, []byte(mainGoContent), 0644)
113-
if err != nil {
114-
fmt.Fprintf(out, "Error creating main.go: %v\n", err)
115-
return
116-
}
90+
// Create output file
91+
outFile, err := os.Create(targetPath)
92+
if err != nil {
93+
return err
94+
}
95+
defer outFile.Close()
11796

118-
// Print the result
119-
fmt.Fprintf(out, "Go project created in '%s'\n", projectName)
97+
// Execute template
98+
return tmpl.Execute(outFile, data)
99+
})
120100
}

templates/rest/go.mod.tmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module {{.ModuleName}}
2+
3+
go 1.20
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package handlers
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
func UserHandler(w http.ResponseWriter, r *http.Request) {
9+
fmt.Fprintln(w, "👤 Hello from the User Handler!")
10+
}

templates/rest/main.go.tmpl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net/http"
6+
7+
"{{.ModuleName}}/handlers"
8+
)
9+
10+
func main() {
11+
http.HandleFunc("/user", handlers.UserHandler)
12+
13+
log.Println("🚀 Server started on http://localhost:8080")
14+
if err := http.ListenAndServe(":8080", nil); err != nil {
15+
log.Fatal(err)
16+
}
17+
}

0 commit comments

Comments
 (0)