-
Notifications
You must be signed in to change notification settings - Fork 3
/
notifier_output.go
73 lines (57 loc) · 1.59 KB
/
notifier_output.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
package instrumentor
import (
"io"
"os"
"path"
"text/template"
"github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/common"
)
func GenerateNotifierSource(notifierDir string, notifierInfo *NotifierInfo) {
var tmpl *template.Template
var err error
tmpl = template.New("notifier")
if tmpl, err = tmpl.Parse(getNotifierText()); err != nil {
panic(err)
}
var outFile io.Writer
if outFile, err = notifierOutputFile(notifierDir, notifierInfo.logWriter); err != nil {
panic(err)
}
if err = tmpl.Execute(outFile, notifierInfo); err != nil {
panic(err)
}
}
func getNotifierText() string {
const text = `package {{.NotifierPackage}}
// ----------------------------------------------------
// Generated by Antithesis instrumentor - do not modify
// ----------------------------------------------------
import "{{.InstrumentationPackageName}}"
func init() {
instrumentation.InitializeModule("{{.SymbolTableName}}", {{.EdgeCount}})
}
func Notify(edge int) {
instrumentation.Notify(edge)
}
`
return text
}
func notifierOutputFile(dir_name string, logWriter *common.LogWriter) (*os.File, error) {
output_file_name := path.Join(dir_name, common.GENERATED_NOTIFIER_SOURCE)
var file *os.File
var err error
if file, err = os.OpenFile(output_file_name, os.O_RDWR|os.O_CREATE, 0644); err != nil {
file = nil
}
if file != nil {
if err = file.Truncate(0); err != nil {
file = nil
}
}
if err == nil {
logWriter.Printf("Notifier file: %q\n", output_file_name)
} else {
logWriter.Printf("Unable to generate Notifier file: %q\n", output_file_name)
}
return file, err
}