2018-09-05 06:35:07 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2018-09-07 08:17:00 +00:00
|
|
|
const SwarmJS = require('../src/index.js');
|
2018-09-05 06:35:07 +00:00
|
|
|
const swarmhash = require('swarmhash');
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
const yargs = require('yargs')
|
|
|
|
.usage('Usage: $0 [command]')
|
|
|
|
.command('get <gateway> <hash>', 'Download content', {
|
|
|
|
gateway: {
|
|
|
|
alias: 'g',
|
|
|
|
default: 'http://swarm-gateways.net'
|
|
|
|
},
|
|
|
|
hash: {
|
|
|
|
alias: 'h'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.command('put <gateway> <filename>', 'Upload file', {
|
|
|
|
gateway: {
|
|
|
|
alias: 'g',
|
|
|
|
default: 'http://swarm-gateways.net'
|
|
|
|
},
|
|
|
|
filename: {
|
|
|
|
alias: 'f'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.command('calculate <filename>', 'Calculate the hash only without uploading')
|
|
|
|
.strict()
|
|
|
|
.version()
|
|
|
|
.showHelpOnFail(false, 'Specify --help for available options')
|
|
|
|
.help()
|
2018-09-11 02:20:31 +00:00
|
|
|
.demand(1, 'Must provide a command');
|
2018-09-05 06:35:07 +00:00
|
|
|
|
|
|
|
const argv = yargs.argv;
|
|
|
|
const gateway = argv.gateway;
|
|
|
|
const command = argv._[0];
|
2018-09-11 02:20:31 +00:00
|
|
|
const swarmjs = new SwarmJS({gateway});
|
2018-09-05 06:35:07 +00:00
|
|
|
|
|
|
|
function abort(msg) {
|
|
|
|
console.log(msg || 'Error occured');
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (command) {
|
|
|
|
case 'get':
|
2018-09-07 08:17:00 +00:00
|
|
|
swarmjs.downloadRaw(argv.hash, function (err, ret) {
|
2018-09-05 06:35:07 +00:00
|
|
|
if (err) {
|
2018-09-11 02:20:31 +00:00
|
|
|
abort('Failed to download: ' + err);
|
2018-09-05 06:35:07 +00:00
|
|
|
} else {
|
2018-09-11 02:20:31 +00:00
|
|
|
console.log(ret);
|
2018-09-05 06:35:07 +00:00
|
|
|
}
|
2018-09-11 02:20:31 +00:00
|
|
|
});
|
|
|
|
break;
|
2018-09-05 06:35:07 +00:00
|
|
|
case 'put':
|
2018-09-07 08:17:00 +00:00
|
|
|
swarmjs.uploadRaw(fs.readFileSync(argv.filename), function (err, ret) {
|
2018-09-05 06:35:07 +00:00
|
|
|
if (err) {
|
2018-09-11 02:20:31 +00:00
|
|
|
console.log('Failed to upload: ' + err);
|
2018-09-05 06:35:07 +00:00
|
|
|
} else {
|
2018-09-11 02:20:31 +00:00
|
|
|
console.log('Swarm hash: ' + ret);
|
2018-09-05 06:35:07 +00:00
|
|
|
}
|
2018-09-11 02:20:31 +00:00
|
|
|
});
|
|
|
|
break;
|
2018-09-05 06:35:07 +00:00
|
|
|
case 'calculate':
|
2018-09-11 02:20:31 +00:00
|
|
|
console.log('Swarm hash: ' + swarmhash(fs.readFileSync(argv.filename)).toString('hex'));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.log('Invalid arguments. Type \'swarmjs --help\' for valid options');
|
2018-09-05 06:35:07 +00:00
|
|
|
}
|