-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·155 lines (141 loc) · 3.95 KB
/
cli.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env node
var program = require('commander')
program.allowUnknownOption(true)
var fs = require('fs')
var exec = require('child_process').exec
var config;
var bootstrap = function(callback) {
fs.readFile('config.json', {'encoding': 'utf8'}, function(err, data) {
if (err) {
console.log('No config.json found.')
config = false
return callback()
}
config = JSON.parse(data)
callback()
})
}
var init = function () {
bootstrap(function() {
console.log('Initializing your pipe.')
if (config !== false) {
return console.log('Aborting. Found an active config.json file.')
}
var writeFile = function(callback) {
fs.writeFile('config.json', '{}', function(err) {
if (err) {
console.log(err)
}
console.log('File config.json written.')
callback()
})
}
// go
writeFile(function() {
console.log('done')
})
})
}
var config = function(key, value) {
bootstrap(function() {
if (!key) return console.log(JSON.stringify(config, null, 2))
var props = key.split('.')
if (!config.hasOwnProperty(props[0])) {
config[props[0]] = {}
}
config[props[0]][props[1]] = value
fs.writeFile('config.json', JSON.stringify(config, null, 2), function(err) {
if (err) return console.log(err)
console.log('done')
})
})
}
var push = function(data) {
bootstrap(function() {
// @todo Check for dumn stuff
var pushCmd = './' + config.pipe.push + '/push'
for (key in config[config.pipe.push]) {
pushCmd += ' --' + key + ' "' + config[config.pipe.push][key] + '"'
}
pushCmd += ' --data ' + data
if (program.verbose) console.log("Running: " + pushCmd)
var child = exec(pushCmd)
child.stdout.on('data', function(data) {
console.log(data);
})
child.stderr.on('data', function(data) {
console.log(data);
})
child.on('close', function(code) {
//console.log('closing code: ' + code);
})
})
}
var pull = function() {
bootstrap(function() {
// @todo Check for dumn stuff
var pullCmd = './' + config.pipe.pull + '/pull'
for (key in config[config.pipe.pull]) {
pullCmd += ' --' + key + ' "' + config[config.pipe.pull][key] + '"'
}
if (program.verbose) console.log("Running: " + pullCmd)
var child = exec(pullCmd)
child.stdout.on('data', function(data) {
console.log(data);
})
child.stderr.on('data', function(data) {
console.log(data);
})
child.on('close', function(code) {
//console.log('closing code: ' + code);
})
})
}
var pump = function() {
bootstrap(function() {
var child = exec('pipe push `pipe pull`')
child.stdout.on('data', function(data) {
console.log(data);
})
child.stderr.on('data', function(data) {
console.log(data);
})
child.on('close', function(code) {
if (config.hasOwnProperty('pump') && config.pump.hasOwnProperty('interval')) {
console.log('Waiting ' + config.pump.interval + ' milliseconds.')
setTimeout(pump, config.pump.interval)
}
})
})
}
if (!process.argv[2]) {
var child = exec('pipe --help')
child.stdout.on('data', function(data) {
console.log(data);
})
}
program
.command('init')
.description('create a `config.json` file and a `drivers` folder in the directory you are in')
.action(init)
program
.command('config [key] [value]')
.description('add or modify a value in the config.json file')
.action(config)
program
.command('push <data>')
.description('Push data using a configured Pipe')
.action(push)
program
.command('pull')
.option('--verbose')
.description('Pull data using a configured Pipe')
.action(pull)
program
.command('pump [operation]')
.description('configure the pump by setting the pump.interval value in config to your desired milliseconds and then use `pipe pump start`')
.action(pump)
program
.version('0.1.0')
.option('--verbose')
.parse(process.argv);