-
Notifications
You must be signed in to change notification settings - Fork 0
/
push
executable file
·115 lines (103 loc) · 2.72 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
#!/usr/bin/env node
var http = require('http')
var split = require('split')
var moment = require('moment')
var _ = require('underscore')
var options = parseArgv(process.argv)
var isJson = require('is-json')
var request = require('request')
var exitSignal = false
var data
if (options.hasOwnProperty('help')) {
help()
process.exit(0)
}
process.stdin.on('error', function(e) {
console.log("error: "+e)
})
process.stdin.pipe(split())
.on('data', function (line) {
data = line
if (data !== '')
push()
})
process.stdin.on('end', function() {
exitSignal = true
})
function push() {
log('data: ' + data)
var jsonFormatted = isJson(data)
var path = 'http:' + options.url
log('path is ' + path)
var packet = {}
if (isJson(data)) {
packet = JSON.parse(data)
}
else {
packet.value = data
}
var format
if (options.format) {
format = options.format
}
else {
format = 'YYYY-MM-DD HH:mm:ss'
}
packet.timestamp = moment().format(format)
// Sanitize number columns as they don't work well in Google Sheets / Cloud Stitch
var keys = _.keys(packet)
var sanitized = false
keys.forEach(function(key) {
if (!isNaN(key)) {
packet['column' + key] = packet[key]
delete packet[key]
sanitized = true
}
})
if (sanitized == true) log(packet)
request.post(path, {form:packet}, function(err,httpResponse,body){
if (err) {
console.log(err)
process.exit(1)
}
else {
log(httpResponse)
if (exitSignal === true) process.exit(0)
}
})
}
function help() {
console.log('./push')
console.log(' --url <url> Example: //api.cloudstitch.com/janesmith/magic-form-20/datasources/sheet')
console.log(' --verbose Use for debug output')
console.log(' --format format string, see momentjs')
console.log('')
console.log('Usage: echo \'{"foo":"bar"}\' | ./push --url //api.cloudstitch.com/janesmith/magic-form-20/datasources/sheet')
console.log('')
}
// 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.
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) == '--') {
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 (params.hasOwnProperty('verbose')) {
console.log(msg)
}
}