optimize: api generating for idea plugin (#68)

* add flag: force to generate api

* add flag: force to generate api

* format api template

* Revert "format api template"
master
Keson 4 years ago committed by GitHub
parent 598ff6d0fc
commit e6f8e0e8c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -14,12 +14,13 @@ import (
"time" "time"
"github.com/logrusorgru/aurora" "github.com/logrusorgru/aurora"
"github.com/urfave/cli"
"github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/core/logx"
apiformat "github.com/tal-tech/go-zero/tools/goctl/api/format" apiformat "github.com/tal-tech/go-zero/tools/goctl/api/format"
"github.com/tal-tech/go-zero/tools/goctl/api/parser" "github.com/tal-tech/go-zero/tools/goctl/api/parser"
apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util" apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
"github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/urfave/cli"
) )
const tmpFile = "%s-%d" const tmpFile = "%s-%d"
@ -29,6 +30,7 @@ var tmpDir = path.Join(os.TempDir(), "goctl")
func GoCommand(c *cli.Context) error { func GoCommand(c *cli.Context) error {
apiFile := c.String("api") apiFile := c.String("api")
dir := c.String("dir") dir := c.String("dir")
force := c.Bool("force")
if len(apiFile) == 0 { if len(apiFile) == 0 {
return errors.New("missing -api") return errors.New("missing -api")
} }
@ -36,10 +38,10 @@ func GoCommand(c *cli.Context) error {
return errors.New("missing -dir") return errors.New("missing -dir")
} }
return DoGenProject(apiFile, dir) return DoGenProject(apiFile, dir, force)
} }
func DoGenProject(apiFile, dir string) error { func DoGenProject(apiFile, dir string, force bool) error {
p, err := parser.NewParser(apiFile) p, err := parser.NewParser(apiFile)
if err != nil { if err != nil {
return err return err
@ -54,9 +56,9 @@ func DoGenProject(apiFile, dir string) error {
logx.Must(genConfig(dir)) logx.Must(genConfig(dir))
logx.Must(genMain(dir, api)) logx.Must(genMain(dir, api))
logx.Must(genServiceContext(dir, api)) logx.Must(genServiceContext(dir, api))
logx.Must(genTypes(dir, api)) logx.Must(genTypes(dir, api, force))
logx.Must(genHandlers(dir, api)) logx.Must(genHandlers(dir, api))
logx.Must(genRoutes(dir, api)) logx.Must(genRoutes(dir, api, force))
logx.Must(genLogic(dir, api)) logx.Must(genLogic(dir, api))
// it does not work // it does not work
format(dir) format(dir)

@ -60,7 +60,7 @@ type (
} }
) )
func genRoutes(dir string, api *spec.ApiSpec) error { func genRoutes(dir string, api *spec.ApiSpec, force bool) error {
var builder strings.Builder var builder strings.Builder
groups, err := getRoutes(api) groups, err := getRoutes(api)
if err != nil { if err != nil {
@ -102,9 +102,11 @@ func genRoutes(dir string, api *spec.ApiSpec) error {
} }
filename := path.Join(dir, handlerDir, routesFilename) filename := path.Join(dir, handlerDir, routesFilename)
if !force {
if err := util.RemoveOrQuit(filename); err != nil { if err := util.RemoveOrQuit(filename); err != nil {
return err return err
} }
}
fp, created, err := apiutil.MaybeCreateFile(dir, handlerDir, routesFilename) fp, created, err := apiutil.MaybeCreateFile(dir, handlerDir, routesFilename)
if err != nil { if err != nil {

@ -42,16 +42,18 @@ func BuildTypes(types []spec.Type) (string, error) {
return builder.String(), nil return builder.String(), nil
} }
func genTypes(dir string, api *spec.ApiSpec) error { func genTypes(dir string, api *spec.ApiSpec, force bool) error {
val, err := BuildTypes(api.Types) val, err := BuildTypes(api.Types)
if err != nil { if err != nil {
return err return err
} }
filename := path.Join(dir, typesDir, typesFile) filename := path.Join(dir, typesDir, typesFile)
if !force {
if err := util.RemoveOrQuit(filename); err != nil { if err := util.RemoveOrQuit(filename); err != nil {
return err return err
} }
}
fp, created, err := apiutil.MaybeCreateFile(dir, typesDir, typesFile) fp, created, err := apiutil.MaybeCreateFile(dir, typesDir, typesFile)
if err != nil { if err != nil {

@ -60,6 +60,6 @@ func NewService(c *cli.Context) error {
return err return err
} }
err = gogen.DoGenProject(apiFilePath, abs) err = gogen.DoGenProject(apiFilePath, abs, true)
return err return err
} }

@ -96,6 +96,10 @@ var (
Name: "api", Name: "api",
Usage: "the api file", Usage: "the api file",
}, },
cli.BoolFlag{
Name: "force",
Usage: "force override the exist files",
},
}, },
Action: gogen.GoCommand, Action: gogen.GoCommand,
}, },

Loading…
Cancel
Save