|
| 1 | +/* |
| 2 | +
|
| 3 | +Copyright © 2025 NAME HERE <EMAIL ADDRESS> |
| 4 | +
|
| 5 | +*/ |
| 6 | + |
| 7 | +package cmd |
| 8 | + |
| 9 | +import ( |
| 10 | + "fmt" |
| 11 | + "io" |
| 12 | + "io/fs" |
| 13 | + "os" |
| 14 | + "path/filepath" |
| 15 | + "strings" |
| 16 | + "text/template" |
| 17 | + |
| 18 | + "github.com/spf13/cobra" |
| 19 | + "github.com/upsaurav12/bootstrap/templates" |
| 20 | +) |
| 21 | + |
| 22 | +// newCmd represents the new command |
| 23 | +var newCmd = &cobra.Command{ |
| 24 | + Use: "new", |
| 25 | + Short: "command for creating a new project.", |
| 26 | + Long: `command for creating a new project.`, |
| 27 | + Run: func(cmd *cobra.Command, args []string) { |
| 28 | + // Check if the project name is provided |
| 29 | + if len(args) < 1 { |
| 30 | + fmt.Fprintln(cmd.OutOrStdout(), "Error: project name is required") |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + // Get the template flag value from the command context |
| 35 | + tmpl, _ := cmd.Flags().GetString("type") |
| 36 | + |
| 37 | + // Get the project name (first argument) |
| 38 | + dirName := args[0] |
| 39 | + |
| 40 | + // Create the new project |
| 41 | + createNewProject(dirName, projectRouter, tmpl, cmd.OutOrStdout()) |
| 42 | + }, |
| 43 | +} |
| 44 | + |
| 45 | +var projectType string |
| 46 | +var projectPort string |
| 47 | +var projectRouter string |
| 48 | + |
| 49 | +func init() { |
| 50 | + // Add the new command to the rootCmd |
| 51 | + rootCmd.AddCommand(newCmd) |
| 52 | + |
| 53 | + // Define the --template flag for this command |
| 54 | + newCmd.Flags().StringVar(&projectType, "type", "", "type of the project") |
| 55 | + newCmd.Flags().StringVar(&projectPort, "port", "", "port of the project") |
| 56 | + newCmd.Flags().StringVar(&projectRouter, "router", "", "router of the project") |
| 57 | +} |
| 58 | + |
| 59 | +func createNewProject(projectName string, projectRouter string, template string, out io.Writer) { |
| 60 | + err := os.Mkdir(projectName, 0755) |
| 61 | + if err != nil { |
| 62 | + fmt.Fprintf(out, "Error creating directory: %v\n", err) |
| 63 | + return |
| 64 | + } |
| 65 | + // Print the template that was passed |
| 66 | + |
| 67 | + // Always add README + Makefile from common |
| 68 | + renderTemplateDir("common", projectName, TemplateData{ |
| 69 | + ModuleName: projectName, |
| 70 | + PortName: projectPort, |
| 71 | + }) |
| 72 | + |
| 73 | + renderTemplateDir("rest"+"/"+projectRouter, projectName, TemplateData{ |
| 74 | + ModuleName: projectName, |
| 75 | + PortName: projectPort, |
| 76 | + }) |
| 77 | + |
| 78 | + if err != nil { |
| 79 | + fmt.Fprintf(out, "Error rendering templates: %v\n", err) |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + fmt.Fprintf(out, "Created '%s' successfully\n", projectName) |
| 84 | +} |
| 85 | + |
| 86 | +type TemplateData struct { |
| 87 | + ModuleName string |
| 88 | + PortName string |
| 89 | +} |
| 90 | + |
| 91 | +func renderTemplateDir(templatePath, destinationPath string, data TemplateData) error { |
| 92 | + return fs.WalkDir(templates.FS, templatePath, func(path string, d fs.DirEntry, err error) error { |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + // Compute relative path (remove the base templatePath) |
| 98 | + relPath, _ := filepath.Rel(templatePath, path) |
| 99 | + targetPath := filepath.Join(destinationPath, strings.TrimSuffix(relPath, ".tmpl")) |
| 100 | + |
| 101 | + if d.IsDir() { |
| 102 | + return os.MkdirAll(targetPath, 0755) |
| 103 | + } |
| 104 | + |
| 105 | + // ✅ Important: use full `path` for ReadFile |
| 106 | + content, err := templates.FS.ReadFile(path) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + |
| 111 | + // Parse template |
| 112 | + tmpl, err := template.New(filepath.Base(path)).Parse(string(content)) |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + // Write file |
| 118 | + outFile, err := os.Create(targetPath) |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + defer outFile.Close() |
| 123 | + |
| 124 | + return tmpl.Execute(outFile, data) |
| 125 | + }) |
| 126 | +} |
0 commit comments