mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-10 16:30:01 +08:00
Merge branches 'goctl-sql-fix' and 'master' of github.com:anqiansong/go-zero into goctl-sql-fix
Conflicts: tools/modelctl/model/modelgen/gen.go
This commit is contained in:
@@ -4,7 +4,7 @@ import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/lang"
|
||||
"github.com/tal-tech/go-zero/core/logx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
@@ -32,8 +32,8 @@ func DartCommand(c *cli.Context) error {
|
||||
dir = dir + "/"
|
||||
}
|
||||
api.Info.Title = strings.Replace(apiFile, ".api", "", -1)
|
||||
lang.Must(genData(dir+"data/", api))
|
||||
lang.Must(genApi(dir+"api/", api))
|
||||
lang.Must(genVars(dir + "vars/"))
|
||||
logx.Must(genData(dir+"data/", api))
|
||||
logx.Must(genApi(dir+"api/", api))
|
||||
logx.Must(genVars(dir + "vars/"))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package gogen
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -13,7 +14,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/logrusorgru/aurora"
|
||||
"github.com/tal-tech/go-zero/core/lang"
|
||||
"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"
|
||||
@@ -44,17 +45,18 @@ func GoCommand(c *cli.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
lang.Must(util.MkdirIfNotExist(dir))
|
||||
lang.Must(genEtc(dir, api))
|
||||
lang.Must(genConfig(dir))
|
||||
lang.Must(genMain(dir, api))
|
||||
lang.Must(genServiceContext(dir, api))
|
||||
lang.Must(genTypes(dir, api))
|
||||
lang.Must(genHandlers(dir, api))
|
||||
lang.Must(genRoutes(dir, api))
|
||||
lang.Must(genLogic(dir, api))
|
||||
logx.Must(util.MkdirIfNotExist(dir))
|
||||
logx.Must(genEtc(dir, api))
|
||||
logx.Must(genConfig(dir))
|
||||
logx.Must(genMain(dir, api))
|
||||
logx.Must(genServiceContext(dir, api))
|
||||
logx.Must(genTypes(dir, api))
|
||||
logx.Must(genHandlers(dir, api))
|
||||
logx.Must(genRoutes(dir, api))
|
||||
logx.Must(genLogic(dir, api))
|
||||
// it does not work
|
||||
format(dir)
|
||||
createGoModFileIfNeed(dir)
|
||||
|
||||
if err := backupAndSweep(apiFile); err != nil {
|
||||
return err
|
||||
@@ -98,7 +100,7 @@ func format(dir string) {
|
||||
cmd := exec.Command("go", "fmt", "./"+dir+"...")
|
||||
_, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
print(err.Error())
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,3 +133,43 @@ func sweep() error {
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func createGoModFileIfNeed(dir string) {
|
||||
absDir, err := filepath.Abs(dir)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var tempPath = absDir
|
||||
var hasGoMod = false
|
||||
for {
|
||||
if tempPath == filepath.Dir(tempPath) {
|
||||
break
|
||||
}
|
||||
tempPath = filepath.Dir(tempPath)
|
||||
if util.FileExists(filepath.Join(tempPath, goModeIdentifier)) {
|
||||
tempPath = filepath.Dir(tempPath)
|
||||
hasGoMod = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasGoMod {
|
||||
gopath := os.Getenv("GOPATH")
|
||||
parent := path.Join(gopath, "src")
|
||||
pos := strings.Index(absDir, parent)
|
||||
if pos < 0 {
|
||||
moduleName := absDir[len(filepath.Dir(absDir))+1:]
|
||||
cmd := exec.Command("go", "mod", "init", moduleName)
|
||||
cmd.Dir = dir
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes())
|
||||
fmt.Printf(outStr + "\n" + errStr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,10 +23,14 @@ func getParentPackage(dir string) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
absDir = strings.ReplaceAll(absDir, `\`, `/`)
|
||||
var rootPath string
|
||||
var tempPath = absDir
|
||||
var hasGoMod = false
|
||||
for {
|
||||
if tempPath == filepath.Dir(tempPath) {
|
||||
break
|
||||
}
|
||||
tempPath = filepath.Dir(tempPath)
|
||||
if goctlutil.FileExists(filepath.Join(tempPath, goModeIdentifier)) {
|
||||
tempPath = filepath.Dir(tempPath)
|
||||
@@ -43,8 +47,7 @@ func getParentPackage(dir string) (string, error) {
|
||||
parent := path.Join(gopath, "src")
|
||||
pos := strings.Index(absDir, parent)
|
||||
if pos < 0 {
|
||||
message := fmt.Sprintf("%s not in gomod project path, or not in GOPATH of %s directory", absDir, gopath)
|
||||
println(message)
|
||||
fmt.Printf("%s not in gomod project path, or not in GOPATH of %s directory\n", absDir, gopath)
|
||||
tempPath = filepath.Dir(absDir)
|
||||
rootPath = absDir[len(tempPath)+1:]
|
||||
} else {
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/logrusorgru/aurora"
|
||||
"github.com/tal-tech/go-zero/core/lang"
|
||||
"github.com/tal-tech/go-zero/core/logx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
"github.com/urfave/cli"
|
||||
@@ -36,9 +36,9 @@ func JavaCommand(c *cli.Context) error {
|
||||
packetName = packetName[:len(packetName)-4]
|
||||
}
|
||||
|
||||
lang.Must(util.MkdirIfNotExist(dir))
|
||||
lang.Must(genPacket(dir, packetName, api))
|
||||
lang.Must(genComponents(dir, packetName, api))
|
||||
logx.Must(util.MkdirIfNotExist(dir))
|
||||
logx.Must(genPacket(dir, packetName, api))
|
||||
logx.Must(genComponents(dir, packetName, api))
|
||||
|
||||
fmt.Println(aurora.Green("Done."))
|
||||
return nil
|
||||
|
||||
@@ -175,7 +175,7 @@ func formatFile(tmplBytes *bytes.Buffer, file *os.File) {
|
||||
builder.WriteString(scanner.Text() + "\n")
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
println(err)
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -268,10 +268,12 @@ func genType(writer io.Writer, tp spec.Type) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
writeBreakline(writer)
|
||||
writeIndent(writer, 1)
|
||||
genGetSet(writer, tp, 2)
|
||||
writeIndent(writer, 1)
|
||||
fmt.Fprintln(writer, "}")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/lang"
|
||||
"github.com/tal-tech/go-zero/core/logx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
|
||||
)
|
||||
|
||||
@@ -14,8 +14,8 @@ func main() {
|
||||
}
|
||||
|
||||
p, err := parser.NewParser(os.Args[1])
|
||||
lang.Must(err)
|
||||
logx.Must(err)
|
||||
api, err := p.Parse()
|
||||
lang.Must(err)
|
||||
logx.Must(err)
|
||||
fmt.Println(api)
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/logrusorgru/aurora"
|
||||
"github.com/tal-tech/go-zero/core/lang"
|
||||
"github.com/tal-tech/go-zero/core/logx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
"github.com/urfave/cli"
|
||||
@@ -34,9 +34,9 @@ func TsCommand(c *cli.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
lang.Must(util.MkdirIfNotExist(dir))
|
||||
lang.Must(genHandler(dir, webApi, caller, api, unwrapApi))
|
||||
lang.Must(genComponents(dir, api))
|
||||
logx.Must(util.MkdirIfNotExist(dir))
|
||||
logx.Must(genHandler(dir, webApi, caller, api, unwrapApi))
|
||||
logx.Must(genComponents(dir, api))
|
||||
|
||||
fmt.Println(aurora.Green("Done."))
|
||||
return nil
|
||||
|
||||
@@ -156,7 +156,7 @@ func paramsForRoute(route spec.Route, prefixForType func(string) string) string
|
||||
hasBody := hasRequestBody(route)
|
||||
rt, err := goTypeToTs(route.RequestType.Name, prefixForType)
|
||||
if err != nil {
|
||||
println(err.Error())
|
||||
fmt.Println(err.Error())
|
||||
return ""
|
||||
}
|
||||
if hasParams && hasBody {
|
||||
|
||||
@@ -8,13 +8,13 @@ import (
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/lang"
|
||||
"github.com/tal-tech/go-zero/core/logx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
)
|
||||
|
||||
func MaybeCreateFile(dir, subdir, file string) (fp *os.File, created bool, err error) {
|
||||
lang.Must(util.MkdirIfNotExist(path.Join(dir, subdir)))
|
||||
logx.Must(util.MkdirIfNotExist(path.Join(dir, subdir)))
|
||||
fpath := path.Join(dir, subdir, file)
|
||||
if util.FileExists(fpath) {
|
||||
fmt.Printf("%s exists, ignored generation\n", fpath)
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
|
||||
"github.com/tal-tech/go-zero/core/conf"
|
||||
"github.com/tal-tech/go-zero/core/hash"
|
||||
"github.com/tal-tech/go-zero/core/lang"
|
||||
"github.com/tal-tech/go-zero/core/logx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/update/config"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
@@ -56,5 +55,5 @@ func main() {
|
||||
|
||||
fs := http.FileServer(http.Dir(c.FileDir))
|
||||
http.Handle(c.FilePath, http.StripPrefix(c.FilePath, forChksumHandler(path.Join(c.FileDir, filename), fs)))
|
||||
lang.Must(http.ListenAndServe(c.ListenOn, nil))
|
||||
logx.Must(http.ListenAndServe(c.ListenOn, nil))
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
||||
@@ -44,16 +43,3 @@ func PathFromGoSrc() (string, error) {
|
||||
// skip slash
|
||||
return dir[len(parent)+1:], nil
|
||||
}
|
||||
|
||||
func GetParentPackage(dir string) (string, error) {
|
||||
absDir, err := filepath.Abs(dir)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
pos := strings.Index(absDir, vars.ProjectName)
|
||||
if pos < 0 {
|
||||
return "", fmt.Errorf("error dir:[%s],please make sure that your project is in the %s directory", vars.ProjectName, dir)
|
||||
}
|
||||
|
||||
return absDir[pos:], nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user