-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
44 lines (34 loc) · 1006 Bytes
/
client.go
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
package main
import (
"bytes"
"fmt"
"go/format"
"io"
"log"
"text/template"
)
const (
clientTemplateName = "client.tpl"
clientTemplateFileName = "./tpl/client.tpl"
clientFileName = clientTemplateName + ".go"
)
var (
clientTemplate = template.Must(template.New(clientTemplateName).Funcs(templateHelpers).ParseFiles(clientTemplateFileName))
)
func renderClient(in io.Writer, data *apiDefinition) error {
buff := bytes.NewBuffer([]byte{})
if len(templateDir) != 0 {
clientTemplate = template.Must(template.New(clientTemplateName).Funcs(templateHelpers).ParseFiles(fmt.Sprintf("./%s/%s", templateDir, "client.tpl")))
}
if err := clientTemplate.Execute(buff, data); err != nil {
return fmt.Errorf("failed to render client: %w", err)
}
src := buff.Bytes()
formatted, err := format.Source(src)
if err != nil {
log.Printf("failed to format source of %s/client.go: err:%s", data.PackageName, err.Error())
formatted = src
}
_, err = in.Write(formatted)
return err
}