-
Notifications
You must be signed in to change notification settings - Fork 0
/
push
executable file
·119 lines (101 loc) · 3.16 KB
/
push
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
#!/usr/bin/env node
var split = require('split')
var isJson = require('is-json')
var moment = require('moment')
var options = parseArgv(process.argv)
var clc = require('cli-color')
// If help flag is set, print help text and exit.
if (options.hasOwnProperty('help')) {
console.log('./push')
console.log(' --feed-key [feed key] Example: r-pi-temperature')
console.log(' --aio-key [aio key] Example: 2d3be35cf8f613afa0d59f2240c00aedf8bdanb49')
console.log(' --verbose Use for debug output')
console.log('')
console.log('Usage: echo 42 | ./push --feed-key r-pi-temperature --aio-key 2d3be35cf8f613afa0d59f2240c00aedf8bdanb49 --verbose')
console.log('Usage: echo \'{"value":89}\' | ./push --feed-key r-pi-temperature --aio-key 2d3be35cf8f613afa0d59f2240c00aedf8bdanb49 --verbose')
console.log('')
process.exit(0)
}
// Output any errors we receive from the pipe.
process.stdin.on('error', function(e) {
console.log("error: "+e)
})
// As we get data in on the pipe, concatenate it onto data until a new line is detected at which point we push the data.
var data
process.stdin.pipe(split())
.on('data', function (line) {
data = line
if (data !== '')
console.log(data)
push(data)
})
// When we detect the process piping to this cli has exited, exitSignal is set to true and we also exit this process after sending data.
var exitSignal = false
process.stdin.on('end', function() {
exitSignal = true
})
function push(data) {
if (data == '') return
var options = parseArgv(process.argv)
console.log(options)
log('data:')
log(data)
log(options)
var doc
var jsonFormatted = isJson(data)
if (jsonFormatted === true) {
doc = JSON.parse(data)
}
else {
doc = {'value': data}
}
log('doc:')
log(doc)
var request = require('request')
var requestOptions = {
uri: 'https://io.adafruit.com/api/feeds/' + options['feed-key'] + '/data.json',
method: 'POST',
json: doc,
rejectUnauthorized: false,
headers: {
'X-AIO-KEY': options['aio-key']
}
}
log('requestOptions:')
log(requestOptions)
request(requestOptions, function (error, response, body) {
log(error)
log(response)
if (!error && response.statusCode == 200) {
console.log(body.id) // Print the shortened url.
}
})
}
// This is the standard function for parsing process.argv. It will return
// an object that where `--argumentName` is a property name and the
// proceding argument is the value. If there is no proceeding value then
// it will be interpreted as a boolean true.
// @todo Consider argv-ee
function parseArgv(argv) {
params = {}
argv.forEach(function(arg, i) {
if (arg.substr(0, 2) == '--') {
paramName = arg.substr(2, arg.length)
nextArg = argv[i+1]
if ((typeof nextArg == 'string' && nextArg.substr(0, 2) == '--') || nextArg == undefined) {
params[paramName] = true
}
else {
params[paramName] = nextArg
}
}
})
return params
}
// A log function that will only log if the --verbose flag is set
function log(msg) {
if (typeof msg == 'object') msg = JSON.stringify(msg)
if (params.hasOwnProperty('verbose')) {
console.log(clc.red(msg))
}
}