main.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // +build generate
  2. package main
  3. import (
  4. "flag"
  5. "fmt"
  6. "log"
  7. "os"
  8. "strings"
  9. "v2ray.com/core/common"
  10. )
  11. var (
  12. pkg = flag.String("pkg", "", "Target package")
  13. path = flag.String("path", "", "Path")
  14. )
  15. func main() {
  16. flag.Parse()
  17. if len(*pkg) == 0 {
  18. panic("Package is not specified.")
  19. }
  20. if len(*path) == 0 {
  21. panic("Path is not specified.")
  22. }
  23. paths := strings.Split(*path, ",")
  24. for i := range paths {
  25. paths[i] = "\"" + paths[i] + "\""
  26. }
  27. pathStr := strings.Join(paths, ", ")
  28. file, err := os.OpenFile("errors.generated.go", os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
  29. if err != nil {
  30. log.Fatalf("Failed to generate errors.generated.go: %v", err)
  31. }
  32. common.Must2(fmt.Fprintln(file, "package", *pkg))
  33. common.Must2(fmt.Fprintln(file, ""))
  34. common.Must2(fmt.Fprintln(file, "import \"v2ray.com/core/common/errors\""))
  35. common.Must2(fmt.Fprintln(file, ""))
  36. common.Must2(fmt.Fprintln(file, "func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("+pathStr+") }"))
  37. common.Must(file.Close())
  38. }