You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

125 lines
2.5 KiB

  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build ignore
  5. package main
  6. import (
  7. "bytes"
  8. "flag"
  9. "go/format"
  10. "io/ioutil"
  11. "log"
  12. "net/http"
  13. "os"
  14. "os/exec"
  15. "strings"
  16. "text/template"
  17. )
  18. const (
  19. goRepoPath = 1 << iota
  20. packagePath
  21. )
  22. var tmpl = template.Must(template.New("").Parse(`// Created by go generate; DO NOT EDIT
  23. // Copyright 2014 The Go Authors. All rights reserved.
  24. // Use of this source code is governed by a BSD-style
  25. // license that can be found in the LICENSE file.
  26. package gosrc
  27. const (
  28. goRepoPath = {{.goRepoPath}}
  29. packagePath = {{.packagePath}}
  30. )
  31. var pathFlags = map[string]int{
  32. {{range $k, $v := .pathFlags}}{{printf "%q" $k}}: {{$v}},
  33. {{end}} }
  34. var validTLDs = map[string]bool{
  35. {{range $v := .validTLDs}}{{printf "%q" $v}}: true,
  36. {{end}} }
  37. `))
  38. var output = flag.String("output", "data.go", "file name to write")
  39. func main() {
  40. log.SetFlags(0)
  41. log.SetPrefix("gen: ")
  42. flag.Parse()
  43. if flag.NArg() != 0 {
  44. log.Fatal("usage: decgen [--output filename]")
  45. }
  46. // Build map of standard repository path flags.
  47. cmd := exec.Command("go", "list", "std", "cmd")
  48. p, err := cmd.Output()
  49. if err != nil {
  50. log.Fatal(err)
  51. }
  52. pathFlags := map[string]int{
  53. "builtin": packagePath | goRepoPath,
  54. "C": packagePath,
  55. }
  56. for _, path := range strings.Fields(string(p)) {
  57. pathFlags[path] |= packagePath | goRepoPath
  58. for {
  59. i := strings.LastIndex(path, "/")
  60. if i < 0 {
  61. break
  62. }
  63. path = path[:i]
  64. pathFlags[path] |= goRepoPath
  65. }
  66. }
  67. // Get list of valid TLDs.
  68. resp, err := http.Get("https://data.iana.org/TLD/tlds-alpha-by-domain.txt")
  69. if err != nil {
  70. log.Fatal(err)
  71. }
  72. defer resp.Body.Close()
  73. p, err = ioutil.ReadAll(resp.Body)
  74. if err != nil {
  75. log.Fatal(err)
  76. }
  77. var validTLDs []string
  78. for _, line := range strings.Split(string(p), "\n") {
  79. line = strings.TrimSpace(line)
  80. if len(line) == 0 || line[0] == '#' {
  81. continue
  82. }
  83. validTLDs = append(validTLDs, "."+strings.ToLower(line))
  84. }
  85. // Generate output.
  86. var buf bytes.Buffer
  87. err = tmpl.Execute(&buf, map[string]interface{}{
  88. "output": *output,
  89. "goRepoPath": goRepoPath,
  90. "packagePath": packagePath,
  91. "pathFlags": pathFlags,
  92. "validTLDs": validTLDs,
  93. })
  94. if err != nil {
  95. log.Fatal("template error:", err)
  96. }
  97. source, err := format.Source(buf.Bytes())
  98. if err != nil {
  99. log.Fatal("source format error:", err)
  100. }
  101. fd, err := os.Create(*output)
  102. _, err = fd.Write(source)
  103. if err != nil {
  104. log.Fatal(err)
  105. }
  106. }