From 1c24e715680e430c1433937bd880a09b4e0c5bcd Mon Sep 17 00:00:00 2001 From: kevin Date: Thu, 27 Aug 2020 11:44:35 +0800 Subject: [PATCH] use yaml, and detect go.mod in current dir --- tools/goctl/api/gogen/gen.go | 8 ++++--- tools/goctl/api/gogen/genetc.go | 11 +++++----- tools/goctl/api/gogen/genmain.go | 2 +- tools/goctl/api/gogen/util.go | 36 +++++++++++++++++++------------- 4 files changed, 32 insertions(+), 25 deletions(-) diff --git a/tools/goctl/api/gogen/gen.go b/tools/goctl/api/gogen/gen.go index c20b4930..95a2a00f 100644 --- a/tools/goctl/api/gogen/gen.go +++ b/tools/goctl/api/gogen/gen.go @@ -143,15 +143,17 @@ func createGoModFileIfNeed(dir string) { var tempPath = absDir var hasGoMod = false for { - if tempPath == filepath.Dir(tempPath) { + if util.FileExists(filepath.Join(tempPath, goModeIdentifier)) { + hasGoMod = true break } + tempPath = filepath.Dir(tempPath) - if util.FileExists(filepath.Join(tempPath, goModeIdentifier)) { - hasGoMod = true + if tempPath == filepath.Dir(tempPath) { break } } + if !hasGoMod { gopath := os.Getenv("GOPATH") parent := path.Join(gopath, "src") diff --git a/tools/goctl/api/gogen/genetc.go b/tools/goctl/api/gogen/genetc.go index 7e6907f0..55082b3e 100644 --- a/tools/goctl/api/gogen/genetc.go +++ b/tools/goctl/api/gogen/genetc.go @@ -13,15 +13,14 @@ import ( const ( defaultPort = 8888 etcDir = "etc" - etcTemplate = `{ - "Name": "{{.serviceName}}", - "Host": "{{.host}}", - "Port": {{.port}} -}` + etcTemplate = `Name: {{.serviceName}} +Host: {{.host}} +Port: {{.port}} +` ) func genEtc(dir string, api *spec.ApiSpec) error { - fp, created, err := util.MaybeCreateFile(dir, etcDir, fmt.Sprintf("%s.json", api.Service.Name)) + fp, created, err := util.MaybeCreateFile(dir, etcDir, fmt.Sprintf("%s.yaml", api.Service.Name)) if err != nil { return err } diff --git a/tools/goctl/api/gogen/genmain.go b/tools/goctl/api/gogen/genmain.go index 1426489c..9d08b7f0 100644 --- a/tools/goctl/api/gogen/genmain.go +++ b/tools/goctl/api/gogen/genmain.go @@ -21,7 +21,7 @@ import ( {{.importPackages}} ) -var configFile = flag.String("f", "etc/{{.serviceName}}.json", "the config file") +var configFile = flag.String("f", "etc/{{.serviceName}}.yaml", "the config file") func main() { flag.Parse() diff --git a/tools/goctl/api/gogen/util.go b/tools/goctl/api/gogen/util.go index 1306d1b8..1e8789b2 100644 --- a/tools/goctl/api/gogen/util.go +++ b/tools/goctl/api/gogen/util.go @@ -28,32 +28,38 @@ func getParentPackage(dir string) (string, error) { 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) rootPath = absDir[len(tempPath)+1:] hasGoMod = true break } + + if tempPath == filepath.Dir(tempPath) { + break + } + + tempPath = filepath.Dir(tempPath) if tempPath == string(filepath.Separator) { break } } - if !hasGoMod { - gopath := os.Getenv("GOPATH") - parent := path.Join(gopath, "src") - pos := strings.Index(absDir, parent) - if pos < 0 { - 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 { - rootPath = absDir[len(parent)+1:] - } + + if hasGoMod { + return rootPath, nil } + + gopath := os.Getenv("GOPATH") + parent := path.Join(gopath, "src") + pos := strings.Index(absDir, parent) + if pos < 0 { + fmt.Printf("%s not in go.mod project path, or not in GOPATH of %s directory\n", absDir, gopath) + tempPath = filepath.Dir(absDir) + rootPath = absDir[len(tempPath)+1:] + } else { + rootPath = absDir[len(parent)+1:] + } + return rootPath, nil }