github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/cmd/swarm/mimegen/generator.go (about)

     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  
    18  // Standard "mime" package rely on system-settings, see mime.osInitMime
    19  // Swarm will run on many OS/Platform/Docker and must behave similar
    20  // This command generates code to add common mime types based on mime.types file
    21  //
    22  // mime.types file provided by mailcap, which follow https://www.iana.org/assignments/media-types/media-types.xhtml
    23  //
    24  // Get last version of mime.types file by:
    25  // docker run --rm -v $(pwd):/tmp alpine:edge /bin/sh -c "apk add -U mailcap; mv /etc/mime.types /tmp"
    26  
    27  import (
    28  	"bufio"
    29  	"bytes"
    30  	"flag"
    31  	"html/template"
    32  	"io/ioutil"
    33  	"strings"
    34  
    35  	"log"
    36  )
    37  
    38  var (
    39  	typesFlag   = flag.String("types", "", "Input mime.types file")
    40  	packageFlag = flag.String("package", "", "Golang package in output file")
    41  	outFlag     = flag.String("out", "", "Output file name for the generated mime types")
    42  )
    43  
    44  type mime struct {
    45  	Name string
    46  	Exts []string
    47  }
    48  
    49  type templateParams struct {
    50  	PackageName string
    51  	Mimes       []mime
    52  }
    53  
    54  func main() {
    55  	// Parse and ensure all needed inputs are specified
    56  	flag.Parse()
    57  	if *typesFlag == "" {
    58  		log.Fatalf("--types is required")
    59  	}
    60  	if *packageFlag == "" {
    61  		log.Fatalf("--types is required")
    62  	}
    63  	if *outFlag == "" {
    64  		log.Fatalf("--out is required")
    65  	}
    66  
    67  	params := templateParams{
    68  		PackageName: *packageFlag,
    69  	}
    70  
    71  	types, err := ioutil.ReadFile(*typesFlag)
    72  	if err != nil {
    73  		log.Fatal(err)
    74  	}
    75  
    76  	scanner := bufio.NewScanner(bytes.NewReader(types))
    77  	for scanner.Scan() {
    78  		txt := scanner.Text()
    79  		if strings.HasPrefix(txt, "#") || len(txt) == 0 {
    80  			continue
    81  		}
    82  		parts := strings.Fields(txt)
    83  		if len(parts) == 1 {
    84  			continue
    85  		}
    86  		params.Mimes = append(params.Mimes, mime{parts[0], parts[1:]})
    87  	}
    88  
    89  	if err = scanner.Err(); err != nil {
    90  		log.Fatal(err)
    91  	}
    92  
    93  	result := bytes.NewBuffer([]byte{})
    94  
    95  	if err := template.Must(template.New("_").Parse(tpl)).Execute(result, params); err != nil {
    96  		log.Fatal(err)
    97  	}
    98  
    99  	if err := ioutil.WriteFile(*outFlag, result.Bytes(), 0600); err != nil {
   100  		log.Fatal(err)
   101  	}
   102  }
   103  
   104  var tpl = `// Code generated by github.com/ethereum/go-ethereum/cmd/swarm/mimegen. DO NOT EDIT.
   105  
   106  package {{ .PackageName }}
   107  
   108  import "mime"
   109  func init() {
   110  	var mimeTypes = map[string]string{
   111  {{- range .Mimes -}}
   112  	{{ $name := .Name -}}
   113  	{{- range .Exts }}
   114  		".{{ . }}": "{{ $name | html }}",
   115  	{{- end }}
   116  {{- end }}
   117  	}
   118  	for ext, name := range mimeTypes {
   119  		if err := mime.AddExtensionType(ext, name); err != nil {
   120  			panic(err)
   121  		}
   122  	}
   123  }
   124  `