11/*
2+
23Copyright © 2025 NAME HERE <EMAIL ADDRESS>
4+
35*/
6+
47package cmd
58
69import (
@@ -19,8 +22,8 @@ import (
1922// newCmd represents the new command
2023var newCmd = & cobra.Command {
2124 Use : "new" ,
22- Short : "this command for creating a new project." ,
23- Long : `this command for creating a new project.` ,
25+ Short : "command for creating a new project." ,
26+ Long : `command for creating a new project.` ,
2427 Run : func (cmd * cobra.Command , args []string ) {
2528 // Check if the project name is provided
2629 if len (args ) < 1 {
@@ -35,7 +38,6 @@ var newCmd = &cobra.Command{
3538 dirName := args [0 ]
3639
3740 // Create the new project
38- // Renaming argument for clarity
3941 createNewProject (dirName , projectRouter , tmpl , cmd .OutOrStdout ())
4042 },
4143}
@@ -54,37 +56,27 @@ func init() {
5456 newCmd .Flags ().StringVar (& projectRouter , "router" , "" , "router of the project" )
5557}
5658
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
59+ func createNewProject (projectName string , projectRouter string , template string , out io.Writer ) {
6060 err := os .Mkdir (projectName , 0755 )
6161 if err != nil {
6262 fmt .Fprintf (out , "Error creating directory: %v\n " , err )
6363 return
6464 }
65+ // Print the template that was passed
6566
66- // Template Data structure for rendering
67- data := TemplateData {
67+ // Always add README + Makefile from common
68+ renderTemplateDir ( "common" , projectName , TemplateData {
6869 ModuleName : projectName ,
6970 PortName : projectPort ,
70- }
71-
72- fmt .Println ("data templateData: " , data .ModuleName , data .PortName )
71+ })
7372
74- // Always add README + Makefile from common
75- // FIX: Path corrected to "common" since templates.FS root is the templates/ directory content.
76- err = renderTemplateDir ("common" , projectName , data )
77- if err != nil {
78- fmt .Fprintf (out , "Error rendering common templates: %v\n " , err )
79- return
80- }
73+ renderTemplateDir ("rest" , projectName , TemplateData {
74+ ModuleName : projectName ,
75+ PortName : projectPort ,
76+ })
8177
82- // Render project specific templates (e.g., "rest/gin")
83- // FIX: Path corrected to remove redundant "templates/" prefix.
84- err = renderTemplateDir (tmpl + "/" + projectRouter , projectName , data )
85- // FIX: Correctly checking the error returned by the function call above.
8678 if err != nil {
87- fmt .Fprintf (out , "Error rendering project templates: %v\n " , err )
79+ fmt .Fprintf (out , "Error rendering templates: %v\n " , err )
8880 return
8981 }
9082
@@ -107,18 +99,16 @@ func renderTemplateDir(templatePath, destinationPath string, data TemplateData)
10799 targetPath := filepath .Join (destinationPath , strings .TrimSuffix (relPath , ".tmpl" ))
108100
109101 if d .IsDir () {
110- // This path is necessary to create all subdirectories correctly
111102 return os .MkdirAll (targetPath , 0755 )
112103 }
113104
114- // Read file content from the embedded filesystem
105+ // ✅ Important: use full `path` for ReadFile
115106 content , err := templates .FS .ReadFile (path )
116107 if err != nil {
117108 return err
118109 }
119110
120111 // Parse template
121- // Note: filepath.Base(path) uses the original path inside the embedded FS
122112 tmpl , err := template .New (filepath .Base (path )).Parse (string (content ))
123113 if err != nil {
124114 return err
0 commit comments