/
spanner.go
63 lines (55 loc) · 1.64 KB
/
spanner.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
package exporter
import (
"fmt"
"os"
"github.com/anz-bank/sysl/internal/bundles"
"github.com/anz-bank/sysl/pkg/arrai"
"github.com/anz-bank/sysl/pkg/loader"
"github.com/anz-bank/sysl/pkg/sysl"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
)
type SpannerExporter struct {
fs afero.Fs
log *logrus.Logger
rootPath string
outPath string
}
// MakeSpannerExporter returns a new SpannerExporter.
func MakeSpannerExporter(fs afero.Fs, logger *logrus.Logger, rootPath, outPath string) *SpannerExporter {
return &SpannerExporter{fs: fs, log: logger, rootPath: rootPath, outPath: outPath}
}
// ExportFile reads in a Sysl file from path, converts it to the output format, and writes it to
// the file system.
func (s *SpannerExporter) ExportFile(inPath string) error {
err := loader.EnsureSyslPb(s.fs, s.rootPath, inPath)
if err != nil {
return err
}
out, err := s.transform(inPath)
if err != nil {
return err
}
err = afero.WriteFile(s.fs, s.outPath, []byte(out), os.ModePerm)
if err != nil {
return err
}
return nil
}
// transform converts the Sysl at path to an SQL output string.
func (s *SpannerExporter) transform(path string) (string, error) {
b := bundles.SpannerExporter
out, err := arrai.EvaluateBundle(b, path)
if err != nil {
return "", errors.Wrap(arrai.ExecutionError{
Context: fmt.Sprintf("export(`%s`)", path),
Err: err,
ShortMsg: "Error executing SQL exporter",
}, "Executing arrai transform failed")
}
return out.String(), nil
}
func (s *SpannerExporter) ExportApp(*sysl.Application) error {
return errors.Errorf("ExportApp not implemented for Spanner exporter")
}