-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommand-search.coffee
56 lines (45 loc) · 1.54 KB
/
command-search.coffee
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
_ = require 'lodash'
commander = require 'commander'
fs = require 'fs'
path = require 'path'
colors = require 'colors'
BaseCommand = require './base-command'
class SearchCommand extends BaseCommand
run: =>
@parseOptions()
meshbluHttp = @getMeshbluHttp()
options = {}
options.as = @as if @as?
meshbluHttp.search @query, options, (error, data) =>
return @die error if error?
console.log JSON.stringify(data, null, 2)
process.exit 0
loadQueryFile: (filename) =>
try
return fs.readFileSync path.resolve(filename)
catch error
console.error colors.yellow error.message
console.error colors.red '\n Unable to open a valid query.json file'
commander.outputHelp()
process.exit 1
parseOptions: =>
commander
.option '-q, --query <query>', 'Meshblu device to get (defaults to uuid from meshblu.json)'
.option '-a, --as <uuid>', 'the uuid to send the message as (defaults to meshblu.json)'
.option '-f, --file <path/to/query.json>', 'query [JSON FILE]'
.usage '[options] <path/to/meshblu.json>'
.parse process.argv
@filename = _.first commander.args
@queryFileName = commander.file
@as = commander.as
{query} = commander
query = @loadQueryFile(@queryFileName) if @queryFileName?
unless query?
commander.outputHelp()
process.exit 0
try
@query = JSON.parse query
catch e
commander.outputHelp()
@die 'Invalid message json'
(new SearchCommand()).run()