-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommand-update.coffee
53 lines (45 loc) · 1.7 KB
/
command-update.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
_ = require 'lodash'
fs = require 'fs'
path = require 'path'
colors = require 'colors'
commander = require 'commander'
BaseCommand = require './base-command'
class UpdateCommand extends BaseCommand
parseUpdate: (filename) =>
try
JSON.parse fs.readFileSync path.resolve(filename)
catch error
console.error colors.yellow error.message
console.error colors.red '\n Unable to open a valid updated-device.json file'
commander.outputHelp()
process.exit 1
parseOptions: =>
commander
.option '-u, --uuid <uuid>', 'uuid to update (defaults to meshblu.json)'
.option '-p, --put', 'Use PUT to send the whole device state. Enables $set, $inc, etc. operators'
.option '-d, --data <\'{"name":"Some Device"}\'>', 'Device Data [JSON]'
.option '-f, --file <path/to/updated-device.json>', 'Device Data [JSON FILE]'
.usage '[options] <path/to/meshblu.json>'
.parse process.argv
@filename = _.first commander.args
@data = commander.data
@uuid = commander.uuid
@updateFileName = commander.file
@usePut = commander.put
@data = @parseUpdate(@updateFileName) if @updateFileName?
return if _.isPlainObject @data
try
@data = JSON.parse @data
catch e
commander.outputHelp()
@die 'You must specify valid update json.'
run: =>
@parseOptions()
meshbluHttp = @getMeshbluHttp()
@uuid ?= @config.uuid
updateMethod = 'update'
updateMethod = 'updateDangerously' if @usePut
meshbluHttp[updateMethod] @uuid, @data, (error, data) =>
return @die error if error?
process.exit 0
(new UpdateCommand()).run()