Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjayheaven committed Jan 25, 2024
2 parents 8692e7f + 57fa203 commit b7e7259
Show file tree
Hide file tree
Showing 11 changed files with 296 additions and 106 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ configs/config.yaml
config.yaml
tmp
logs
build/*
build/*
assets/.DS_Store
44 changes: 44 additions & 0 deletions cmd/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cmd

import (
"fmt"
"os"
"os/exec"

"github.com/spf13/cobra"
)

func addSwagger() error {
getCmd := exec.Command("go", "get",
"github.com/swaggo/gin-swagger",
"github.com/swaggo/swag",
"github.com/swaggo/files",
)
getCmd.Stderr = os.Stderr
getCmd.Stdout = os.Stdout
return getCmd.Run()
}

var AddCmd = &cobra.Command{
Use: "add [module name]",
Short: "Get the version of Go-Gin-Boilerplate",
Example: "ggb version",
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println("please enter module name")
fmt.Println("See 'ggb help add'")
return
}

module := args[0]

switch module {
case "swagger":
if err := addSwagger(); err != nil {
fmt.Println(err)
return
}
}

},
}
89 changes: 39 additions & 50 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,22 @@ import (
"errors"
"fmt"
"go-gin-boilerplate/tools"
"html/template"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"
)

func createFile(filePath, templateContent, moduleName string) error {
tmpl, err := template.New("module").Parse(templateContent)
if err != nil {
return err
}

file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()

moduleNamePlural := ""
if strings.HasSuffix(moduleName, "y") {
moduleNamePlural = strings.TrimSuffix(moduleName, "y") + "ies"
} else {
moduleNamePlural = moduleName + "s"
}
moduleNameUpperFirst := strings.ToUpper(string(moduleName[0])) + moduleName[1:]

data := struct {
ModuleName string
ModuleNamePlural string
ModuleNameUpperFirst string
}{
ModuleName: moduleName,
ModuleNamePlural: moduleNamePlural,
ModuleNameUpperFirst: moduleNameUpperFirst,
}

if err := tmpl.Execute(file, data); err != nil {
return err
}

return nil
var newProjectCmd = &cobra.Command{
Use: "project [project name]",
Short: "Create a new project",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
projectName := args[0]
fmt.Printf("Creating a new project: %s\n", projectName)
},
}

// var newProjectCmd = &cobra.Command{
// Use: "project [project name]",
// Short: "Create a new project",
// Args: cobra.ExactArgs(1),
// Run: func(cmd *cobra.Command, args []string) {
// projectName := args[0]
// fmt.Printf("Creating a new project: %s\n", projectName)
// },
// }
var newModuleCmd = &cobra.Command{
Use: "module [module name]",
Short: "Create a new module for the project",
Expand Down Expand Up @@ -86,7 +48,7 @@ var newModuleCmd = &cobra.Command{

for filePath, templateContent := range fileTempaltes {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
err := createFile(filePath, templateContent, moduleName)
err := tools.CreateFileByTmplContent(filePath, templateContent, moduleName)
if err != nil {
fmt.Printf("Error creating file: %v\n", err)
return
Expand All @@ -106,7 +68,12 @@ var NewCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
// Do Stuff Here
if len(args) < 1 {
cmd.Print(errors.New("please enter name"))
cmd.Println(errors.New("please enter name"))
fmt.Println("See 'ggb help new'")
return
}
if args[0] != "module" && args[0] != "project" {
cmd.Println(errors.New("please enter available command"))
fmt.Println("See 'ggb help new'")
return
}
Expand All @@ -115,13 +82,35 @@ var NewCmd = &cobra.Command{
}

func init() {
NewCmd.AddCommand(newModuleCmd)
NewCmd.AddCommand(newModuleCmd, newProjectCmd)
}

func run() {
fmt.Printf("create new module")
fmt.Printf("create new module or new project\n")

// create template like hugo
// todo: ggb new project

// 需要生成的目录 和文件 有哪些

// 和开发有关的

// api,空目录
// assets,不需要,
// build 不需要
// cmd 需要的
// config 需要的
// docs 不需要
// githooks 需要的
// internal 需要的
// scripts 需要
// test 需要
// tools 需要
// web 需要
// .gitignore 需要
// go.mod 需要
// go.sum 需要
// main.go 需要
// makefile 需要

}
3 changes: 0 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ var rootCmd = &cobra.Command{
},
}

var Verbose bool

// You will additionally define flags and handle configuration in your init() function.
func init() {

rootCmd.AddCommand(ServerStartCmd) // add server start command
rootCmd.AddCommand(VersionCmd) // add version command
rootCmd.AddCommand(NewCmd) // add new command
Expand Down
6 changes: 2 additions & 4 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package cmd
import (
"go-gin-boilerplate/configs"
"go-gin-boilerplate/internal/pkg/logger"
"go-gin-boilerplate/internal/pkg/mysql"
"go-gin-boilerplate/internal/pkg/redis"
"go-gin-boilerplate/internal/router"

"net/http"
Expand Down Expand Up @@ -51,9 +49,9 @@ func start() {
EnvConfig := configs.EnvConfig

// connect database
mysql.Connect(&EnvConfig.Mysql)
// mysql.Connect(&EnvConfig.Mysql)
// connect redis
redis.Connect(&EnvConfig.Redis)
// redis.Connect(&EnvConfig.Redis)

// graceful shutdown
server := &http.Server{
Expand Down
1 change: 0 additions & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var VersionCmd = &cobra.Command{
Short: "Get the version of Go-Gin-Boilerplate",
Example: "ggb version",
Run: func(cmd *cobra.Command, args []string) {
// Do Stuff Here
fmt.Printf(`Go-Gin-Boilerplate version: %v`, "v0.0.1")
},
}
21 changes: 1 addition & 20 deletions configs/config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,4 @@

# server
server:
port: ":YOUR_SERVER_PORT"

# jwt
jwt:
secret: YOUR_JWT_SECRET

# mysql
mysql:
host: YOUR_MYSQL_HOST
port: YOUR_MYSQL_PORT
user: YOUR_MYSQL_USER
password: YOUR_MYSQL_PASSWORD
database: YOUR_DATABASE_NAME

# redis
redis:
host: YOUR_REDIS_HOST
port: YOUR_REDIS_PORT
password: YOUR_REDIS_PASSWORD
db: YOUR_REDIS_DB
port: ":8080"
20 changes: 12 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ require (

require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/bytedance/sonic v1.10.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
Expand All @@ -31,13 +33,13 @@ require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/jsonreference v0.20.4 // indirect
github.com/go-openapi/spec v0.20.14 // indirect
github.com/go-openapi/swag v0.22.7 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.6 // indirect
github.com/go-openapi/spec v0.20.4 // indirect
github.com/go-openapi/swag v0.19.15 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.16.0 // indirect
github.com/go-playground/validator/v10 v10.17.0 // indirect
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/snappy v0.0.1 // indirect
Expand All @@ -52,13 +54,14 @@ require (
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/rogpeppe/go-internal v1.11.0 // 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
Expand All @@ -76,11 +79,12 @@ require (
golang.org/x/arch v0.7.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.16.1 // indirect
golang.org/x/tools v0.13.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit b7e7259

Please sign in to comment.