github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/common/errors/errorgen/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"v2ray.com/core/common"
    10  )
    11  
    12  func main() {
    13  	pwd, err := os.Getwd()
    14  	if err != nil {
    15  		fmt.Println("can not get current working directory")
    16  		os.Exit(1)
    17  	}
    18  	pkg := filepath.Base(pwd)
    19  	if pkg == "v2ray-core" {
    20  		pkg = "core"
    21  	}
    22  
    23  	moduleName, gmnErr := common.GetModuleName(pwd)
    24  	if gmnErr != nil {
    25  		fmt.Println("can not get module path", gmnErr)
    26  		os.Exit(1)
    27  	}
    28  
    29  	file, err := os.OpenFile("errors.generated.go", os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
    30  	if err != nil {
    31  		log.Fatalf("Failed to generate errors.generated.go: %v", err)
    32  		os.Exit(1)
    33  	}
    34  	defer file.Close()
    35  
    36  	fmt.Fprintln(file, "package", pkg)
    37  	fmt.Fprintln(file, "")
    38  	fmt.Fprintln(file, "import \""+moduleName+"/common/errors\"")
    39  	fmt.Fprintln(file, "")
    40  	fmt.Fprintln(file, "type errPathObjHolder struct{}")
    41  	fmt.Fprintln(file, "")
    42  	fmt.Fprintln(file, "func newError(values ...interface{}) *errors.Error {")
    43  	fmt.Fprintln(file, "	return errors.New(values...).WithPathObj(errPathObjHolder{})")
    44  	fmt.Fprintln(file, "}")
    45  }