-
Notifications
You must be signed in to change notification settings - Fork 42
/
cmd_protobuf.go
106 lines (87 loc) · 2.92 KB
/
cmd_protobuf.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
package main
import (
"fmt"
"os"
"strings"
"github.com/anz-bank/sysl/pkg/cmdutils"
"github.com/anz-bank/sysl/pkg/pbutil"
"github.com/anz-bank/sysl/pkg/sysl"
"gopkg.in/alecthomas/kingpin.v2"
)
type protobufCmd struct {
output string
mode string
compact bool
filter string
splitappspath string
}
func (p *protobufCmd) Name() string { return "protobuf" }
func (p *protobufCmd) MaxSyslModule() int { return 1 }
func (p *protobufCmd) Configure(app *kingpin.Application) *kingpin.CmdClause {
cmd := app.Command(p.Name(), "Generate textpb/json/binary").Alias("pb")
cmd.Flag("output", "output file name").Short('o').Default("-").StringVar(&p.output)
cmd.Flag(
"split-apps",
`Splits Applications into their own directory structure under the given base path.
Ignored if output is set. This BREAKS referencing.`).
Short('s').
Default("-").
StringVar(&p.splitappspath)
opts := []string{"textpb", "json", "pb"}
cmd.Flag("mode", fmt.Sprintf("output mode: [%s]", strings.Join(opts, ","))).
Default(opts[0]).
EnumVar(&p.mode, opts...)
cmd.Flag("compact", "Output without newlines and indentations").
Default("false").
BoolVar(&p.compact)
cmd.Flag("filter", "filter applications").StringVar(&p.filter)
EnsureFlagsNonEmpty(cmd)
return cmd
}
func (p *protobufCmd) Execute(args cmdutils.ExecuteArgs) error {
args.Logger.Debugf("Protobuf: %+v", *p)
if p.splitappspath != "-" {
args.Logger.Warn("Using --split-apps to split input into multiple files will BREAK references")
}
p.output = strings.TrimSpace(p.output)
p.mode = strings.TrimSpace(p.mode)
toJSON := p.mode == "json" || p.mode == "" && strings.HasSuffix(p.output, ".json")
opt := pbutil.OutputOptions{Compact: p.compact}
m := args.Modules[0]
if p.filter != "" {
apps := make(map[string]*sysl.Application)
for app, v := range m.Apps {
if strings.HasPrefix(app, p.filter) {
apps[app] = v
}
}
m.Apps = apps
m.Imports = nil
}
if toJSON {
if p.splitappspath != "-" {
return pbutil.OutputSplitApplications(m, "json", opt, p.splitappspath, "data.json", args.Filesystem)
} else if p.output == "-" {
return pbutil.FJSONPBWithOpt(os.Stdout, m, opt)
}
return pbutil.JSONPBWithOpt(m, p.output, args.Filesystem, opt)
}
if p.mode == "" || p.mode == "textpb" {
if p.output == "-" && p.splitappspath == "-" {
return pbutil.FTextPBWithOpt(os.Stdout, m, opt)
}
if p.splitappspath != "-" {
return pbutil.OutputSplitApplications(m, p.mode, opt, p.splitappspath, "data.textpb", args.Filesystem)
} else {
return pbutil.TextPBWithOpt(m, p.output, args.Filesystem, opt)
}
}
// output format is binary
if p.output == "-" && p.splitappspath == "-" {
return pbutil.GeneratePBBinaryMessage(os.Stdout, m)
}
if p.splitappspath != "-" {
return pbutil.OutputSplitApplications(m, p.mode, opt, p.splitappspath, "data.pb", args.Filesystem)
}
return pbutil.GeneratePBBinaryMessageFile(m, p.output, args.Filesystem)
}