-
Notifications
You must be signed in to change notification settings - Fork 43
/
cmd_import.go
110 lines (100 loc) · 3.05 KB
/
cmd_import.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
package main
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"github.com/anz-bank/sysl/pkg/cmdutils"
"github.com/sirupsen/logrus"
"github.com/anz-bank/sysl/pkg/importer"
"github.com/spf13/afero"
"gopkg.in/alecthomas/kingpin.v2"
)
type importCmd struct {
importer.OutputData
filename string
format string
outFile string
}
func (p *importCmd) Name() string { return "import" }
func (p *importCmd) MaxSyslModule() int { return 0 }
func (p *importCmd) Configure(app *kingpin.Application) *kingpin.CmdClause {
optsText := buildOptionsText(importer.Formats)
cmd := app.Command(p.Name(), "Import foreign type to Sysl. Supported types: ["+optsText+"]")
cmd.Flag("input", "path of file to import").Short('i').Required().StringVar(&p.filename)
cmd.Flag("app-name",
"name of the Sysl app to define in Sysl model.").Short('a').StringVar(&p.AppName)
cmd.Flag("package",
"name of the Sysl package to define in Sysl model.").Short('p').StringVar(&p.Package)
cmd.Flag("output", "path of file to write the imported sysl, writes to stdout when not specified").
Short('o').StringVar(&p.outFile)
cmd.Flag("format", fmt.Sprintf("format of the input filename, options: [%s]. "+
"Formats are autodetected, but this can force the use of a particular importer.", optsText)).Short('f').
StringVar(&p.format)
cmd.Flag("import-paths", "comma separated list of paths used to resolve imports in "+
"the input file. Currently only used for protobuf input.").Short('I').
StringVar(&p.ImportPaths)
cmd.Flag("shallow", "does shallow parsing of input and excludes definitions imported by the specification").
Short('s').
BoolVar(&p.Shallow)
return cmd
}
func (p *importCmd) Execute(args cmdutils.ExecuteArgs) error {
isDir, err := afero.IsDir(args.Filesystem, p.filename)
if err != nil {
return err
}
var content []byte
if !isDir {
content, err = afero.ReadFile(args.Filesystem, p.filename)
if err != nil {
return err
}
}
var imp importer.Importer
inputFilePath, err := filepath.Abs(p.filename)
if err != nil {
return err
}
imp, err = importer.Factory(inputFilePath, isDir, p.format, content, logrus.New())
if err != nil {
return err
}
imp, err = imp.Configure(&importer.ImporterArg{
AppName: p.AppName,
PackageName: p.Package,
Imports: p.ImportPaths,
Shallow: p.Shallow,
})
if err != nil {
return err
}
var output string
// TODO: Abstract this logic.
switch imp.(type) {
case *importer.ArraiImporter:
output, err = imp.LoadFile(inputFilePath)
case *importer.ProtobufImporter:
// TODO: support `Load` for proto importer
output, err = imp.LoadFile(p.filename)
default:
output, err = imp.Load(string(content))
}
if err != nil {
return err
}
if p.outFile != "" {
return afero.WriteFile(args.Filesystem, p.outFile, []byte(output), os.ModePerm)
}
_, err = fmt.Println(output)
return err
}
func buildOptionsText(opts []importer.Format) string {
var optionsText []string
for _, format := range opts {
optionsText = append(optionsText, format.Name)
}
sort.Strings(optionsText)
return strings.Join(optionsText, ", ")
}