Browse Source

Refine errorgen

loyalsoldier 5 years ago
parent
commit
0650af46f4
1 changed files with 22 additions and 16 deletions
  1. 22 16
      common/errors/errorgen/main.go

+ 22 - 16
common/errors/errorgen/main.go

@@ -5,35 +5,41 @@ import (
 	"log"
 	"os"
 	"path/filepath"
+
+	"v2ray.com/core/common"
 )
 
-func getCurrentPkg() (string, error) {
-	path, err := os.Getwd()
+func main() {
+	pwd, err := os.Getwd()
 	if err != nil {
-		return "", err
+		fmt.Println("can not get current working directory")
+		os.Exit(1)
+	}
+	pkg := filepath.Base(pwd)
+	if pkg == "v2ray-core" {
+		pkg = "core"
 	}
-	return filepath.Base(path), nil
-}
 
-func main() {
-	pkg, err := getCurrentPkg()
-	if err != nil {
-		log.Fatal("Failed to get current package: ", err.Error())
-		return
+	moduleName, gmnErr := common.GetModuleName(pwd)
+	if gmnErr != nil {
+		fmt.Println("can not get module path", gmnErr)
+		os.Exit(1)
 	}
 
 	file, err := os.OpenFile("errors.generated.go", os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
 	if err != nil {
 		log.Fatalf("Failed to generate errors.generated.go: %v", err)
-		return
+		os.Exit(1)
 	}
+	defer file.Close()
 
 	fmt.Fprintln(file, "package", pkg)
 	fmt.Fprintln(file, "")
-	fmt.Fprintln(file, "import \"v2ray.com/core/common/errors\"")
+	fmt.Fprintln(file, "import \""+moduleName+"/common/errors\"")
 	fmt.Fprintln(file, "")
-	fmt.Fprintln(file, "type errPathObjHolder struct {}")
-	fmt.Fprintln(file, "func newError(values ...interface{}) *errors.Error { return errors.New(values...).WithPathObj(errPathObjHolder{}) }")
-
-	file.Close()
+	fmt.Fprintln(file, "type errPathObjHolder struct{}")
+	fmt.Fprintln(file, "")
+	fmt.Fprintln(file, "func newError(values ...interface{}) *errors.Error {")
+	fmt.Fprintln(file, "	return errors.New(values...).WithPathObj(errPathObjHolder{})")
+	fmt.Fprintln(file, "}")
 }