-
Notifications
You must be signed in to change notification settings - Fork 7
/
util.go
340 lines (303 loc) · 8.8 KB
/
util.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// util.go: misc functions to convert/send http requests/sort maps
package catalog
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"reflect"
"sort"
"strings"
"github.com/anz-bank/sysl/pkg/diagrams"
"github.com/anz-bank/protoc-gen-sysl/newsysl"
"github.com/anz-bank/sysl/pkg/syslutil"
"github.com/joshcarp/mermaid-go/mermaid"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/anz-bank/sysl/pkg/cmdutils"
"github.com/anz-bank/sysl/pkg/sequencediagram"
"github.com/hashicorp/go-retryablehttp"
"github.com/anz-bank/sysl/pkg/sysl"
)
// SanitiseOutputName removes characters so that the string can be used as a hyperlink.
func SanitiseOutputName(s string) string {
return strings.ReplaceAll(strings.ReplaceAll(s, " ", ""), "/", "")
}
// rootDirectory converts a path (eg whatever/anotherdir/this.that) to the ../ pattern to get
// back to the original folder that the sysl-catalog command was executed from
func rootDirectory(s string) string {
var rootPath string
dir, _ := path.Split(s)
numberOfLevels := len(strings.Split(dir, "/"))
for i := 0; i < numberOfLevels; i++ {
rootPath += "../"
}
return rootPath
}
func SortedKeys(m interface{}) []string {
keys := reflect.ValueOf(m).MapKeys()
ret := make([]string, 0, len(keys))
for _, v := range keys {
ret = append(ret, v.String())
}
sort.Strings(ret)
return ret
}
// NewTypeRef returns a type reference, needed to correctly generate data model diagrams
func NewTypeRef(appName, typeName string) *sysl.Type {
return &sysl.Type{
Type: &sysl.Type_TypeRef{
TypeRef: &sysl.ScopedRef{
Ref: &sysl.Scope{Appname: &sysl.AppName{
Part: []string{appName},
},
Path: []string{appName, typeName},
},
},
},
}
}
// TernaryOperator returns the first element if bool is true and the second element is false
func TernaryOperator(condition bool, i ...interface{}) interface{} {
if condition {
return i[0]
}
return i[1]
}
// createProjectApp returns a "project" app used to make integration diagrams for any "sub module" apps
func createProjectApp(Apps map[string]*sysl.Application) *sysl.Application {
app := newsysl.Application("")
app.Endpoints = make(map[string]*sysl.Endpoint)
app.Endpoints["_"] = newsysl.Endpoint("_")
app.Endpoints["_"].Stmt = []*sysl.Statement{}
for key, _ := range Apps {
app.Endpoints["_"].Stmt = append(app.Endpoints["_"].Stmt, newsysl.StringStatement(key))
}
if app.Attrs == nil {
app.Attrs = make(map[string]*sysl.Attribute)
}
if _, ok := app.Attrs["appfmt"]; !ok {
app.Attrs["appfmt"] = &sysl.Attribute{
Attribute: &sysl.Attribute_S{S: "%(appname)"},
}
}
return app
}
// createProjectApp returns a "project" app used to make integration diagrams for any "sub module" apps
func createModuleFromSlices(m *sysl.Module, stmnts []string) *sysl.Module {
ret := &sysl.Module{Apps: make(map[string]*sysl.Application)}
for _, appName := range stmnts {
for key, e := range m.GetApps() {
if Attribute(e, "package") == appName {
ret.Apps[key] = e
}
}
}
return ret
}
type Attr interface {
GetAttrs() map[string]*sysl.Attribute
}
func Attribute(a Attr, query string) string {
if description := a.GetAttrs()[query]; description != nil {
return description.GetS()
}
return ""
}
func Fields(t *sysl.Type) map[string]*sysl.Type {
if tuple := t.GetTuple(); tuple != nil {
return tuple.GetAttrDefs()
}
return nil
}
func FieldType(t *sysl.Type) string {
typeName, typeDetail := syslutil.GetTypeDetail(t)
if typeName == "primitive" {
return strings.ToLower(typeDetail)
}
if typeName == "sequence" {
return "sequence of " + typeDetail
}
if typeName == "type_ref" {
return strings.Join(t.GetTypeRef().GetRef().GetPath(), ".")
}
if typeName != "" {
return typeName
}
return typeDetail
}
type Namer interface {
Attr
GetName() *sysl.AppName
}
// GetAppPackageName returns the package and app name of any sysl application
func GetAppPackageName(a Namer) (string, string) {
appName := strings.Join(a.GetName().GetPart(), "")
packageName := appName
if attr := a.GetAttrs()["package"]; attr != nil {
packageName = attr.GetS()
}
return packageName, appName
}
func ModulePackageName(m *sysl.Module) string {
for _, key := range SortedKeys(m.GetApps()) {
app := m.Apps[key]
packageName, _ := GetAppPackageName(app)
return packageName
}
return ""
}
// Map applies a function to every element in a string slice
func Map(vs []string, funcs ...func(string) string) []string {
vsm := make([]string, len(vs))
for i, v := range vs {
for j, f := range funcs {
var middle string
if j == 0 {
middle = f(v)
vsm[i] = middle
}
vsm[i] = f(middle)
}
}
return vsm
}
// Map applies a function to every element in a string slice
func Filter(vs []string, f func(string) bool) []string {
vsm := make([]string, 0, len(vs))
for _, v := range vs {
if f(v) {
vsm = append(vsm, v)
}
}
return vsm
}
func AsSet(in []string) map[string]struct{} {
ret := make(map[string]struct{})
for _, e := range in {
ret[e] = struct{}{}
}
return ret
}
// RetryHTTPRequest retries the given request
func RetryHTTPRequest(url string) ([]byte, error) {
client := retryablehttp.NewClient()
client.Logger = nil
client.RetryMax = 100
resp, err := client.Get(url)
if err != nil {
return nil, err
}
return ioutil.ReadAll(resp.Body)
}
// PlantUMLURL returns a PlantUML url
func PlantUMLURL(plantumlService, contents string) (string, error) {
encoded, err := diagrams.DeflateAndEncode([]byte(contents))
if err != nil {
return "", err
}
return fmt.Sprint(plantumlService, "/", "svg", "/~1", encoded), nil
}
func HttpToFile(fs afero.Fs, fileName, url string) error {
if err := fs.MkdirAll(path.Dir(fileName), os.ModePerm); err != nil {
return err
}
out, err := RetryHTTPRequest(url)
if err != nil {
return err
}
if err := afero.WriteFile(fs, fileName, out, os.ModePerm); err != nil {
return err
}
return nil
}
func (p *Generator) PUMLFile(fs afero.Fs, fileName, contents string) error {
fileName = strings.ReplaceAll(fileName, ".svg", ".puml")
if err := fs.MkdirAll(path.Dir(fileName), os.ModePerm); err != nil {
return err
}
if err := afero.WriteFile(fs, fileName, []byte(contents), os.ModePerm); err != nil {
return err
}
return nil
}
func PlantUMLJava(service, out string) error {
out = strings.TrimRight(out, "/")
cleanup := exec.Command("find", out, "-type", "f", "-name", "*.puml", "-delete")
defer cleanup.Run()
command := []string{"java", "-Xms256m", "-Xmx512m", "-Djava.security.egd=file:/dev/./urandom", "-XX:+UnlockExperimentalVMOptions", "-Djava.awt.headless=true", "-jar", service, "-tsvg", `"` + out + `*/**.puml"`}
c2 := exec.Command("sh", "-c", strings.Join(command, " "))
return c2.Run()
}
func PlantUMLNailGun(contents string) ([]byte, error) {
c2 := exec.Command("./ng", "net.sourceforge.plantuml.Run", "-tsvg", "-p")
c2.Stdin = strings.NewReader(contents)
var stdout, stderr bytes.Buffer
c2.Stdout = &stdout
c2.Stderr = &stderr
if err := c2.Run(); err != nil {
return nil, err
}
if len(stderr.Bytes()) != 0 {
return nil, errors.New(stderr.String())
}
plantuml := strings.TrimLeft(stdout.String(), "\n")
plantuml = strings.TrimRight(plantuml, "\n")
return []byte(plantuml), nil
}
// GenerateAndWriteMermaidDiagram writes a mermaid svg to file
func GenerateAndWriteMermaidDiagram(fs afero.Fs, fileName string, data string) error {
mermaidSvg := []byte(mermaid.Execute(data) + "\n")
var err = afero.WriteFile(fs, fileName, mermaidSvg, os.ModePerm)
if err != nil {
return err
}
return nil
}
// GenerateRedoc creates a redoc html file
func GenerateAndWriteRedoc(fs afero.Fs, fileName string, specURL string) error {
redoc := BuildRedoc(specURL)
err := afero.WriteFile(fs, fileName, redoc, os.ModePerm)
if err != nil {
return err
}
return nil
}
// CreateSequenceDiagram creates an sequence diagram and returns the sequence diagram string and any errors
func CreateSequenceDiagram(m *sysl.Module, call string) (string, error) {
l := &cmdutils.Labeler{}
p := &sequencediagram.SequenceDiagParam{
AppLabeler: l,
EndpointLabeler: l,
Endpoints: []string{call},
Title: call,
}
return sequencediagram.GenerateSequenceDiag(m, p, logrus.New())
}
type Typer interface {
GetType() *sysl.Type
}
// GetAppTypeName takes a Sysl Type and returns the appName and typeName of a param
// If the type is a primitive, the appName returned is "primitive"
func GetAppTypeName(param Typer) (appName string, typeName string) {
ref := param.GetType().GetTypeRef().GetRef()
appNameParts := ref.GetAppname().GetPart()
if a, b := syslutil.GetTypeDetail(param.GetType()); a == "primitive" {
return a, b
}
if len(appNameParts) > 0 {
typeNameParts := ref.GetPath()
if typeNameParts != nil {
appName = appNameParts[0]
typeName = typeNameParts[0]
} else {
typeName = appNameParts[0]
}
} else {
typeName = ref.GetPath()[0]
}
return appName, typeName
}