-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
76 lines (60 loc) · 1.66 KB
/
render.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
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
package autonats
import (
"bytes"
"errors"
"fmt"
"go/format"
"io/ioutil"
"path/filepath"
"sort"
)
type RenderData struct {
PackageName, FileName, Path string
Services []*Service
Imports []string
Timeout int
JsonLib string
Tracing bool
}
func Render(data *RenderData) error {
if data == nil || len(data.Services) == 0 {
return errors.New("no data found to render")
}
data.Imports = append(data.Imports,
"github.com/zyra/autonats",
"github.com/nats-io/nats.go",
"time",
"github.com/json-iterator/go",
)
if data.Tracing {
data.Imports = append(data.Imports,
"github.com/nats-io/not.go",
"github.com/opentracing/opentracing-go",
"github.com/opentracing/opentracing-go/ext",
"github.com/opentracing/opentracing-go/log")
}
sort.Strings(data.Imports)
sort.Slice(data.Services, func(i, j int) bool {
return data.Services[i].Name < data.Services[j].Name
})
data.PackageName = data.Services[0].PackageName
data.Timeout = 5
outFile := filepath.Join(data.Path, data.FileName)
b := make([]byte, 0)
buff := bytes.NewBuffer(b)
err := tmplService.Execute(buff, data)
if err != nil {
return fmt.Errorf("failed to execute template: %s", err.Error())
}
out, err := format.Source(buff.Bytes())
if err != nil {
_ = ioutil.WriteFile(outFile, buff.Bytes(), 0655)
return fmt.Errorf("failed to run gofmt on generated source: %s", err.Error())
}
fmt.Printf("rendering data to %s\n", outFile)
if err := ioutil.WriteFile(outFile, out, 0655); err != nil {
return fmt.Errorf("failed to write file: %s", err.Error())
} else {
return nil
}
}