1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 | const help=`Usage: skale [options] <command> [<args>]
|
6 |
|
7 | Commands:
|
8 | init Set configuration
|
9 | run <file> [<args>...] Run file on skale cluster
|
10 | demo <file> [<args>...] Run demo file on skale cluster
|
11 |
|
12 | Options:
|
13 | -c, --config=<file> Set configuration file [~/.skalerc]
|
14 | -H, --host=<hostname> Set skale hostname [SKALE_HOST]
|
15 | -p, --port=<portnum> Set skale port number [SKALE_PORT]
|
16 | -k, --key=<acess_key> Set skale access key [SKALE_KEY]
|
17 | -s, --ssl Use SSL protocol
|
18 | -h, --help Show help
|
19 | -V, --version Show version
|
20 | `;
|
21 |
|
22 | const fs = require('fs');
|
23 |
|
24 | const argv = require('minimist')(process.argv.slice(2), {
|
25 | string: ['c', 'config', 'H', 'host', 'p', 'port', 'k', 'key'],
|
26 | boolean: ['h', 'help', 'V', 'version', 's', 'ssl'],
|
27 | });
|
28 |
|
29 | if (argv.h || argv.help) {
|
30 | console.log(help);
|
31 | process.exit();
|
32 | }
|
33 | if (argv.V || argv.version) {
|
34 | var pkg = require('./package');
|
35 | console.log(pkg.name + '-' + pkg.version);
|
36 | process.exit();
|
37 | }
|
38 |
|
39 | const config = load(argv);
|
40 | const proto = config.ssl ? require('https') : require('http');
|
41 |
|
42 | switch (argv._[0]) {
|
43 | case 'init':
|
44 | break;
|
45 | case 'demo':
|
46 | run(__dirname + '/' + argv._[1], argv._.splice(2));
|
47 | break;
|
48 | case 'run':
|
49 | run(argv._[1], argv._.splice(2));
|
50 | break;
|
51 | default:
|
52 | die('Error: invalid command: ' + argv._[0]);
|
53 | }
|
54 |
|
55 | function die(err) {
|
56 | console.error(help);
|
57 | console.error(err);
|
58 | process.exit(1);
|
59 | }
|
60 |
|
61 | function load(argv) {
|
62 | var conf = {}, save = false;
|
63 | var path = argv.c || argv.config || process.env.SKALE_CONFIG || process.env.HOME + '/.skalerc';
|
64 | try { conf = JSON.parse(fs.readFileSync(path)); } catch (error) { save = true; }
|
65 | conf.host = argv.H || argv.host || process.env.SKALE_HOST || conf.host || die('Error: missing host');
|
66 | conf.port = argv.p || argv.port || process.env.SKALE_PORT || conf.port || die('Error: missing port');
|
67 | conf.key = argv.k || argv.key || conf.key;
|
68 | conf.ssl = argv.s || argv.ssl || (conf.ssl ? true : false);
|
69 | if (save || argv._[0] == 'init') fs.writeFileSync(path, JSON.stringify(conf, null, 2));
|
70 | return conf;
|
71 | }
|
72 |
|
73 | function run(src, args) {
|
74 | fs.readFile(src, {encoding: 'utf8'}, function (err, data) {
|
75 | if (err) throw err;
|
76 |
|
77 | var postdata = JSON.stringify({src: data, args: args});
|
78 |
|
79 | var options = {
|
80 | hostname: config.host,
|
81 | port: config.port,
|
82 | path: '/run',
|
83 | method: 'POST',
|
84 | headers: {
|
85 | 'X-Auth': config.key,
|
86 | 'Content-Type': 'application/json',
|
87 | 'Content-Length': Buffer.byteLength(postdata)
|
88 | }
|
89 | };
|
90 |
|
91 | var req = proto.request(options, function (res) {
|
92 | res.setEncoding('utf8');
|
93 | res.pipe(process.stdout);
|
94 | });
|
95 |
|
96 | req.on('error', function (err) {throw err;});
|
97 | req.end(postdata);
|
98 | });
|
99 | }
|