-
Notifications
You must be signed in to change notification settings - Fork 42
/
arrai.go
112 lines (95 loc) · 2.68 KB
/
arrai.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
package importer
import (
"fmt"
"github.com/anz-bank/sysl/pkg/arrai"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// ArraiImporter encapsulates glue code for calling arr.ai scripts to import specs.
type ArraiImporter struct {
appName string
pkg string
asset []byte
logger *logrus.Logger
}
// MakeArraiImporterImporter returns a new ArraiImporter.
func MakeArraiImporterImporter(asset []byte, logger *logrus.Logger) *ArraiImporter {
return &ArraiImporter{
asset: asset,
logger: logger,
}
}
// Configure allows the imported Sysl application name, package and import directories to be specified.
func (i *ArraiImporter) Configure(appName, packageName, _ string) (Importer, error) {
if appName == "" {
return nil, errors.New("application name not provided")
}
i.appName = appName
i.pkg = packageName
return i, nil
}
// LoadFile generates a Sysl spec be invoking the arr.ai script.
func (i *ArraiImporter) LoadFile(path string) (string, error) {
args, err := buildArraiImporterArgs(&arraiImporterArgs{
appName: i.appName,
specPath: path,
pkg: i.pkg,
})
if err != nil {
return "", err
}
val, err := arrai.EvaluateBundle(i.asset, args...)
if err != nil {
return "", errors.Wrap(arrai.ExecutionError{
Context: fmt.Sprintf("import(`%s`, `%s`)", i.appName, path),
Err: err,
ShortMsg: err.Error(),
}, "Executing arr.ai transform failed")
}
return val.String(), nil
}
// Load generates a Sysl spec given the content of an input file.
func (i *ArraiImporter) Load(content string) (string, error) {
args, err := buildArraiImporterArgs(&arraiImporterArgs{
appName: i.appName,
specContent: content,
pkg: i.pkg,
})
if err != nil {
return "", err
}
val, err := arrai.EvaluateBundle(i.asset, args...)
if err != nil {
return "", errors.Wrap(arrai.ExecutionError{
Context: fmt.Sprintf("AppName: %s, Content: %s", i.appName, content),
Err: err,
ShortMsg: err.Error(),
}, "Executing arr.ai transform failed")
}
return val.String(), nil
}
type arraiImporterArgs struct {
appName, specPath, specContent, pkg string
}
func buildArraiImporterArgs(a *arraiImporterArgs) ([]string, error) {
args := []string{}
if a.specContent != "" && a.specPath != "" {
return nil, errors.New("provide only path to spec or the spec content")
}
if a.specContent == "" && a.specPath == "" {
return nil, errors.New("spec not provided")
}
if a.appName != "" {
args = append(args, "--app-name", a.appName)
}
if a.specContent != "" {
args = append(args, "--spec", a.specContent)
}
if a.specPath != "" {
args = append(args, "--input", a.specPath)
}
if a.pkg != "" {
args = append(args, "--package", a.pkg)
}
return args, nil
}