File: main.go

package info (click to toggle)
golang-gitlab-lupine-go-mimedb 1.33.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 156 kB
  • sloc: makefile: 5
file content (65 lines) | stat: -rw-r--r-- 1,226 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"os"
	"os/exec"
)

type MimeType struct {
	Source       string   `json:"source"`
	Extensions   []string `json:"extensions"`
	Compressible bool     `json:"compressible"`
	Charset      string   `json:"charset"`
}

type Data map[string]MimeType

func main() {
	out, err := os.OpenFile("generated_mime_types.go", os.O_RDWR|os.O_CREATE, 0644)
	if err != nil {
		panic(err)
	}

	defer out.Close()

	rsp, err := http.Get("https://cdn.rawgit.com/jshttp/mime-db/v1.33.0/db.json")
	if err != nil {
		panic(err)
	}

	if rsp.StatusCode != http.StatusOK {
		panic(rsp)
	}

	var result Data

	if err := json.NewDecoder(rsp.Body).Decode(&result); err != nil {
		panic(err)
	}

	fmt.Fprintln(out, "package mimedb")
	fmt.Fprintln(out, "")
	fmt.Fprintln(out, "var (")
	fmt.Fprintln(out, "	mimeTypeToExts = map[string][]string{")

	for mimeType, entry := range result {
		if len(entry.Extensions) > 0 {
			fmt.Fprintf(out, "		%q: %#v,\n", mimeType, entry.Extensions)
		}
	}

	fmt.Fprintln(out, "	}")
	fmt.Fprintln(out, ")")

	if err := out.Close(); err != nil {
		panic(err)
	}

	// run `go fmt` on the output
	if err := exec.Command("go", "fmt").Run(); err != nil {
		panic(err)
	}
}