From e6f8e0e8c3d41fe05206cd19b14b9636468bec9a Mon Sep 17 00:00:00 2001 From: Keson Date: Mon, 14 Sep 2020 17:12:31 +0800 Subject: [PATCH] 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" --- tools/goctl/api/gogen/gen.go | 12 +++++++----- tools/goctl/api/gogen/genroutes.go | 8 +++++--- tools/goctl/api/gogen/gentypes.go | 8 +++++--- tools/goctl/api/new/newservice.go | 2 +- tools/goctl/goctl.go | 4 ++++ 5 files changed, 22 insertions(+), 12 deletions(-) diff --git a/tools/goctl/api/gogen/gen.go b/tools/goctl/api/gogen/gen.go index 8361572e..d1283d07 100644 --- a/tools/goctl/api/gogen/gen.go +++ b/tools/goctl/api/gogen/gen.go @@ -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) diff --git a/tools/goctl/api/gogen/genroutes.go b/tools/goctl/api/gogen/genroutes.go index e431f3d1..20783095 100644 --- a/tools/goctl/api/gogen/genroutes.go +++ b/tools/goctl/api/gogen/genroutes.go @@ -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) diff --git a/tools/goctl/api/gogen/gentypes.go b/tools/goctl/api/gogen/gentypes.go index 3dc94d0c..40980187 100644 --- a/tools/goctl/api/gogen/gentypes.go +++ b/tools/goctl/api/gogen/gentypes.go @@ -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) diff --git a/tools/goctl/api/new/newservice.go b/tools/goctl/api/new/newservice.go index 632e976b..b180cf56 100644 --- a/tools/goctl/api/new/newservice.go +++ b/tools/goctl/api/new/newservice.go @@ -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 } diff --git a/tools/goctl/goctl.go b/tools/goctl/goctl.go index fbb7fc2d..e51b09c3 100644 --- a/tools/goctl/goctl.go +++ b/tools/goctl/goctl.go @@ -96,6 +96,10 @@ var ( Name: "api", Usage: "the api file", }, + cli.BoolFlag{ + Name: "force", + Usage: "force override the exist files", + }, }, Action: gogen.GoCommand, },