diff --git a/cmd/create_project.go b/cmd/create_project.go new file mode 100644 index 0000000..5f44b6b --- /dev/null +++ b/cmd/create_project.go @@ -0,0 +1,92 @@ +package cmd + +import ( + "fmt" + "path/filepath" + + "github.com/gookit/color" + "github.com/mukezhz/geng/pkg/constant" + "github.com/mukezhz/geng/pkg/terminal" + "github.com/mukezhz/geng/pkg/utility" + "github.com/spf13/cobra" +) + +var newProjectCmd = &cobra.Command{ + Use: "new [project name]", + Short: "Create a new project", + Args: cobra.MaximumNArgs(1), + Run: createProject, +} + +func createProject(cmd *cobra.Command, args []string) { + var projectName string + var projectModuleName string + var goVersion string + var projectDescription string + var author string + var directory string + + if len(args) == 0 { + questions := []terminal.ProjectQuestion{ + terminal.NewShortQuestion(constant.ProjectNameKEY, constant.ProjectName+" *", "Enter Project Name:"), + terminal.NewShortQuestion(constant.ProjectModuleNameKEY, constant.ProjectModuleName+" *", "Enter Module Name:"), + terminal.NewShortQuestion(constant.AuthorKEY, constant.Author+" [Optional]", "Enter Author Detail[Mukesh Chaudhary ] [Optional]"), + terminal.NewLongQuestion(constant.ProjectDescriptionKEY, constant.ProjectDescription+" [Optional]", "Enter Project Description [Optional]"), + terminal.NewShortQuestion(constant.GoVersionKEY, constant.GoVersion+" [Optional]", "Enter Go Version (Default: 1.20) [Optional]"), + terminal.NewShortQuestion(constant.DirectoryKEY, constant.Directory+" [Optional]", "Enter Project Directory (Default: package_name) [Optional]"), + } + terminal.StartInteractiveTerminal(questions) + + for _, q := range questions { + switch q.Key { + case constant.ProjectNameKEY: + projectName = q.Answer + case constant.ProjectDescriptionKEY: + projectDescription = q.Answer + case constant.AuthorKEY: + author = q.Answer + case constant.ProjectModuleNameKEY: + projectModuleName = q.Answer + case constant.GoVersionKEY: + goVersion = q.Answer + case constant.DirectoryKEY: + directory = q.Answer + } + } + } else { + projectName = args[0] + projectModuleName, _ = cmd.Flags().GetString("mod") + goVersion, _ = cmd.Flags().GetString("version") + directory, _ = cmd.Flags().GetString("dir") + } + + goVersion = utility.CheckVersion(goVersion) + if projectName == "" { + color.Redln("Error: project name is required") + return + } + if projectModuleName == "" { + color.Redln("Error: module name is required") + return + } + + data := utility.GetModuleDataFromModuleName(projectName, projectModuleName, goVersion) + data.ProjectDescription = projectDescription + data.Author = author + + data.Directory = directory + if data.Directory == "" { + data.Directory = filepath.Join(data.Directory, data.PackageName) + } + targetRoot := data.Directory + + templatePath := filepath.Join("templates", "wesionary", "project") + err := utility.GenerateFiles(templatesFS, templatePath, targetRoot, data) + if err != nil { + color.Redln("Error generate file", err) + return + } + + utility.PrintColorizeProjectDetail(data) + fmt.Println("") +} diff --git a/cmd/new_module.go b/cmd/new_module.go new file mode 100644 index 0000000..2332fa3 --- /dev/null +++ b/cmd/new_module.go @@ -0,0 +1,79 @@ +package cmd + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/gookit/color" + "github.com/mukezhz/geng/pkg/constant" + "github.com/mukezhz/geng/pkg/terminal" + "github.com/mukezhz/geng/pkg/utility" + "github.com/spf13/cobra" +) + +var newModuleCmd = &cobra.Command{ + Use: "gen features [name]", + Short: "Create a new features", + Args: cobra.MaximumNArgs(2), + Run: createModule, +} + +func createModule(_ *cobra.Command, args []string) { + projectModule, err := utility.GetModuleNameFromGoModFile() + if err != nil { + fmt.Println("Error finding Module name from go.mod:", err) + return + } + currentDir, err := os.Getwd() + if err != nil { + color.Redln("Error getting current directory:", err) + panic(err) + } + projectPath, err := utility.FindGitRoot(currentDir) + if err != nil { + fmt.Println("Error finding Git root:", err) + return + } + mainModulePath := filepath.Join(projectPath, "domain", "features", "module.go") + if err != nil { + panic(err) + } + var moduleName string + if len(args) == 1 { + questions := []terminal.ProjectQuestion{ + terminal.NewShortQuestion(constant.ModueleNameKEY, constant.ModueleNameKEY+" *", "Enter Module Name:"), + } + terminal.StartInteractiveTerminal(questions) + + for _, q := range questions { + switch q.Key { + case constant.ModueleNameKEY: + moduleName = q.Answer + } + } + } else { + moduleName = args[1] + } + if !utility.CheckGolangIdentifier(moduleName) { + color.Redln("Error: module name is invalid") + return + } + data := utility.GetModuleDataFromModuleName(moduleName, projectModule.Module, projectModule.GoVersion) + + // Define the directory structure + targetRoot := filepath.Join(".", "domain", "features", data.PackageName) + templatePath := filepath.Join(".", "templates", "wesionary", "module") + + err = utility.GenerateFiles(templatesFS, templatePath, targetRoot, data) + if err != nil { + color.Redln("Error: generate file", err) + return + } + + updatedCode := utility.AddAnotherFxOptionsInModule(mainModulePath, data.PackageName, data.ProjectModuleName) + utility.WriteContentToPath(mainModulePath, updatedCode) + + utility.PrintColorizeModuleDetail(data) + +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..1230750 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,62 @@ +package cmd + +import ( + "embed" + "errors" + "fmt" + "os" + + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +var ( + // Used for flags. + cfgFile string + userLicense string + + rootCmd = &cobra.Command{ + Use: "cobra-cli", + 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.`, + } + templatesFS embed.FS +) + +func initConfig() { + if cfgFile != "" { + // Use config file from the flag. + viper.SetConfigFile(cfgFile) + } else { + // Find home directory. + home, err := os.UserHomeDir() + cobra.CheckErr(err) + + // Search config in home directory with name ".cobra" (without extension). + viper.AddConfigPath(home) + viper.SetConfigType("yaml") + viper.SetConfigName(".cobra") + } + + viper.AutomaticEnv() + + err := viper.ReadInConfig() + + notFound := &viper.ConfigFileNotFoundError{} + switch { + case err != nil && !errors.As(err, notFound): + cobra.CheckErr(err) + case err != nil && errors.As(err, notFound): + // The config file is optional, we shouldn't exit when the config is not found + break + default: + fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) + } +} + +func Execute(fs embed.FS) error { + templatesFS = fs + return rootCmd.Execute() +} diff --git a/cmd/run_project.go b/cmd/run_project.go new file mode 100644 index 0000000..843365a --- /dev/null +++ b/cmd/run_project.go @@ -0,0 +1,41 @@ +package cmd + +import ( + "github.com/mukezhz/geng/pkg/utility" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +var runProjectCmd = &cobra.Command{ + Use: "run [project name]", + Short: "Run the project", + Args: cobra.MaximumNArgs(1), + Run: runProject, +} + +func runProject(cmd *cobra.Command, args []string) { + runGo := "go" + // execute command from golang + err := utility.ExecuteCommand(runGo, args...) + if err != nil { + return + } +} + +func init() { + cobra.OnInitialize(initConfig) + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)") + rootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "author name for copyright attribution") + rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "name of license for the project") + rootCmd.PersistentFlags().Bool("viper", false, "use Viper for configuration") + cobra.CheckErr(viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))) + cobra.CheckErr(viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))) + viper.SetDefault("author", "NAME HERE ") + viper.SetDefault("license", "none") + newProjectCmd.Flags().StringP("mod", "m", "", "features name") + newProjectCmd.Flags().StringP("dir", "d", "", "target directory") + newProjectCmd.Flags().StringP("version", "v", "", "version support") + rootCmd.AddCommand(newModuleCmd) + rootCmd.AddCommand(newProjectCmd) + rootCmd.AddCommand(runProjectCmd) +} diff --git a/go.mod b/go.mod index de7935f..a5cb0bd 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/charmbracelet/lipgloss v0.7.1 github.com/gookit/color v1.5.4 github.com/spf13/cobra v1.8.0 + github.com/spf13/viper v1.18.2 go.uber.org/fx v1.20.1 golang.org/x/text v0.14.0 ) @@ -16,23 +17,37 @@ require ( github.com/atotto/clipboard v0.1.4 // indirect github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect + github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-isatty v0.0.19 // indirect github.com/mattn/go-localereader v0.0.1 // indirect github.com/mattn/go-runewidth v0.0.14 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect github.com/muesli/cancelreader v0.2.2 // indirect github.com/muesli/reflow v0.3.0 // indirect github.com/muesli/termenv v0.15.2 // indirect + github.com/pelletier/go-toml/v2 v2.1.0 // indirect github.com/rivo/uniseg v0.4.4 // indirect + github.com/sagikazarmark/locafero v0.4.0 // indirect + github.com/sagikazarmark/slog-shim v0.1.0 // indirect + github.com/sourcegraph/conc v0.3.0 // indirect + github.com/spf13/afero v1.11.0 // indirect + github.com/spf13/cast v1.6.0 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/subosito/gotenv v1.6.0 // indirect github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect - go.uber.org/atomic v1.7.0 // indirect + go.uber.org/atomic v1.9.0 // indirect go.uber.org/dig v1.17.0 // indirect - go.uber.org/multierr v1.6.0 // indirect + go.uber.org/multierr v1.9.0 // indirect go.uber.org/zap v1.23.0 // indirect - golang.org/x/sync v0.3.0 // indirect - golang.org/x/sys v0.12.0 // indirect + golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect + golang.org/x/sync v0.5.0 // indirect + golang.org/x/sys v0.15.0 // indirect golang.org/x/term v0.12.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 8c3f3a5..a3c047a 100644 --- a/go.sum +++ b/go.sum @@ -14,14 +14,29 @@ github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2 github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk= github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= +github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0= github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= +github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4= @@ -29,6 +44,8 @@ github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+Ei github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI= github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo= github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= @@ -37,47 +54,77 @@ github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= -github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= -github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= +github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= +github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= +github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= +github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= +github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= +github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= +github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= +github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= +github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= +github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ= +github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8= github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= -go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= -go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= +go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/dig v1.17.0 h1:5Chju+tUvcC+N7N6EV08BJz41UZuO3BmHcN4A287ZLI= go.uber.org/dig v1.17.0/go.mod h1:rTxpf7l5I0eBTlE6/9RL+lDybC7WFwY2QH55ZSjy1mU= go.uber.org/fx v1.20.1 h1:zVwVQGS8zYvhh9Xxcu4w1M6ESyeMzebzj2NbSayZ4Mk= go.uber.org/fx v1.20.1/go.mod h1:iSYNbHf2y55acNCwCXKx7LbWb5WG1Bnue5RDXz1OREg= go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= -go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= -go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= +go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= +go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY= go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY= -golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= -golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= +golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= +golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= -golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.12.0 h1:/ZfYdc3zq+q02Rv9vGqTeSItdzZTSNDmfTi0mBAuidU= golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index ff970ed..c0415b2 100644 --- a/main.go +++ b/main.go @@ -2,197 +2,16 @@ package main import ( "embed" - "fmt" - "github.com/gookit/color" - "github.com/mukezhz/geng/pkg/constant" - "github.com/mukezhz/geng/pkg/terminal" - "github.com/mukezhz/geng/pkg/utility" - "github.com/spf13/cobra" "os" - "path/filepath" + + "github.com/mukezhz/geng/cmd" ) //go:embed templates/wesionary/* var templatesFS embed.FS -var rootCmd = &cobra.Command{ - Use: "geng", - Short: "Go Generate[geng] is a tool for generating Go modules", - Long: "Go Generate[geng] is a tool for generating Go modules. ", - TraverseChildren: true, -} - -var newModuleCmd = &cobra.Command{ - Use: "gen features [name]", - Short: "Create a new features", - Args: cobra.MaximumNArgs(2), - Run: createModule, -} - -var newProjectCmd = &cobra.Command{ - Use: "new [project name]", - Short: "Create a new project", - Args: cobra.MaximumNArgs(1), - Run: createProject, -} - -var runProjectCmd = &cobra.Command{ - Use: "run [project name]", - Short: "Run the project", - Args: cobra.MaximumNArgs(1), - Run: runProject, -} - -func init() { - newProjectCmd.Flags().StringP("mod", "m", "", "features name") - newProjectCmd.Flags().StringP("dir", "d", "", "target directory") - newProjectCmd.Flags().StringP("version", "v", "", "version support") - rootCmd.AddCommand(newModuleCmd, newProjectCmd, runProjectCmd) -} - func main() { - if err := rootCmd.Execute(); err != nil { + if err := cmd.Execute(templatesFS); err != nil { os.Exit(1) } } - -func createModule(_ *cobra.Command, args []string) { - projectModule, err := utility.GetModuleNameFromGoModFile() - if err != nil { - fmt.Println("Error finding Module name from go.mod:", err) - return - } - currentDir, err := os.Getwd() - if err != nil { - color.Redln("Error getting current directory:", err) - panic(err) - } - projectPath, err := utility.FindGitRoot(currentDir) - if err != nil { - fmt.Println("Error finding Git root:", err) - return - } - mainModulePath := filepath.Join(projectPath, "domain", "features", "module.go") - if err != nil { - panic(err) - } - var moduleName string - if len(args) == 1 { - questions := []terminal.ProjectQuestion{ - terminal.NewShortQuestion(constant.ModueleNameKEY, constant.ModueleNameKEY+" *", "Enter Module Name:"), - } - terminal.StartInteractiveTerminal(questions) - - for _, q := range questions { - switch q.Key { - case constant.ModueleNameKEY: - moduleName = q.Answer - } - } - } else { - moduleName = args[1] - } - if !utility.CheckGolangIdentifier(moduleName) { - color.Redln("Error: module name is invalid") - return - } - data := utility.GetModuleDataFromModuleName(moduleName, projectModule.Module, projectModule.GoVersion) - - // Define the directory structure - targetRoot := filepath.Join(".", "domain", "features", data.PackageName) - templatePath := filepath.Join(".", "templates", "wesionary", "module") - - err = utility.GenerateFiles(templatesFS, templatePath, targetRoot, data) - if err != nil { - color.Redln("Error: generate file", err) - return - } - - updatedCode := utility.AddAnotherFxOptionsInModule(mainModulePath, data.PackageName, data.ProjectModuleName) - utility.WriteContentToPath(mainModulePath, updatedCode) - - utility.PrintColorizeModuleDetail(data) - -} - -func createProject(cmd *cobra.Command, args []string) { - var projectName string - var projectModuleName string - var goVersion string - var projectDescription string - var author string - var directory string - - if len(args) == 0 { - questions := []terminal.ProjectQuestion{ - terminal.NewShortQuestion(constant.ProjectNameKEY, constant.ProjectName+" *", "Enter Project Name:"), - terminal.NewShortQuestion(constant.ProjectModuleNameKEY, constant.ProjectModuleName+" *", "Enter Module Name:"), - terminal.NewShortQuestion(constant.AuthorKEY, constant.Author+" [Optional]", "Enter Author Detail[Mukesh Chaudhary ] [Optional]"), - terminal.NewLongQuestion(constant.ProjectDescriptionKEY, constant.ProjectDescription+" [Optional]", "Enter Project Description [Optional]"), - terminal.NewShortQuestion(constant.GoVersionKEY, constant.GoVersion+" [Optional]", "Enter Go Version (Default: 1.20) [Optional]"), - terminal.NewShortQuestion(constant.DirectoryKEY, constant.Directory+" [Optional]", "Enter Project Directory (Default: package_name) [Optional]"), - } - terminal.StartInteractiveTerminal(questions) - - for _, q := range questions { - switch q.Key { - case constant.ProjectNameKEY: - projectName = q.Answer - case constant.ProjectDescriptionKEY: - projectDescription = q.Answer - case constant.AuthorKEY: - author = q.Answer - case constant.ProjectModuleNameKEY: - projectModuleName = q.Answer - case constant.GoVersionKEY: - goVersion = q.Answer - case constant.DirectoryKEY: - directory = q.Answer - } - } - } else { - projectName = args[0] - projectModuleName, _ = cmd.Flags().GetString("mod") - goVersion, _ = cmd.Flags().GetString("version") - directory, _ = cmd.Flags().GetString("dir") - } - - goVersion = utility.CheckVersion(goVersion) - if projectName == "" { - color.Redln("Error: project name is required") - return - } - if projectModuleName == "" { - color.Redln("Error: module name is required") - return - } - - data := utility.GetModuleDataFromModuleName(projectName, projectModuleName, goVersion) - data.ProjectDescription = projectDescription - data.Author = author - - data.Directory = directory - if data.Directory == "" { - data.Directory = filepath.Join(data.Directory, data.PackageName) - } - targetRoot := data.Directory - - templatePath := filepath.Join("templates", "wesionary", "project") - err := utility.GenerateFiles(templatesFS, templatePath, targetRoot, data) - if err != nil { - color.Redln("Error generate file", err) - return - } - - utility.PrintColorizeProjectDetail(data) - fmt.Println("") -} - -func runProject(cmd *cobra.Command, args []string) { - runGo := "go" - // execute command from golang - err := utility.ExecuteCommand(runGo, args...) - if err != nil { - return - } -}