forked from compose/transporter
/
command.go
249 lines (204 loc) · 5.58 KB
/
command.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
package main
import (
"flag"
"fmt"
"github.com/compose/transporter/pkg/adaptor"
"github.com/mitchellh/cli"
)
// a list of generators for all the subcommand types
var subCommandFactory = map[string]cli.CommandFactory{
"list": func() (cli.Command, error) {
return &listCommand{}, nil
},
"test": func() (cli.Command, error) {
return &testCommand{}, nil
},
"run": func() (cli.Command, error) {
return &runCommand{}, nil
},
"eval": func() (cli.Command, error) {
return &evalCommand{}, nil
},
"about": func() (cli.Command, error) {
return &aboutCommand{}, nil
},
}
// listCommand loads the config, and lists the configured nodes
type listCommand struct {
configFilename string
}
func (c *listCommand) Synopsis() string {
return "list all configured nodes"
}
func (c *listCommand) Help() string {
return `Usage: trasporter list --config [file]
list the nodes that have been configured in the configuration yaml`
}
func (c *listCommand) Run(args []string) int {
var configFilename string
cmdFlags := flag.NewFlagSet("list", flag.ContinueOnError)
cmdFlags.Usage = func() { c.Help() }
cmdFlags.StringVar(&configFilename, "config", "", "config file")
cmdFlags.Parse(args)
config, err := LoadConfig(configFilename)
if err != nil {
fmt.Println(err)
return 1
}
fmt.Printf("%-20s %-15s %s\n", "Name", "Type", "URI")
for n, v := range config.Nodes {
kind, _ := v["type"].(string)
uri, _ := v["uri"].(string)
fmt.Printf("%-20s %-15s %s\n", n, kind, uri)
}
return 0
}
// runCommand loads a js file, and compiles and runs a
// javascript pipeline defined therein
type runCommand struct {
}
func newRunCommand() (cli.Command, error) {
return &runCommand{}, nil
}
func (c *runCommand) Help() string {
return `Usage: transporter run [--config file] <filename>
Run a transporter transporter application by sourcing a file containing the javascript application
and compiling the transporter pipeline`
}
func (c *runCommand) Synopsis() string {
return "Run a transporter application loaded from a file"
}
func (c *runCommand) Run(args []string) int {
var configFilename string
cmdFlags := flag.NewFlagSet("run", flag.ContinueOnError)
cmdFlags.Usage = func() { c.Help() }
cmdFlags.StringVar(&configFilename, "config", "", "config file")
cmdFlags.Parse(args)
config, err := LoadConfig(configFilename)
if err != nil {
fmt.Println(err)
return 1
}
if len(cmdFlags.Args()) == 0 {
fmt.Println("Error: A name of a file to run is required")
return 1
}
builder, err := NewJavascriptBuilder(config, cmdFlags.Args()[0], "")
if err != nil {
fmt.Println(err)
return 1
}
if err = builder.Build(); err != nil {
fmt.Println(err)
return 1
}
if err = builder.Run(); err != nil {
fmt.Println(err)
return 1
}
return 0
}
// runCommand loads a js file, and compiles and runs a
// javascript pipeline defined therein
type testCommand struct {
}
func (c *testCommand) Help() string {
return `Usage: transporter test [--config file] <filename>
Compile a transporter application by sourcing an application file, but do not run it`
}
func (c *testCommand) Synopsis() string {
return "display the compiled nodes without starting a pipeline"
}
func (c *testCommand) Run(args []string) int {
var configFilename string
cmdFlags := flag.NewFlagSet("test", flag.ContinueOnError)
cmdFlags.Usage = func() { c.Help() }
cmdFlags.StringVar(&configFilename, "config", "", "config file")
cmdFlags.Parse(args)
config, err := LoadConfig(configFilename)
if err != nil {
fmt.Println(err)
return 1
}
if len(cmdFlags.Args()) == 0 {
fmt.Println("Error: A name of a file to test is required")
return 1
}
builder, err := NewJavascriptBuilder(config, cmdFlags.Args()[0], "")
if err != nil {
fmt.Println(err)
return 1
}
if err = builder.Build(); err != nil {
fmt.Println(err)
return 1
}
fmt.Println(builder)
return 0
}
// evalCommand compiles inline javascript into a transporter pipeline,
// and runs it
type evalCommand struct {
}
func (c *evalCommand) Help() string {
return `Usage: transporter eval [--config file] <javascript>
Compile a transporter application by evaluating the given javascript`
}
func (c *evalCommand) Synopsis() string {
return "Eval javascript to build and run a transporter application"
}
func (c *evalCommand) Run(args []string) int {
var configFilename string
cmdFlags := flag.NewFlagSet("run", flag.ContinueOnError)
cmdFlags.Usage = func() { c.Help() }
cmdFlags.StringVar(&configFilename, "config", "", "config file")
cmdFlags.Parse(args)
config, err := LoadConfig(configFilename)
if err != nil {
fmt.Println(err)
return 1
}
if len(cmdFlags.Args()) == 0 {
fmt.Println("Error: A string to evaluate is required")
return 1
}
builder, err := NewJavascriptBuilder(config, "", cmdFlags.Args()[0])
if err != nil {
fmt.Println(err)
return 1
}
if err = builder.Build(); err != nil {
fmt.Println(err)
return 1
}
if err = builder.Run(); err != nil {
fmt.Println(err)
return 1
}
return 0
}
type aboutCommand struct{}
func (c *aboutCommand) Help() string {
return `Usage: transporter about [adaptor]
display information about the included database adaptors.
specifying the adaptor will display the adaptors configuration options
`
}
func (c *aboutCommand) Synopsis() string {
return "Show information about database adaptors"
}
func (c *aboutCommand) Run(args []string) int {
if len(args) == 0 {
for _, a := range adaptor.Adaptors {
fmt.Printf("%-20s %s\n", a.Name, a.Description)
}
return 0
}
a, ok := adaptor.Adaptors[args[0]]
if !ok {
fmt.Printf("no adaptor named '%s' exists\n", args[0])
return 1
}
fmt.Print(a.About())
return 0
}