~samwhited/xmpp

ref: e8c09b3ff1c1489c21d96a0f7f8f0e8728fc095a xmpp/disco/gen.go -rw-r--r-- 3.8 KiB
e8c09b3fSam Whited design: fix typo in design doc template 1 year, 4 months ago
                                                                                
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2017 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

// +build ignore

// The gen command creates disco identity options from the disco-categories
// registry located at https://xmpp.org/registrar/disco-categories.html.
package main

import (
	"bytes"
	"encoding/xml"
	"flag"
	"go/format"
	"io"
	"log"
	"net/http"
	"os"
	"path/filepath"
	"strings"
	"text/template"
)

type registry struct {
	XMLName    xml.Name `xml:"registry"`
	Categories []struct {
		Name string `xml:"name"`
		Desc string `xml:"desc"`
		Type []struct {
			Name    string `xml:"name"`
			XMLLang string `xml:"http://www.w3.org/XML/1998/namespace lang"`
			Desc    string `xml:"desc"`
			Doc     string `xml:"doc"`
		} `xml:"type"`
	} `xml:"category"`
}

func main() {
	var (
		catURL  = `https://xmpp.org/registrar/disco-categories.xml`
		tmpDir  = os.TempDir()
		outPath = `./`
	)
	flag.StringVar(&catURL, "categories", catURL, "A link to the disco-categories registry")
	flag.StringVar(&tmpDir, "tmp", tmpDir, "A temporary directory to downlaod files to")
	flag.StringVar(&outPath, "out", outPath, "A directory to output Go files to")
	flag.Parse()

	ident := func(s string) string {
		return strings.Map(func(r rune) rune {
			switch r {
			case ' ', '-', '_':
				return rune(-1)
			}
			return r
		}, s)
	}

	tmpl := template.Must(template.New("categories").Funcs(map[string]interface{}{
		"ident": ident,
		"export": func(s string) string {
			s = strings.Title(s)
			// Manual tweaks to make the output more idiomatic Go.
			for _, cap := range []string{
				"Rss", "Xmpp", "Smtp", "Sms", "Qq", "Msn", "Lcs", "Irc", "Icq", "Aim",
				"S2s", "C2s", "Sm", "Pc", "Soap", "Pam", "Ntlm", "Ldap", "Http-Ws",
				"Mrim", "Ocs", "Pep", "Im",
			} {
				if strings.HasPrefix(s, cap) {
					s = strings.Replace(s, cap, strings.ToUpper(cap), 1)
					break
				}
			}
			return ident(s)
		},
	}).Parse(`// Code generated by running "go generate" in mellium.im/xmpp/disco. DO NOT EDIT.

package disco

// Predefined identities generated from the Service Discovery Identities
// Registry as registered with the XMPP Registrar.
var (
{{- range $cat := .Categories}}{{range .Type}}
	// Category: {{$cat.Desc}}
	// Type: {{.Desc}}
	{{export  $cat.Name}}{{export .Name}} = Identity{Category: {{printf "%q" $cat.Name}}, Type: {{printf "%q" .Name}}}
{{end}}{{end}}
)`))

	logger := log.New(os.Stderr, "", log.LstdFlags)
	if err := genFile(catURL, tmpDir, filepath.Join(outPath, "categories.go"), tmpl); err != nil {
		logger.Fatal(err)
	}
}

func genFile(regURL, tmpDir, outFile string, tmpl *template.Template) error {
	fd, err := openOrDownload(regURL, tmpDir)
	if err != nil {
		return err
	}
	defer fd.Close()

	out, err := os.Create(outFile)
	if err != nil {
		return err
	}
	defer fd.Close()

	reg := registry{}
	d := xml.NewDecoder(fd)
	if err = d.Decode(&reg); err != nil {
		return err
	}

	buf := new(bytes.Buffer)
	if err = tmpl.Execute(buf, reg); err != nil {
		return err
	}
	b, err := format.Source(buf.Bytes())
	if err != nil {
		return err
	}
	_, err = io.Copy(out, bytes.NewReader(b))
	return err
}

type writeCloser struct {
	io.Writer
	closer func() error
}

func (f writeCloser) Close() error {
	return f.closer()
}

// opens the provided registry URL (downloading it if it doesn't exist).
func openOrDownload(catURL, tmpDir string) (*os.File, error) {
	registryXML := filepath.Join(tmpDir, filepath.Base(catURL))
	fd, err := os.Open(registryXML)
	if err != nil {
		fd, err = os.Create(registryXML)
		if err != nil {
			return nil, err
		}
		// If we couldn't open it for reading, attempt to download it.
		resp, err := http.Get(catURL)
		if err != nil {
			return nil, err
		}
		_, err = io.Copy(fd, resp.Body)
		if err != nil {
			return nil, err
		}
		resp.Body.Close()
		_, err = fd.Seek(0, 0)
		if err != nil {
			return nil, err
		}
	}
	return fd, err
}