mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-12-28 04:55:37 -05:00
simplify error creation
This commit is contained in:
@@ -3,47 +3,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"v2ray.com/core/common"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
var (
|
||||
pkg = flag.String("pkg", "", "Target package")
|
||||
path = flag.String("path", "", "Path")
|
||||
)
|
||||
func getCurrentPkg() (string, error) {
|
||||
path, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Base(path), nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
if len(*pkg) == 0 {
|
||||
panic("Package is not specified.")
|
||||
pkg, err := getCurrentPkg()
|
||||
if err != nil {
|
||||
log.Fatal("Failed to get current package: ", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if len(*path) == 0 {
|
||||
panic("Path is not specified.")
|
||||
}
|
||||
|
||||
paths := strings.Split(*path, ",")
|
||||
for i := range paths {
|
||||
paths[i] = "\"" + paths[i] + "\""
|
||||
}
|
||||
pathStr := strings.Join(paths, ", ")
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
common.Must2(fmt.Fprintln(file, "package", *pkg))
|
||||
common.Must2(fmt.Fprintln(file, ""))
|
||||
common.Must2(fmt.Fprintln(file, "import \"v2ray.com/core/common/errors\""))
|
||||
common.Must2(fmt.Fprintln(file, ""))
|
||||
common.Must2(fmt.Fprintln(file, "func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("+pathStr+") }"))
|
||||
fmt.Fprintln(file, "package", pkg)
|
||||
fmt.Fprintln(file, "")
|
||||
fmt.Fprintln(file, "import \"v2ray.com/core/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{}) }")
|
||||
|
||||
common.Must(file.Close())
|
||||
file.Close()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user