diff --git a/.gitignore b/.gitignore index 0131197..77a072f 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,5 @@ # Go workspace file go.work .idea +bin *.log \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fb41320 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +.PHONY: build-linux +build-linux: + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o ./bin/nunu . + +.PHONY: build-darwin +build-darwin: + CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -o ./bin/nunu . + +.PHONY: build-windows +build-windows: + CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -o ./bin/nunu.exe . + diff --git a/cmd/nunu/root.go b/cmd/nunu/root.go index 0eec149..fbb57bf 100644 --- a/cmd/nunu/root.go +++ b/cmd/nunu/root.go @@ -6,7 +6,7 @@ import ( "github.com/go-nunu/nunu/internal/command/wire" "github.com/go-nunu/nunu/internal/command/create" - "github.com/go-nunu/nunu/internal/command/project" + "github.com/go-nunu/nunu/internal/command/new" "github.com/go-nunu/nunu/internal/command/run" "github.com/go-nunu/nunu/internal/command/upgrade" "github.com/spf13/cobra" @@ -20,7 +20,7 @@ var CmdRoot = &cobra.Command{ } func init() { - CmdRoot.AddCommand(project.CmdNew) + CmdRoot.AddCommand(new.CmdNew) CmdRoot.AddCommand(create.CmdCreate) CmdRoot.AddCommand(run.CmdRun) diff --git a/config/config.go b/config/config.go index fa3812f..78f20d3 100644 --- a/config/config.go +++ b/config/config.go @@ -1,9 +1,11 @@ package config -const ( - Version = "1.0.7" - WireCmd = "github.com/google/wire/cmd/wire@latest" - NunuCmd = "github.com/go-nunu/nunu@latest" - RepoBase = "https://github.com/go-nunu/nunu-layout-base.git" - RepoAdvanced = "https://github.com/go-nunu/nunu-layout-advanced.git" +var ( + Version = "1.0.8" + WireCmd = "github.com/google/wire/cmd/wire@latest" + NunuCmd = "github.com/go-nunu/nunu@latest" + RepoBase = "https://github.com/go-nunu/nunu-layout-base.git" + RepoAdvanced = "https://github.com/go-nunu/nunu-layout-advanced.git" + RunExcludeDir = ".git,.idea,tmp,vendor" + RunIncludeExt = "go,tpl,tmpl,html,yaml,yml,toml,ini,json" ) diff --git a/docs/en/tutorial.md b/docs/en/tutorial.md index 0e6ead6..732ac38 100644 --- a/docs/en/tutorial.md +++ b/docs/en/tutorial.md @@ -190,6 +190,31 @@ internal/repository/order.go // Interact with databases and Redis internal/model/order.go // Database table entity, GORM model ``` +## Starting the Project +Finally, in Nunu, you can use the following command to start the project: + +```bash +// Please modify the MySQL and Redis configuration information in config/local.yml before starting the server + +// Before starting the server for the first time, run the following database migration +nunu run ./cmd/migration + +// Start the server +nunu run ./cmd/server + +// Or + +nunu run + +// Or + +nunu run ./cmd/server --excludeDir=".git,.idea,tmp,vendor" --includeExt="go,yml,vue" -- --conf=./config/local.yml +``` + +After running the above command, Nunu will automatically start the project and monitor file updates, supporting hot-reloading. + + + ## Automatic Generation of Swagger Documentation First, we need to install the swag command-line tool on our local machine. You can do this by running the following command: @@ -228,24 +253,6 @@ Open the documentation page in your browser: http://127.0.0.1:8000/swagger/index.html ``` -## Starting the Project -Finally, in Nunu, you can use the following command to start the project: - -```bash -// Please modify the MySQL and Redis configuration information in config/local.yml before starting the server - -// Before starting the server for the first time, run the following database migration -nunu run ./cmd/migration - -// Start the server -nunu run ./cmd/server - -// Or - -nunu run -``` - -After running the above command, Nunu will automatically start the project and monitor file updates, supporting hot-reloading. ## Conclusion diff --git a/docs/zh/tutorial.md b/docs/zh/tutorial.md index 883f06d..e28c620 100644 --- a/docs/zh/tutorial.md +++ b/docs/zh/tutorial.md @@ -198,6 +198,30 @@ internal/repository/order.go // 与数据库和Redis等交互 internal/model/order.go // 数据表实体,GORM model ``` +## 启动项目 +最后,在Nunu中,可以使用以下命令启动项目: + +```bash +// 请先修改config/local.yml中的 MySQL 和 Redis 配置信息 + +// 初次启动server之前,请先执行以下数据库迁移 +nunu run ./cmd/migration + + // 启动server +nunu run ./cmd/server + +// 或 + +nunu run + +// 或 + + nunu run ./cmd/server --excludeDir=".git,.idea,tmp,vendor" --includeExt="go,yml,vue" -- --conf=./config/local.yml +``` + +执行完上述命令后,Nunu会自动启动项目,并监听文件更新,支持热重启。 + + ## 自动化生成Swagger文档 @@ -238,25 +262,6 @@ make swag http://127.0.0.1:8000/swagger/index.html ``` -## 启动项目 -最后,在Nunu中,可以使用以下命令启动项目: - -```bash -// 请先修改config/local.yml中的 MySQL 和 Redis 配置信息 - -// 初次启动server之前,请先执行以下数据库迁移 -nunu run ./cmd/migration - - // 启动server -nunu run ./cmd/server - -// 或 - -nunu run -``` - -执行完上述命令后,Nunu会自动启动项目,并监听文件更新,支持热重启。 - ## 总结 Nunu框架提供了一套优雅的项目结构和命令操作,使得开发者可以更加高效地开发Web应用程序。通过本教程,你已经学会了如何使用Nunu创建项目、创建Handler、创建Service、创建Repository、编译Wire和启动项目。希望这些内容能够帮助你更好地使用Nunu框架。 diff --git a/go.mod b/go.mod index 45945b4..d9ad9d2 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/go-nunu/nunu go 1.16 require ( - github.com/AlecAivazis/survey/v2 v2.3.6 + github.com/AlecAivazis/survey/v2 v2.3.7 github.com/fsnotify/fsnotify v1.6.0 github.com/spf13/cobra v1.7.0 ) diff --git a/go.sum b/go.sum index b04d31b..4883fbf 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/AlecAivazis/survey/v2 v2.3.6 h1:NvTuVHISgTHEHeBFqt6BHOe4Ny/NwGZr7w+F8S9ziyw= -github.com/AlecAivazis/survey/v2 v2.3.6/go.mod h1:4AuI9b7RjAR+G7v9+C4YSlX/YL3K3cWNXgWXOhllqvI= +github.com/AlecAivazis/survey/v2 v2.3.7 h1:6I/u8FvytdGsgonrYsVn2t8t4QiRnh6QSTqkkhIiSjQ= +github.com/AlecAivazis/survey/v2 v2.3.7/go.mod h1:xUTIdE4KCOIjsBAE1JYsUPoCqYdZ1reCfTwbto0Fduo= github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s= github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= @@ -32,16 +32,35 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956 h1:XeJjHH1KiLpKGb6lvMiksZ9l0fVUh+AmGcm0nOMEBOY= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20210503060354-a79de5458b56 h1:b8jxX3zqjpqb2LklXPzKSGJhzyxCOZSz8ncv8Nv+y7w= -golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY= -golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg= +golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 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= diff --git a/internal/command/project/project.go b/internal/command/new/new.go similarity index 98% rename from internal/command/project/project.go rename to internal/command/new/new.go index 7b8b0d0..d90d519 100644 --- a/internal/command/project/project.go +++ b/internal/command/new/new.go @@ -1,14 +1,14 @@ -package project +package new import ( "bytes" "fmt" + "github.com/AlecAivazis/survey/v2" "log" "os" "os/exec" "path/filepath" - "github.com/AlecAivazis/survey/v2" "github.com/go-nunu/nunu/config" "github.com/go-nunu/nunu/internal/pkg/helper" "github.com/spf13/cobra" @@ -37,8 +37,6 @@ func NewProject() *Project { return &Project{} } -// the questions to ask - func run(cmd *cobra.Command, args []string) { p := NewProject() if len(args) == 0 { @@ -48,7 +46,6 @@ func run(cmd *cobra.Command, args []string) { Suggest: nil, }, &p.ProjectName, survey.WithValidator(survey.Required)) if err != nil { - fmt.Println(err.Error()) return } } else { diff --git a/internal/command/run/run_unix.go b/internal/command/run/run_unix.go index 57b1a60..52041f8 100644 --- a/internal/command/run/run_unix.go +++ b/internal/command/run/run_unix.go @@ -5,11 +5,13 @@ package run import ( "fmt" + "github.com/go-nunu/nunu/config" "log" "os" "os/exec" "os/signal" "path/filepath" + "strings" "syscall" "time" @@ -24,6 +26,20 @@ var quit = make(chan os.Signal, 1) type Run struct { } +var excludeDir string +var includeExt string + +func init() { + CmdRun.Flags().StringVarP(&excludeDir, "excludeDir", "", excludeDir, `eg: nunu run --excludeDir="tmp,vendor,.git,.idea"`) + CmdRun.Flags().StringVarP(&includeExt, "includeExt", "", includeExt, `eg: nunu run --includeExt="go,tpl,tmpl,html,yaml,yml,toml,ini,json"`) + if excludeDir == "" { + excludeDir = config.RunExcludeDir + } + if includeExt == "" { + includeExt = config.RunIncludeExt + } +} + var CmdRun = &cobra.Command{ Use: "run", Short: "nunu run [main.go path]", @@ -41,7 +57,7 @@ var CmdRun = &cobra.Command{ return } if dir == "" { - cmdPath, err := helper.FindMain(base) + cmdPath, err := helper.FindMain(base, excludeDir) if err != nil { fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", err) @@ -74,6 +90,8 @@ var CmdRun = &cobra.Command{ } signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) fmt.Printf("\033[35mNunu run %s.\033[0m\n", dir) + fmt.Printf("\033[35mWatch excludeDir %s\033[0m\n", excludeDir) + fmt.Printf("\033[35mWatch includeExt %s\033[0m\n", includeExt) watch(dir, programArgs) }, @@ -92,20 +110,33 @@ func watch(dir string, programArgs []string) { } defer watcher.Close() + excludeDirArr := strings.Split(excludeDir, ",") + includeExtArr := strings.Split(includeExt, ",") + includeExtMap := make(map[string]struct{}) + for _, s := range includeExtArr { + includeExtMap[s] = struct{}{} + } // Add files to watcher err = filepath.Walk(watchPath, func(path string, info os.FileInfo, err error) error { if err != nil { return err } + for _, s := range excludeDirArr { + if s == "" { + continue + } + if strings.HasPrefix(path, s) { + return nil + } + } if !info.IsDir() { ext := filepath.Ext(info.Name()) - if ext == ".go" || ext == ".yml" || ext == ".yaml" || ext == ".html" { + if _, ok := includeExtMap[strings.TrimPrefix(ext, ".")]; ok { err = watcher.Add(path) if err != nil { fmt.Println("Error:", err) } } - } return nil }) diff --git a/internal/command/run/run_windows.go b/internal/command/run/run_windows.go index ff959ba..3051900 100644 --- a/internal/command/run/run_windows.go +++ b/internal/command/run/run_windows.go @@ -5,12 +5,14 @@ package run import ( "fmt" + "github.com/go-nunu/nunu/config" "log" "os" "os/exec" "os/signal" "path/filepath" "strconv" + "strings" "syscall" "time" @@ -25,6 +27,20 @@ var quit = make(chan os.Signal, 1) type Run struct { } +var excludeDir string +var includeExt string + +func init() { + CmdRun.Flags().StringVarP(&excludeDir, "excludeDir", "", excludeDir, `eg: nunu run --excludeDir="tmp,vendor,.git,.idea"`) + CmdRun.Flags().StringVarP(&includeExt, "includeExt", "", includeExt, `eg: nunu run --includeExt="go,tpl,tmpl,html,yaml,yml,toml,ini,json"`) + if excludeDir == "" { + excludeDir = config.RunExcludeDir + } + if includeExt == "" { + includeExt = config.RunIncludeExt + } +} + var CmdRun = &cobra.Command{ Use: "run", Short: "nunu run [main.go path]", @@ -42,7 +58,7 @@ var CmdRun = &cobra.Command{ return } if dir == "" { - cmdPath, err := helper.FindMain(base) + cmdPath, err := helper.FindMain(base, excludeDir) if err != nil { fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", err) @@ -75,6 +91,8 @@ var CmdRun = &cobra.Command{ } signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) fmt.Printf("\033[35mNunu run %s.\033[0m\n", dir) + fmt.Printf("\033[35mWatch excludeDir %s\033[0m\n", excludeDir) + fmt.Printf("\033[35mWatch includeExt %s\033[0m\n", includeExt) watch(dir, programArgs) }, @@ -93,14 +111,28 @@ func watch(dir string, programArgs []string) { } defer watcher.Close() + excludeDirArr := strings.Split(excludeDir, ",") + includeExtArr := strings.Split(includeExt, ",") + includeExtMap := make(map[string]struct{}) + for _, s := range includeExtArr { + includeExtMap[s] = struct{}{} + } // Add files to watcher err = filepath.Walk(watchPath, func(path string, info os.FileInfo, err error) error { if err != nil { return err } + for _, s := range excludeDirArr { + if s == "" { + continue + } + if strings.HasPrefix(path, s) { + return nil + } + } if !info.IsDir() { ext := filepath.Ext(info.Name()) - if ext == ".go" || ext == ".yml" || ext == ".yaml" || ext == ".html" { + if _, ok := includeExtMap[strings.TrimPrefix(ext, ".")]; ok { err = watcher.Add(path) if err != nil { fmt.Println("Error:", err) diff --git a/internal/pkg/helper/helper.go b/internal/pkg/helper/helper.go index 452af15..a7f183c 100644 --- a/internal/pkg/helper/helper.go +++ b/internal/pkg/helper/helper.go @@ -33,7 +33,7 @@ func SplitArgs(cmd *cobra.Command, args []string) (cmdArgs, programArgs []string } return args, []string{} } -func FindMain(base string) (map[string]string, error) { +func FindMain(base, excludeDir string) (map[string]string, error) { wd, err := os.Getwd() if err != nil { return nil, err @@ -41,11 +41,17 @@ func FindMain(base string) (map[string]string, error) { if !strings.HasSuffix(wd, "/") { wd += "/" } + excludeDirArr := strings.Split(excludeDir, ",") cmdPath := make(map[string]string) err = filepath.Walk(base, func(path string, info os.FileInfo, err error) error { if err != nil { return err } + for _, s := range excludeDirArr { + if strings.HasPrefix(path, s) { + return nil + } + } if !info.IsDir() && filepath.Ext(path) == ".go" { content, err := os.ReadFile(path) if err != nil {