generator.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2018 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. // Standard "mime" package rely on system-settings, see mime.osInitMime
  18. // Swarm will run on many OS/Platform/Docker and must behave similar
  19. // This command generates code to add common mime types based on mime.types file
  20. //
  21. // mime.types file provided by mailcap, which follow https://www.iana.org/assignments/media-types/media-types.xhtml
  22. //
  23. // Get last version of mime.types file by:
  24. // docker run --rm -v $(pwd):/tmp alpine:edge /bin/sh -c "apk add -U mailcap; mv /etc/mime.types /tmp"
  25. import (
  26. "bufio"
  27. "bytes"
  28. "flag"
  29. "html/template"
  30. "io/ioutil"
  31. "strings"
  32. "log"
  33. )
  34. var (
  35. typesFlag = flag.String("types", "", "Input mime.types file")
  36. packageFlag = flag.String("package", "", "Golang package in output file")
  37. outFlag = flag.String("out", "", "Output file name for the generated mime types")
  38. )
  39. type mime struct {
  40. Name string
  41. Exts []string
  42. }
  43. type templateParams struct {
  44. PackageName string
  45. Mimes []mime
  46. }
  47. func main() {
  48. // Parse and ensure all needed inputs are specified
  49. flag.Parse()
  50. if *typesFlag == "" {
  51. log.Fatalf("--types is required")
  52. }
  53. if *packageFlag == "" {
  54. log.Fatalf("--types is required")
  55. }
  56. if *outFlag == "" {
  57. log.Fatalf("--out is required")
  58. }
  59. params := templateParams{
  60. PackageName: *packageFlag,
  61. }
  62. types, err := ioutil.ReadFile(*typesFlag)
  63. if err != nil {
  64. log.Fatal(err)
  65. }
  66. scanner := bufio.NewScanner(bytes.NewReader(types))
  67. for scanner.Scan() {
  68. txt := scanner.Text()
  69. if strings.HasPrefix(txt, "#") || len(txt) == 0 {
  70. continue
  71. }
  72. parts := strings.Fields(txt)
  73. if len(parts) == 1 {
  74. continue
  75. }
  76. params.Mimes = append(params.Mimes, mime{parts[0], parts[1:]})
  77. }
  78. if err = scanner.Err(); err != nil {
  79. log.Fatal(err)
  80. }
  81. result := bytes.NewBuffer([]byte{})
  82. if err := template.Must(template.New("_").Parse(tpl)).Execute(result, params); err != nil {
  83. log.Fatal(err)
  84. }
  85. if err := ioutil.WriteFile(*outFlag, result.Bytes(), 0600); err != nil {
  86. log.Fatal(err)
  87. }
  88. }
  89. var tpl = `// Code generated by github.com/ethereum/go-ethereum/cmd/swarm/mimegen. DO NOT EDIT.
  90. package {{ .PackageName }}
  91. import "mime"
  92. func init() {
  93. var mimeTypes = map[string]string{
  94. {{- range .Mimes -}}
  95. {{ $name := .Name -}}
  96. {{- range .Exts }}
  97. ".{{ . }}": "{{ $name | html }}",
  98. {{- end }}
  99. {{- end }}
  100. }
  101. for ext, name := range mimeTypes {
  102. if err := mime.AddExtensionType(ext, name); err != nil {
  103. panic(err)
  104. }
  105. }
  106. }
  107. `