Skip to content

Commit

Permalink
update README
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinkuo committed Oct 10, 2023
1 parent 5305a14 commit b38db93
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 75 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
# crud
## intro
crud is a tool for generate api defined files from datasource.
datasource supported:

**datasource supported:**
1. mysql

api files supported:
**api files supported:**
1. proto3
2. zero api

## install
```bash
go install github.com/kelvinkuo/crud@latest
```

## usage
...

Expand All @@ -20,6 +26,7 @@ api files supported:
- [X] args parser
- [X] project layout
- [X] api file support
- [ ] fully test
- [ ] document
- [ ] comment
- [ ] README
83 changes: 10 additions & 73 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package cmd

import (
"fmt"
"log"
"os"
"strings"

"github.com/kelvinkuo/crud/internal/consts"
"github.com/kelvinkuo/crud/internal/core/convert/convertfactory"
"github.com/kelvinkuo/crud/internal/core/convert/filter"
"github.com/kelvinkuo/crud/internal/db/dbfactory"
"github.com/kelvinkuo/crud/internal/protocol/protocolfactory"
"github.com/kelvinkuo/crud/internal/core"
"github.com/spf13/cobra"
)

Expand All @@ -33,14 +27,16 @@ var rootCmd = &cobra.Command{
Use: "crud",
Short: "generate CRUD defined content from datasource",
Long: `Generate CRUD defined content from datasource.
Database supported: mysql
Format Support: proto3, zero api
Full example:
crud -f proto3 --source "root:123456@tcp(127.0.0.1:3306)/shop" -m "add,delete,update,info,search" -c "created_at,updated_at,deleted_at" -t * -s shop -go_package shop -package shop
Common example:
supported database : mysql
supported format: proto3, zero api
example simple:
crud -f zeroapi --source "root:123456@tcp(127.0.0.1:3306)/shop" -s shop
example full:
crud -f proto3 --source "root:123456@tcp(127.0.0.1:3306)/shop" -m "add,delete,update,info,list,search" -c "created_at,updated_at,deleted_at" -t * -s shop -go_package shop -package shop
`,
Run: func(cmd *cobra.Command, args []string) {
// flags
Expand All @@ -56,7 +52,7 @@ crud -f zeroapi --source "root:123456@tcp(127.0.0.1:3306)/shop" -s shop
pkg = service
}
// generate
generate()
core.Generate(datasource, format, service, pkg, goPackage, tableNames, ignoreCols, methods)
},
}

Expand All @@ -71,70 +67,11 @@ func init() {
rootCmd.Flags().SortFlags = false
rootCmd.Flags().StringVarP(&format, "format", "f", "proto3", "output format support proto3, zero")
rootCmd.Flags().StringVar(&datasource, "source", "", "datasource example: root:123456@tcp(127.0.0.1:3306)/shop")
rootCmd.Flags().StringVarP(&methodStr, "method", "m", "add,delete,update,info,search", "methods separated by \",\"")
rootCmd.Flags().StringVarP(&methodStr, "method", "m", "add,delete,update,info,list,search", "methods separated by \",\"")
rootCmd.Flags().StringVarP(&ignoreColStr, "ignore_cols", "c", "create_at,create_time,created_at,update_at,update_time,updated_at",
"columns ignored separated by \",\"")
rootCmd.Flags().StringVarP(&tableStr, "table", "t", "*", "tables separated by \",\" or \"*\" for all tables")
rootCmd.Flags().StringVarP(&service, "service", "s", "", "service name")
rootCmd.Flags().StringVar(&goPackage, "go_package", "", "go_package used in proto3 (default the same as service)")
rootCmd.Flags().StringVarP(&pkg, "package", "p", "", "package used in proto3 (default the same as service)")
}

func generate() {
// init db
dbInstance := dbfactory.NewDB(consts.MYSQL)
err := dbInstance.Init(datasource)
if err != nil {
log.Fatal("db init error ", err)
}
if len(tableNames) == 0 {
tableNames = dbInstance.AllTableNames()
}
tables, err := dbInstance.GetTables(tableNames)
if err != nil {
log.Fatal(err)
}

// init protocol
protocolType := format
p := protocolfactory.NewProtocol(protocolType, pkg, goPackage)
convertor := convertfactory.NewConverter(protocolType)
for _, method := range methods {
convertor.AddItemCreator(convertfactory.NewItemCreator(protocolType, method))
}
convertor.AddColumnFilter(filter.NewStringFilter(ignoreCols))

for _, table := range tables {
msgList, err := convertor.CreateMetaMessage(table)
if err != nil {
log.Fatal(err)
}
for _, msg := range msgList {
err = p.AddMessage(msg)
if err != nil {
log.Fatal(err)
}
}

items, err := convertor.CreateItems(table, service)
if err != nil {
log.Fatal(err)
}
for _, item := range items {
err = p.AddItem(item)
if err != nil {
log.Fatal(err)
}
err = p.AddMessage(item.Request())
if err != nil {
log.Fatal(err)
}
err = p.AddMessage(item.Response())
if err != nil {
log.Fatal(err)
}
}
}

fmt.Print(p.String())
}
70 changes: 70 additions & 0 deletions internal/core/core.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package core

import (
"fmt"
"log"

"github.com/kelvinkuo/crud/internal/consts"
"github.com/kelvinkuo/crud/internal/core/convert/convertfactory"
"github.com/kelvinkuo/crud/internal/core/convert/filter"
"github.com/kelvinkuo/crud/internal/db/dbfactory"
"github.com/kelvinkuo/crud/internal/protocol/protocolfactory"
)

func Generate(datasource, protocolType, service, pkg, goPackage string, tableNames, ignoreCols, methods []string) {
// init db
dbInstance := dbfactory.NewDB(consts.MYSQL)
err := dbInstance.Init(datasource)
if err != nil {
log.Fatal("db init error ", err)
}
if len(tableNames) == 0 {
tableNames = dbInstance.AllTableNames()
}
tables, err := dbInstance.GetTables(tableNames)
if err != nil {
log.Fatal(err)
}

// init protocol
p := protocolfactory.NewProtocol(protocolType, pkg, goPackage)
convertor := convertfactory.NewConverter(protocolType)
for _, method := range methods {
convertor.AddItemCreator(convertfactory.NewItemCreator(protocolType, method))
}
convertor.AddColumnFilter(filter.NewStringFilter(ignoreCols))

for _, table := range tables {
msgList, err := convertor.CreateMetaMessage(table)
if err != nil {
log.Fatal(err)
}
for _, msg := range msgList {
err = p.AddMessage(msg)
if err != nil {
log.Fatal(err)
}
}

items, err := convertor.CreateItems(table, service)
if err != nil {
log.Fatal(err)
}
for _, item := range items {
err = p.AddItem(item)
if err != nil {
log.Fatal(err)
}
err = p.AddMessage(item.Request())
if err != nil {
log.Fatal(err)
}
err = p.AddMessage(item.Response())
if err != nil {
log.Fatal(err)
}
}
}

fmt.Print(p.String())
}

0 comments on commit b38db93

Please sign in to comment.