Skip to content

Commit

Permalink
feat: remove unused statements & show error on generating module when…
Browse files Browse the repository at this point in the history
… required field is not provide
  • Loading branch information
mukezhz committed Mar 17, 2024
1 parent 539806c commit 30bd043
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
8 changes: 7 additions & 1 deletion cmd/create_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ var newProjectCmd = &cobra.Command{
Run: createProject,
}

func setupFlagsForNewProject(cmd *cobra.Command) {
cmd.Flags().StringP("mod", "m", "", "module name")
cmd.Flags().StringP("dir", "d", "", "target directory")
cmd.Flags().StringP("version", "v", "", "version support: Default: 1.20")
}

func createProject(cmd *cobra.Command, args []string) {
var projectName string
var projectModuleName string
Expand Down Expand Up @@ -79,7 +85,7 @@ func createProject(cmd *cobra.Command, args []string) {
return
}
if projectModuleName == "" {
color.Redln("Error: module name is required")
color.Redln("Error: golang module name is required")
return
}

Expand Down
34 changes: 27 additions & 7 deletions cmd/new_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,33 @@ import (

"github.com/gookit/color"
"github.com/mukezhz/geng/pkg/constant"
"github.com/mukezhz/geng/pkg/model"
"github.com/mukezhz/geng/pkg/terminal"
"github.com/mukezhz/geng/pkg/utility"
"github.com/spf13/cobra"
)

var newModuleCmd = &cobra.Command{
Use: "gen module [name]",
Use: "gen mod [name]",
Short: "Create a new domain",
Args: cobra.MaximumNArgs(2),
Run: createModule,
Long: `
Create a new module|service|middleware in the project.
Example:
geng gen mod [name]
geng gen srv [name]
geng gen mid [name]
Default:
geng gen -> geng gen mod
`,
Args: cobra.MaximumNArgs(2),
Run: generate,
}

func createModule(_ *cobra.Command, args []string) {
func generate(_ *cobra.Command, args []string) {
if len(args) == 0 {
args = append(args, "module")
}
projectModule, err := utility.GetModuleNameFromGoModFile()
if err != nil {
fmt.Println("Error finding Module name from go.mod:", err)
Expand All @@ -35,6 +49,12 @@ func createModule(_ *cobra.Command, args []string) {
fmt.Println("Error finding Git root:", err)
return
}
// Define the directory structure
generateModule(projectPath, args, projectModule)

}

func generateModule(projectPath string, args []string, projectModule model.GoMod) {
mainModulePath := filepath.Join(projectPath, "domain", "module.go")
var moduleName string
if len(args) == 1 {
Expand All @@ -50,7 +70,7 @@ func createModule(_ *cobra.Command, args []string) {
}
if q.Input.Exited() {
color.Redln("exited without completing...")
return

}
}
} else {
Expand All @@ -62,11 +82,10 @@ func createModule(_ *cobra.Command, args []string) {
}
data := utility.GetModuleDataFromModuleName(moduleName, projectModule.Module, projectModule.GoVersion)

// Define the directory structure
targetRoot := filepath.Join(".", "domain", data.PackageName)
templatePath := filepath.Join(".", "templates", "wesionary", "module")

err = utility.GenerateFiles(templatesFS, templatePath, targetRoot, data)
err := utility.GenerateFiles(templatesFS, templatePath, targetRoot, data)
if err != nil {
color.Redln("Error: generate file", err)
return
Expand All @@ -76,4 +95,5 @@ func createModule(_ *cobra.Command, args []string) {
utility.WriteContentToPath(mainModulePath, updatedCode)

utility.PrintColorizeModuleDetail(data)

}
10 changes: 2 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,10 @@ import (
)

var (
// Used for flags.
cfgFile string
userLicense string

rootCmd = &cobra.Command{
Use: "cobra-cli",
Use: "geng",
Short: "A generator for Cobra based Applications",
Long: `Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Long: `geng is a CLI library for Go that empowers applications.`,
}
templatesFS embed.FS
)
Expand Down
4 changes: 1 addition & 3 deletions cmd/run_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ func runProject(_ *cobra.Command, args []string) {
}

func init() {
newProjectCmd.Flags().StringP("mod", "m", "", "features name")
newProjectCmd.Flags().StringP("dir", "d", "", "target directory")
newProjectCmd.Flags().StringP("version", "v", "", "version support")
setupFlagsForNewProject(newProjectCmd)
rootCmd.AddCommand(newModuleCmd)
rootCmd.AddCommand(newProjectCmd)
rootCmd.AddCommand(runProjectCmd)
Expand Down

0 comments on commit 30bd043

Please sign in to comment.