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