1 |
|
2 | program = require 'commander'
|
3 | path = require 'path'
|
4 |
|
5 | parse = (args) ->
|
6 | program
|
7 | .arguments('<FILE>')
|
8 | .action((mod, env) ->
|
9 | program.modulefile = mod
|
10 | )
|
11 | .option('--name <role>', 'Role name', String, '')
|
12 | .option('--broker <uri>', 'Broker address', String, '')
|
13 | .parse(args)
|
14 | return program
|
15 |
|
16 | normalize = (options) ->
|
17 |
|
18 | options.broker = process.env['MSGFLO_BROKER'] if not options.broker
|
19 | options.broker = process.env['CLOUDAMQP_URL'] if not options.broker
|
20 | options.broker = 'amqp://localhost' if not options.broker
|
21 | options.modulefile = path.resolve process.cwd(), options.modulefile
|
22 | basename = (fp) ->
|
23 | path.basename(fp, path.extname(fp)).toLowerCase()
|
24 | options.name = basename options.modulefile if not options.modulefile
|
25 | return options
|
26 |
|
27 | start = (options, callback) ->
|
28 | Part = require options.modulefile
|
29 | part = Part options.broker, options.name
|
30 | part.start (err) ->
|
31 | return callback err, part
|
32 |
|
33 | exports.main = () ->
|
34 |
|
35 | options = parse process.argv
|
36 | options = normalize options
|
37 | start options, (err, part) ->
|
38 | throw err if err
|
39 | console.log "#{options.name} connected to #{options.broker}"
|
40 |
|