Browse Source

use yaml, and detect go.mod in current dir

kevin 4 years ago
parent
commit
1c24e71568

+ 5 - 3
tools/goctl/api/gogen/gen.go

@@ -143,15 +143,17 @@ func createGoModFileIfNeed(dir string) {
 	var tempPath = absDir
 	var tempPath = absDir
 	var hasGoMod = false
 	var hasGoMod = false
 	for {
 	for {
-		if tempPath == filepath.Dir(tempPath) {
+		if util.FileExists(filepath.Join(tempPath, goModeIdentifier)) {
+			hasGoMod = true
 			break
 			break
 		}
 		}
+
 		tempPath = filepath.Dir(tempPath)
 		tempPath = filepath.Dir(tempPath)
-		if util.FileExists(filepath.Join(tempPath, goModeIdentifier)) {
-			hasGoMod = true
+		if tempPath == filepath.Dir(tempPath) {
 			break
 			break
 		}
 		}
 	}
 	}
+
 	if !hasGoMod {
 	if !hasGoMod {
 		gopath := os.Getenv("GOPATH")
 		gopath := os.Getenv("GOPATH")
 		parent := path.Join(gopath, "src")
 		parent := path.Join(gopath, "src")

+ 5 - 6
tools/goctl/api/gogen/genetc.go

@@ -13,15 +13,14 @@ import (
 const (
 const (
 	defaultPort = 8888
 	defaultPort = 8888
 	etcDir      = "etc"
 	etcDir      = "etc"
-	etcTemplate = `{
-    "Name": "{{.serviceName}}",
-    "Host": "{{.host}}",
-    "Port": {{.port}}
-}`
+	etcTemplate = `Name: {{.serviceName}}
+Host: {{.host}}
+Port: {{.port}}
+`
 )
 )
 
 
 func genEtc(dir string, api *spec.ApiSpec) error {
 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 {
 	if err != nil {
 		return err
 		return err
 	}
 	}

+ 1 - 1
tools/goctl/api/gogen/genmain.go

@@ -21,7 +21,7 @@ import (
 	{{.importPackages}}
 	{{.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() {
 func main() {
 	flag.Parse()
 	flag.Parse()

+ 21 - 15
tools/goctl/api/gogen/util.go

@@ -28,32 +28,38 @@ func getParentPackage(dir string) (string, error) {
 	var tempPath = absDir
 	var tempPath = absDir
 	var hasGoMod = false
 	var hasGoMod = false
 	for {
 	for {
-		if tempPath == filepath.Dir(tempPath) {
-			break
-		}
-		tempPath = filepath.Dir(tempPath)
 		if goctlutil.FileExists(filepath.Join(tempPath, goModeIdentifier)) {
 		if goctlutil.FileExists(filepath.Join(tempPath, goModeIdentifier)) {
 			tempPath = filepath.Dir(tempPath)
 			tempPath = filepath.Dir(tempPath)
 			rootPath = absDir[len(tempPath)+1:]
 			rootPath = absDir[len(tempPath)+1:]
 			hasGoMod = true
 			hasGoMod = true
 			break
 			break
 		}
 		}
+
+		if tempPath == filepath.Dir(tempPath) {
+			break
+		}
+
+		tempPath = filepath.Dir(tempPath)
 		if tempPath == string(filepath.Separator) {
 		if tempPath == string(filepath.Separator) {
 			break
 			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
 	return rootPath, nil
 }
 }