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"
"github.com/logrusorgru/aurora"
"github.com/urfave/cli"
"github.com/tal-tech/go-zero/core/logx"
apiformat "github.com/tal-tech/go-zero/tools/goctl/api/format"
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/urfave/cli"
)
const tmpFile = "%s-%d"
@ -29,6 +30,7 @@ var tmpDir = path.Join(os.TempDir(), "goctl")
func GoCommand(c *cli.Context) error {
apiFile := c.String("api")
dir := c.String("dir")
force := c.Bool("force")
if len(apiFile) == 0 {
return errors.New("missing -api")
}
@ -36,10 +38,10 @@ func GoCommand(c *cli.Context) error {
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)
if err != nil {
return err
@ -54,9 +56,9 @@ func DoGenProject(apiFile, dir string) error {
logx.Must(genConfig(dir))
logx.Must(genMain(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(genRoutes(dir, api))
logx.Must(genRoutes(dir, api, force))
logx.Must(genLogic(dir, api))
// it does not work
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
groups, err := getRoutes(api)
if err != nil {
@ -102,8 +102,10 @@ func genRoutes(dir string, api *spec.ApiSpec) error {
}
filename := path.Join(dir, handlerDir, routesFilename)
if err := util.RemoveOrQuit(filename); err != nil {
return err
if !force {
if err := util.RemoveOrQuit(filename); err != nil {
return err
}
}
fp, created, err := apiutil.MaybeCreateFile(dir, handlerDir, routesFilename)

@ -42,15 +42,17 @@ func BuildTypes(types []spec.Type) (string, error) {
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)
if err != nil {
return err
}
filename := path.Join(dir, typesDir, typesFile)
if err := util.RemoveOrQuit(filename); err != nil {
return err
if !force {
if err := util.RemoveOrQuit(filename); err != nil {
return err
}
}
fp, created, err := apiutil.MaybeCreateFile(dir, typesDir, typesFile)

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

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

Loading…
Cancel
Save