optimized api new with absolute path like: goctl api new $PWD/xxxx (#67)

* rebase upstream

* rebase

* trim no need line

* trim no need line

* trim no need line

* update doc

* remove update

* optimized api new with absolute path like: goctl api new $PWD/xxxx

* optimized api new with absolute path like: goctl api new $PWD/xxxx

* optimized api new with absolute path like: goctl api new $PWD/xxxx

* optimized api new with absolute path like: goctl api new $PWD/xxxx

Co-authored-by: kingxt <dream4kingxt@163.com>
This commit is contained in:
kingxt
2020-09-13 16:17:21 +08:00
committed by GitHub
parent fb22589cf5
commit 05df86436f
5 changed files with 31 additions and 17 deletions

View File

@@ -6,6 +6,7 @@ import (
"text/template"
"github.com/tal-tech/go-zero/tools/goctl/api/gogen"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/urfave/cli"
)
@@ -28,18 +29,24 @@ service {{.name}}-api {
func NewService(c *cli.Context) error {
args := c.Args()
name := "greet"
dirName := "greet"
if len(args) > 0 {
name = args.First()
dirName = args.First()
}
location := name
err := os.MkdirAll(location, os.ModePerm)
abs, err := filepath.Abs(dirName)
if err != nil {
return err
}
filename := name + ".api"
apiFilePath := filepath.Join(location, filename)
err = util.MkdirIfNotExist(abs)
if err != nil {
return err
}
dirName = filepath.Base(filepath.Clean(abs))
filename := dirName + ".api"
apiFilePath := filepath.Join(abs, filename)
fp, err := os.Create(apiFilePath)
if err != nil {
return err
@@ -48,11 +55,11 @@ func NewService(c *cli.Context) error {
defer fp.Close()
t := template.Must(template.New("template").Parse(apiTemplate))
if err := t.Execute(fp, map[string]string{
"name": name,
"name": dirName,
}); err != nil {
return err
}
err = gogen.DoGenProject(apiFilePath, location)
err = gogen.DoGenProject(apiFilePath, abs)
return err
}