feat: optimizing code

This commit is contained in:
chris
2023-07-23 08:54:29 +08:00
parent 919fc96268
commit 4d88a7809e
13 changed files with 75 additions and 40 deletions

View File

@@ -166,23 +166,49 @@ Nunu采用了经典的分层架构。同时为了更好地实现模块化和
go install github.com/go-nunu/nunu@latest 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 目录配置到环境变量中即可 > tips: 如果`go install`成功却提示找不到nunu命令这是因为环境变量没有配置可以把 GOBIN 目录配置到环境变量中即可
### 创建新项目 ### 创建新项目
您可以使用以下命令创建一个新的Golang项目 您可以使用以下命令创建一个新的Golang项目
```bash ```bash
// 推荐新用户选择Advanced Layout
nunu new projectName 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-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项目结构。 此命令将创建一个名为`projectName`的目录并在其中生成一个优雅的Golang项目结构。
### 创建组件 ### 创建组件

View File

@@ -2,37 +2,37 @@ package nunu
import ( import (
"fmt" "fmt"
"github.com/go-nunu/nunu/config"
"github.com/go-nunu/nunu/internal/command/create" "github.com/go-nunu/nunu/internal/command/create"
"github.com/go-nunu/nunu/internal/command/project" "github.com/go-nunu/nunu/internal/command/project"
"github.com/go-nunu/nunu/internal/command/run" "github.com/go-nunu/nunu/internal/command/run"
"github.com/go-nunu/nunu/internal/command/upgrade" "github.com/go-nunu/nunu/internal/command/upgrade"
"github.com/go-nunu/nunu/internal/command/wire" "github.com/go-nunu/nunu/internal/command/wire"
"github.com/go-nunu/nunu/internal/version"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var rootCmd = &cobra.Command{ var CmdRoot = &cobra.Command{
Use: "nunu", Use: "nunu",
Example: "nunu new demo-api", 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", 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() { func init() {
rootCmd.AddCommand(project.NewCmd) CmdRoot.AddCommand(project.CmdNew)
rootCmd.AddCommand(create.CreateCmd) CmdRoot.AddCommand(create.CmdCreate)
rootCmd.AddCommand(wire.WireCmd) CmdRoot.AddCommand(wire.CmdWire)
rootCmd.AddCommand(run.RunCmd) CmdRoot.AddCommand(run.CmdRun)
rootCmd.AddCommand(upgrade.UpgradeCmd) CmdRoot.AddCommand(upgrade.CmdUpgrade)
create.CreateCmd.AddCommand(create.CreateHandlerCmd) create.CmdCreate.AddCommand(create.CmdCreateHandler)
create.CreateCmd.AddCommand(create.CreateServiceCmd) create.CmdCreate.AddCommand(create.CmdCreateService)
create.CreateCmd.AddCommand(create.CreateRepositoryCmd) create.CmdCreate.AddCommand(create.CmdCreateRepository)
create.CreateCmd.AddCommand(create.CreateModelCmd) create.CmdCreate.AddCommand(create.CmdCreateModel)
create.CreateCmd.AddCommand(create.CreateAllCmd) create.CmdCreate.AddCommand(create.CmdCreateAll)
} }
// Execute executes the root command. // Execute executes the root command.
func Execute() error { func Execute() error {
return rootCmd.Execute() return CmdRoot.Execute()
} }

View File

@@ -1,6 +1,7 @@
package config package config
const ( const (
Version = "1.0.5"
WireCmd = "github.com/google/wire/cmd/wire@latest" WireCmd = "github.com/google/wire/cmd/wire@latest"
NunuCmd = "github.com/go-nunu/nunu@latest" NunuCmd = "github.com/go-nunu/nunu@latest"
RepoBase = "https://github.com/go-nunu/nunu-layout-base.git" RepoBase = "https://github.com/go-nunu/nunu-layout-base.git"

View File

@@ -19,6 +19,13 @@ Nunu 是一个基于 Golang 的应用脚手架,它可以帮助您快速构建
go install github.com/go-nunu/nunu@latest 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 目录配置到环境变量中即可 > tips: 如果`go install`成功却提示找不到nunu命令这是因为环境变量没有配置可以把 GOBIN 目录配置到环境变量中即可
## 创建新项目 ## 创建新项目
@@ -27,6 +34,8 @@ go install github.com/go-nunu/nunu@latest
```bash ```bash
nunu new projectName nunu new projectName
// 推荐新用户选择Advanced Layout
``` ```
此命令将创建一个名为 `projectName` 的目录,并在其中生成一个优雅的 Golang 项目结构。 此命令将创建一个名为 `projectName` 的目录,并在其中生成一个优雅的 Golang 项目结构。
@@ -35,10 +44,12 @@ nunu new projectName
`nunu new`默认拉取github源你也可以使用国内加速仓库 `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-advanced.git
// 使用基础模板
nunu new projectName -r https://gitee.com/go-nunu/nunu-layout-basic.git
``` ```

View File

@@ -27,7 +27,7 @@ func NewCreate() *Create {
return &Create{} return &Create{}
} }
var CreateCmd = &cobra.Command{ var CmdCreate = &cobra.Command{
Use: "create [type] [handler-name]", Use: "create [type] [handler-name]",
Short: "Create a new handler/service/repository/model", Short: "Create a new handler/service/repository/model",
Example: "nunu create handler user", Example: "nunu create handler user",
@@ -36,35 +36,35 @@ var CreateCmd = &cobra.Command{
}, },
} }
var CreateHandlerCmd = &cobra.Command{ var CmdCreateHandler = &cobra.Command{
Use: "handler", Use: "handler",
Short: "Create a new handler", Short: "Create a new handler",
Example: "nunu create handler user", Example: "nunu create handler user",
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Run: runCreate, Run: runCreate,
} }
var CreateServiceCmd = &cobra.Command{ var CmdCreateService = &cobra.Command{
Use: "service", Use: "service",
Short: "Create a new service", Short: "Create a new service",
Example: "nunu create service user", Example: "nunu create service user",
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Run: runCreate, Run: runCreate,
} }
var CreateRepositoryCmd = &cobra.Command{ var CmdCreateRepository = &cobra.Command{
Use: "repository", Use: "repository",
Short: "Create a new repository", Short: "Create a new repository",
Example: "nunu create repository user", Example: "nunu create repository user",
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Run: runCreate, Run: runCreate,
} }
var CreateModelCmd = &cobra.Command{ var CmdCreateModel = &cobra.Command{
Use: "model", Use: "model",
Short: "Create a new model", Short: "Create a new model",
Example: "nunu create model user", Example: "nunu create model user",
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Run: runCreate, Run: runCreate,
} }
var CreateAllCmd = &cobra.Command{ var CmdCreateAll = &cobra.Command{
Use: "all", Use: "all",
Short: "Create a new handler & service & repository & model", Short: "Create a new handler & service & repository & model",
Example: "nunu create all user", Example: "nunu create all user",
@@ -127,7 +127,6 @@ func (c *Create) genFile() {
} }
func createFile(dirPath string, filename string) *os.File { func createFile(dirPath string, filename string) *os.File {
filePath := dirPath + filename filePath := dirPath + filename
// 创建文件夹
err := os.MkdirAll(dirPath, os.ModePerm) err := os.MkdirAll(dirPath, os.ModePerm)
if err != nil { if err != nil {
log.Fatalf("Failed to create dir %s: %v", dirPath, err) 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 { if stat != nil {
return nil return nil
} }
// 创建文件
file, err := os.Create(filePath) file, err := os.Create(filePath)
if err != nil { if err != nil {
log.Fatalf("Failed to create file %s: %v", filePath, err) log.Fatalf("Failed to create file %s: %v", filePath, err)

View File

@@ -18,7 +18,7 @@ type Project struct {
ProjectName string `survey:"name"` ProjectName string `survey:"name"`
} }
var NewCmd = &cobra.Command{ var CmdNew = &cobra.Command{
Use: "new", Use: "new",
Example: "nunu new demo-api", Example: "nunu new demo-api",
Short: "create a new project.", Short: "create a new project.",
@@ -30,7 +30,7 @@ var (
) )
func init() { 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 { func NewProject() *Project {
@@ -178,7 +178,7 @@ func (p *Project) modTidy() error {
func (p *Project) rmGit() { func (p *Project) rmGit() {
os.RemoveAll(p.ProjectName + "/.git") os.RemoveAll(p.ProjectName + "/.git")
} }
func (p Project) installWire() { func (p *Project) installWire() {
fmt.Printf("go install %s\n", config.WireCmd) fmt.Printf("go install %s\n", config.WireCmd)
cmd := exec.Command("go", "install", config.WireCmd) cmd := exec.Command("go", "install", config.WireCmd)
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout

View File

@@ -24,7 +24,7 @@ var quit = make(chan os.Signal, 1)
type Run struct { type Run struct {
} }
var RunCmd = &cobra.Command{ var CmdRun = &cobra.Command{
Use: "run", Use: "run",
Short: "nunu run [main.go path]", Short: "nunu run [main.go path]",
Long: "nunu run [main.go path]", Long: "nunu run [main.go path]",

View File

@@ -25,7 +25,7 @@ var quit = make(chan os.Signal, 1)
type Run struct { type Run struct {
} }
var RunCmd = &cobra.Command{ var CmdRun = &cobra.Command{
Use: "run", Use: "run",
Short: "nunu run [main.go path]", Short: "nunu run [main.go path]",
Long: "nunu run [main.go path]", Long: "nunu run [main.go path]",

View File

@@ -10,7 +10,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var UpgradeCmd = &cobra.Command{ var CmdUpgrade = &cobra.Command{
Use: "upgrade", Use: "upgrade",
Short: "Upgrade the nunu command.", Short: "Upgrade the nunu command.",
Long: "Upgrade the nunu command.", Long: "Upgrade the nunu command.",

View File

@@ -12,7 +12,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var WireCmd = &cobra.Command{ var CmdWire = &cobra.Command{
Use: "wire", Use: "wire",
Short: "nunu wire [wire.go path]", Short: "nunu wire [wire.go path]",
Long: "nunu wire [wire.go path]", Long: "nunu wire [wire.go path]",

View File

@@ -11,8 +11,6 @@ import (
) )
func GetProjectName(dir string) string { func GetProjectName(dir string) string {
// 打开 go.mod 文件
modFile, err := os.Open(dir + "/go.mod") modFile, err := os.Open(dir + "/go.mod")
if err != nil { if err != nil {
fmt.Println("go.mod does not exist", err) fmt.Println("go.mod does not exist", err)

View File

@@ -1,3 +0,0 @@
package version
const Version = "1.0.5"

View File

@@ -1,9 +1,13 @@
package main package main
import ( import (
"fmt"
"github.com/go-nunu/nunu/cmd/nunu" "github.com/go-nunu/nunu/cmd/nunu"
) )
func main() { func main() {
nunu.Execute() err := nunu.Execute()
if err != nil {
fmt.Println("execute error: ", err.Error())
}
} }