diff --git a/README_zh.md b/README_zh.md index 7ca40e9..f71a3fe 100644 --- a/README_zh.md +++ b/README_zh.md @@ -166,23 +166,49 @@ Nunu采用了经典的分层架构。同时,为了更好地实现模块化和 go install github.com/go-nunu/nunu@latest ``` +国内用户可以使用`GOPROXY`加速`go install` + +``` +$ go env -w GO111MODULE=on +$ go env -w GOPROXY=https://goproxy.cn,direct +``` + > tips: 如果`go install`成功,却提示找不到nunu命令,这是因为环境变量没有配置,可以把 GOBIN 目录配置到环境变量中即可 + ### 创建新项目 您可以使用以下命令创建一个新的Golang项目: ```bash +// 推荐新用户选择Advanced Layout nunu new projectName ``` -默认拉取github源,你也可以使用国内加速仓库 + +`nunu new`默认拉取github源,你也可以使用国内加速仓库 ``` +// 使用高级模板(推荐) +nunu new projectName -r https://gitee.com/go-nunu/nunu-layout-advanced.git + // 使用基础模板 nunu new projectName -r https://gitee.com/go-nunu/nunu-layout-basic.git -// 使用高级模板 -nunu new projectName -r https://gitee.com/go-nunu/nunu-layout-advanced.git + ``` + +> Nunu内置了两种类型的Layout: + +* **基础模板(Basic Layout)** + +Basic Layout 包含一个非常精简的架构目录结构,适合非常熟悉Nunu项目的开发者使用。 + +* **高级模板(Advanced Layout)** + +**建议:我们推荐新手优先选择使用Advanced Layout。** + + +Advanced Layout 包含了很多Nunu的用法示例( db、redis、 jwt、 cron、 migration等),适合开发者快速学习了解Nunu的架构思想。 + 此命令将创建一个名为`projectName`的目录,并在其中生成一个优雅的Golang项目结构。 ### 创建组件 diff --git a/cmd/nunu/root.go b/cmd/nunu/root.go index a9ede41..9945879 100644 --- a/cmd/nunu/root.go +++ b/cmd/nunu/root.go @@ -2,37 +2,37 @@ package nunu import ( "fmt" + "github.com/go-nunu/nunu/config" "github.com/go-nunu/nunu/internal/command/create" "github.com/go-nunu/nunu/internal/command/project" "github.com/go-nunu/nunu/internal/command/run" "github.com/go-nunu/nunu/internal/command/upgrade" "github.com/go-nunu/nunu/internal/command/wire" - "github.com/go-nunu/nunu/internal/version" "github.com/spf13/cobra" ) -var rootCmd = &cobra.Command{ +var CmdRoot = &cobra.Command{ Use: "nunu", Example: "nunu new demo-api", Short: "\n _ _ \n| \\ | |_ _ _ __ _ _ \n| \\| | | | | '_ \\| | | |\n| |\\ | |_| | | | | |_| |\n|_| \\_|\\__,_|_| |_|\\__,_| \n \n" + "\x1B[38;2;66;211;146mA\x1B[39m \x1B[38;2;67;209;149mC\x1B[39m\x1B[38;2;68;206;152mL\x1B[39m\x1B[38;2;69;204;155mI\x1B[39m \x1B[38;2;70;201;158mt\x1B[39m\x1B[38;2;71;199;162mo\x1B[39m\x1B[38;2;72;196;165mo\x1B[39m\x1B[38;2;73;194;168ml\x1B[39m \x1B[38;2;74;192;171mf\x1B[39m\x1B[38;2;75;189;174mo\x1B[39m\x1B[38;2;76;187;177mr\x1B[39m \x1B[38;2;77;184;180mb\x1B[39m\x1B[38;2;78;182;183mu\x1B[39m\x1B[38;2;79;179;186mi\x1B[39m\x1B[38;2;80;177;190ml\x1B[39m\x1B[38;2;81;175;193md\x1B[39m\x1B[38;2;82;172;196mi\x1B[39m\x1B[38;2;83;170;199mn\x1B[39m\x1B[38;2;83;167;202mg\x1B[39m \x1B[38;2;84;165;205mg\x1B[39m\x1B[38;2;85;162;208mo\x1B[39m \x1B[38;2;86;160;211ma\x1B[39m\x1B[38;2;87;158;215mp\x1B[39m\x1B[38;2;88;155;218ml\x1B[39m\x1B[38;2;89;153;221mi\x1B[39m\x1B[38;2;90;150;224mc\x1B[39m\x1B[38;2;91;148;227ma\x1B[39m\x1B[38;2;92;145;230mt\x1B[39m\x1B[38;2;93;143;233mi\x1B[39m\x1B[38;2;94;141;236mo\x1B[39m\x1B[38;2;95;138;239mn\x1B[39m\x1B[38;2;96;136;243m.\x1B[39m", - Version: fmt.Sprintf("\n _ _ \n| \\ | |_ _ _ __ _ _ \n| \\| | | | | '_ \\| | | |\n| |\\ | |_| | | | | |_| |\n|_| \\_|\\__,_|_| |_|\\__,_| \n \nNunu %s - Copyright (c) 2023 Nunu\nReleased under the MIT License.\n\n", version.Version), + Version: fmt.Sprintf("\n _ _ \n| \\ | |_ _ _ __ _ _ \n| \\| | | | | '_ \\| | | |\n| |\\ | |_| | | | | |_| |\n|_| \\_|\\__,_|_| |_|\\__,_| \n \nNunu %s - Copyright (c) 2023 Nunu\nReleased under the MIT License.\n\n", config.Version), } func init() { - rootCmd.AddCommand(project.NewCmd) - rootCmd.AddCommand(create.CreateCmd) - rootCmd.AddCommand(wire.WireCmd) - rootCmd.AddCommand(run.RunCmd) - rootCmd.AddCommand(upgrade.UpgradeCmd) - create.CreateCmd.AddCommand(create.CreateHandlerCmd) - create.CreateCmd.AddCommand(create.CreateServiceCmd) - create.CreateCmd.AddCommand(create.CreateRepositoryCmd) - create.CreateCmd.AddCommand(create.CreateModelCmd) - create.CreateCmd.AddCommand(create.CreateAllCmd) + CmdRoot.AddCommand(project.CmdNew) + CmdRoot.AddCommand(create.CmdCreate) + CmdRoot.AddCommand(wire.CmdWire) + CmdRoot.AddCommand(run.CmdRun) + CmdRoot.AddCommand(upgrade.CmdUpgrade) + create.CmdCreate.AddCommand(create.CmdCreateHandler) + create.CmdCreate.AddCommand(create.CmdCreateService) + create.CmdCreate.AddCommand(create.CmdCreateRepository) + create.CmdCreate.AddCommand(create.CmdCreateModel) + create.CmdCreate.AddCommand(create.CmdCreateAll) } // Execute executes the root command. func Execute() error { - return rootCmd.Execute() + return CmdRoot.Execute() } diff --git a/config/config.go b/config/config.go index d9aa7d9..e92ee1c 100644 --- a/config/config.go +++ b/config/config.go @@ -1,6 +1,7 @@ package config const ( + Version = "1.0.5" 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" diff --git a/docs/zh/guide.md b/docs/zh/guide.md index 0736178..8f9cfb9 100644 --- a/docs/zh/guide.md +++ b/docs/zh/guide.md @@ -19,6 +19,13 @@ Nunu 是一个基于 Golang 的应用脚手架,它可以帮助您快速构建 go install github.com/go-nunu/nunu@latest ``` +国内用户可以使用`GOPROXY`加速`go install` + +``` +$ go env -w GO111MODULE=on +$ go env -w GOPROXY=https://goproxy.cn,direct +``` + > tips: 如果`go install`成功,却提示找不到nunu命令,这是因为环境变量没有配置,可以把 GOBIN 目录配置到环境变量中即可 ## 创建新项目 @@ -27,6 +34,8 @@ go install github.com/go-nunu/nunu@latest ```bash nunu new projectName + +// 推荐新用户选择Advanced Layout ``` 此命令将创建一个名为 `projectName` 的目录,并在其中生成一个优雅的 Golang 项目结构。 @@ -35,10 +44,12 @@ nunu new projectName `nunu new`默认拉取github源,你也可以使用国内加速仓库 ``` -// 使用基础模板 -nunu new projectName -r https://gitee.com/go-nunu/nunu-layout-basic.git // 使用高级模板(推荐) nunu new projectName -r https://gitee.com/go-nunu/nunu-layout-advanced.git + +// 使用基础模板 +nunu new projectName -r https://gitee.com/go-nunu/nunu-layout-basic.git + ``` diff --git a/internal/command/create/create.go b/internal/command/create/create.go index 8b2cecd..de87e5c 100644 --- a/internal/command/create/create.go +++ b/internal/command/create/create.go @@ -27,7 +27,7 @@ func NewCreate() *Create { return &Create{} } -var CreateCmd = &cobra.Command{ +var CmdCreate = &cobra.Command{ Use: "create [type] [handler-name]", Short: "Create a new handler/service/repository/model", Example: "nunu create handler user", @@ -36,35 +36,35 @@ var CreateCmd = &cobra.Command{ }, } -var CreateHandlerCmd = &cobra.Command{ +var CmdCreateHandler = &cobra.Command{ Use: "handler", Short: "Create a new handler", Example: "nunu create handler user", Args: cobra.ExactArgs(1), Run: runCreate, } -var CreateServiceCmd = &cobra.Command{ +var CmdCreateService = &cobra.Command{ Use: "service", Short: "Create a new service", Example: "nunu create service user", Args: cobra.ExactArgs(1), Run: runCreate, } -var CreateRepositoryCmd = &cobra.Command{ +var CmdCreateRepository = &cobra.Command{ Use: "repository", Short: "Create a new repository", Example: "nunu create repository user", Args: cobra.ExactArgs(1), Run: runCreate, } -var CreateModelCmd = &cobra.Command{ +var CmdCreateModel = &cobra.Command{ Use: "model", Short: "Create a new model", Example: "nunu create model user", Args: cobra.ExactArgs(1), Run: runCreate, } -var CreateAllCmd = &cobra.Command{ +var CmdCreateAll = &cobra.Command{ Use: "all", Short: "Create a new handler & service & repository & model", Example: "nunu create all user", @@ -127,7 +127,6 @@ func (c *Create) genFile() { } func createFile(dirPath string, filename string) *os.File { filePath := dirPath + filename - // 创建文件夹 err := os.MkdirAll(dirPath, os.ModePerm) if err != nil { log.Fatalf("Failed to create dir %s: %v", dirPath, err) @@ -136,7 +135,6 @@ func createFile(dirPath string, filename string) *os.File { if stat != nil { return nil } - // 创建文件 file, err := os.Create(filePath) if err != nil { log.Fatalf("Failed to create file %s: %v", filePath, err) diff --git a/internal/command/project/project.go b/internal/command/project/project.go index 692ba53..92bd569 100644 --- a/internal/command/project/project.go +++ b/internal/command/project/project.go @@ -18,7 +18,7 @@ type Project struct { ProjectName string `survey:"name"` } -var NewCmd = &cobra.Command{ +var CmdNew = &cobra.Command{ Use: "new", Example: "nunu new demo-api", Short: "create a new project.", @@ -30,7 +30,7 @@ var ( ) func init() { - NewCmd.Flags().StringVarP(&repoURL, "repo-url", "r", repoURL, "layout repo") + CmdNew.Flags().StringVarP(&repoURL, "repo-url", "r", repoURL, "layout repo") } func NewProject() *Project { @@ -178,7 +178,7 @@ func (p *Project) modTidy() error { func (p *Project) rmGit() { os.RemoveAll(p.ProjectName + "/.git") } -func (p Project) installWire() { +func (p *Project) installWire() { fmt.Printf("go install %s\n", config.WireCmd) cmd := exec.Command("go", "install", config.WireCmd) cmd.Stdout = os.Stdout diff --git a/internal/command/run/run_unix.go b/internal/command/run/run_unix.go index 9a7a407..2022127 100644 --- a/internal/command/run/run_unix.go +++ b/internal/command/run/run_unix.go @@ -24,7 +24,7 @@ var quit = make(chan os.Signal, 1) type Run struct { } -var RunCmd = &cobra.Command{ +var CmdRun = &cobra.Command{ Use: "run", Short: "nunu run [main.go path]", Long: "nunu run [main.go path]", diff --git a/internal/command/run/run_windows.go b/internal/command/run/run_windows.go index cf49a8a..ff959ba 100644 --- a/internal/command/run/run_windows.go +++ b/internal/command/run/run_windows.go @@ -25,7 +25,7 @@ var quit = make(chan os.Signal, 1) type Run struct { } -var RunCmd = &cobra.Command{ +var CmdRun = &cobra.Command{ Use: "run", Short: "nunu run [main.go path]", Long: "nunu run [main.go path]", diff --git a/internal/command/upgrade/upgrade.go b/internal/command/upgrade/upgrade.go index 82e6080..1cecadf 100644 --- a/internal/command/upgrade/upgrade.go +++ b/internal/command/upgrade/upgrade.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var UpgradeCmd = &cobra.Command{ +var CmdUpgrade = &cobra.Command{ Use: "upgrade", Short: "Upgrade the nunu command.", Long: "Upgrade the nunu command.", diff --git a/internal/command/wire/wire.go b/internal/command/wire/wire.go index 3b1357c..3453507 100644 --- a/internal/command/wire/wire.go +++ b/internal/command/wire/wire.go @@ -12,7 +12,7 @@ import ( "github.com/spf13/cobra" ) -var WireCmd = &cobra.Command{ +var CmdWire = &cobra.Command{ Use: "wire", Short: "nunu wire [wire.go path]", Long: "nunu wire [wire.go path]", diff --git a/internal/pkg/helper/helper.go b/internal/pkg/helper/helper.go index b6afde3..452af15 100644 --- a/internal/pkg/helper/helper.go +++ b/internal/pkg/helper/helper.go @@ -11,8 +11,6 @@ import ( ) func GetProjectName(dir string) string { - - // 打开 go.mod 文件 modFile, err := os.Open(dir + "/go.mod") if err != nil { fmt.Println("go.mod does not exist", err) diff --git a/internal/version/version.go b/internal/version/version.go deleted file mode 100644 index c26dcc5..0000000 --- a/internal/version/version.go +++ /dev/null @@ -1,3 +0,0 @@ -package version - -const Version = "1.0.5" diff --git a/main.go b/main.go index 01664f7..98adda2 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,13 @@ package main import ( + "fmt" "github.com/go-nunu/nunu/cmd/nunu" ) func main() { - nunu.Execute() + err := nunu.Execute() + if err != nil { + fmt.Println("execute error: ", err.Error()) + } }