Skip to content

Commit 5dd712d

Browse files
committed
fix: new commits
1 parent 59ef137 commit 5dd712d

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

cmd/new.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var newCmd = &cobra.Command{
3535
dirName := args[0]
3636

3737
// Create the new project
38+
// Renaming argument for clarity
3839
createNewProject(dirName, projectRouter, tmpl, cmd.OutOrStdout())
3940
},
4041
}
@@ -53,26 +54,35 @@ func init() {
5354
newCmd.Flags().StringVar(&projectRouter, "router", "", "router of the project")
5455
}
5556

56-
func createNewProject(projectName string, projectRouter string, template string, out io.Writer) {
57+
// Renamed 'template' to 'tmpl' for consistency with usage in newCmd.Run
58+
func createNewProject(projectName string, projectRouter string, tmpl string, out io.Writer) {
59+
// Create the project root directory
5760
err := os.Mkdir(projectName, 0755)
5861
if err != nil {
5962
fmt.Fprintf(out, "Error creating directory: %v\n", err)
6063
return
6164
}
62-
// Print the template that was passed
6365

64-
// Always add README + Makefile from common
65-
renderTemplateDir("templates/common", projectName, TemplateData{
66+
// Template Data structure for rendering
67+
data := TemplateData{
6668
ModuleName: projectName,
6769
PortName: projectPort,
68-
})
70+
}
6971

70-
renderTemplateDir("templates/"+template+"/"+projectRouter, projectName, TemplateData{
71-
ModuleName: projectName,
72-
PortName: projectPort,
73-
})
72+
// Always add README + Makefile from common
73+
// FIX: Path corrected to "common" since templates.FS root is the templates/ directory content.
74+
err = renderTemplateDir("common", projectName, data)
75+
if err != nil {
76+
fmt.Fprintf(out, "Error rendering common templates: %v\n", err)
77+
return
78+
}
79+
80+
// Render project specific templates (e.g., "rest/gin")
81+
// FIX: Path corrected to remove redundant "templates/" prefix.
82+
err = renderTemplateDir(tmpl+"/"+projectRouter, projectName, data)
83+
// FIX: Correctly checking the error returned by the function call above.
7484
if err != nil {
75-
fmt.Fprintf(out, "Error rendering templates: %v\n", err)
85+
fmt.Fprintf(out, "Error rendering project templates: %v\n", err)
7686
return
7787
}
7888

@@ -90,26 +100,29 @@ func renderTemplateDir(templatePath, destinationPath string, data TemplateData)
90100
return err
91101
}
92102

103+
// Compute relative path (remove the base templatePath)
93104
relPath, _ := filepath.Rel(templatePath, path)
94105
targetPath := filepath.Join(destinationPath, strings.TrimSuffix(relPath, ".tmpl"))
95106

96107
if d.IsDir() {
108+
// This path is necessary to create all subdirectories correctly
97109
return os.MkdirAll(targetPath, 0755)
98110
}
99111

100-
// Read file from embed.FS
112+
// Read file content from the embedded filesystem
101113
content, err := templates.FS.ReadFile(path)
102114
if err != nil {
103115
return err
104116
}
105117

106118
// Parse template
119+
// Note: filepath.Base(path) uses the original path inside the embedded FS
107120
tmpl, err := template.New(filepath.Base(path)).Parse(string(content))
108121
if err != nil {
109122
return err
110123
}
111124

112-
// Create destination file
125+
// Write file
113126
outFile, err := os.Create(targetPath)
114127
if err != nil {
115128
return err

templates/embed.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,11 @@ package templates
22

33
import "embed"
44

5-
//go:embed rest/**/*
5+
// FS contains the contents of the templates directory.
6+
// The //go:embed directive is crucial for embedding these files into the compiled binary.
7+
// Since this file is located in the 'templates' directory,
8+
// the paths 'common' and 'rest' correctly refer to the template folders.
9+
//
10+
//go:embed common
11+
//go:embed rest
612
var FS embed.FS

0 commit comments

Comments
 (0)