|
@@ -17,30 +17,30 @@
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
*/
|
|
|
|
|
|
-'use strict';
|
|
|
+'use strict'
|
|
|
|
|
|
-const path = require('path');
|
|
|
-const fs = require('fs');
|
|
|
-const assert = require('assert');
|
|
|
+const path = require('path')
|
|
|
+const fs = require('fs')
|
|
|
+const assert = require('assert')
|
|
|
|
|
|
-const { RuntimeApi } = require('@joystream/runtime-api');
|
|
|
+const { RuntimeApi } = require('@joystream/runtime-api')
|
|
|
|
|
|
-const meow = require('meow');
|
|
|
-const chalk = require('chalk');
|
|
|
-const _ = require('lodash');
|
|
|
+const meow = require('meow')
|
|
|
+const chalk = require('chalk')
|
|
|
+const _ = require('lodash')
|
|
|
|
|
|
-const debug = require('debug')('joystream:cli');
|
|
|
+const debug = require('debug')('joystream:cli')
|
|
|
|
|
|
// Project root
|
|
|
-const project_root = path.resolve(__dirname, '..');
|
|
|
+const project_root = path.resolve(__dirname, '..')
|
|
|
|
|
|
// Configuration (default)
|
|
|
-const pkg = require(path.resolve(project_root, 'package.json'));
|
|
|
+const pkg = require(path.resolve(project_root, 'package.json'))
|
|
|
|
|
|
// Parse CLI
|
|
|
const FLAG_DEFINITIONS = {
|
|
|
// TODO
|
|
|
-};
|
|
|
+}
|
|
|
|
|
|
const cli = meow(`
|
|
|
Usage:
|
|
@@ -59,172 +59,168 @@ const cli = meow(`
|
|
|
head Send a HEAD request for a file, and print headers.
|
|
|
Requires a storage node URL and a content ID.
|
|
|
`,
|
|
|
- { flags: FLAG_DEFINITIONS });
|
|
|
+ { flags: FLAG_DEFINITIONS })
|
|
|
|
|
|
-function assert_file(name, filename)
|
|
|
-{
|
|
|
- assert(filename, `Need a ${name} parameter to proceed!`);
|
|
|
- assert(fs.statSync(filename).isFile(), `Path "${filename}" is not a file, aborting!`);
|
|
|
+function assert_file (name, filename) {
|
|
|
+ assert(filename, `Need a ${name} parameter to proceed!`)
|
|
|
+ assert(fs.statSync(filename).isFile(), `Path "${filename}" is not a file, aborting!`)
|
|
|
}
|
|
|
|
|
|
const commands = {
|
|
|
'upload': async (runtime_api, url, filename, do_type_id) => {
|
|
|
// Check parameters
|
|
|
- assert_file('file', filename);
|
|
|
+ assert_file('file', filename)
|
|
|
|
|
|
- const size = fs.statSync(filename).size;
|
|
|
- console.log(`File "${filename}" is ` + chalk.green(size) + ' Bytes.');
|
|
|
+ const size = fs.statSync(filename).size
|
|
|
+ console.log(`File "${filename}" is ` + chalk.green(size) + ' Bytes.')
|
|
|
|
|
|
if (!do_type_id) {
|
|
|
- do_type_id = 1;
|
|
|
+ do_type_id = 1
|
|
|
}
|
|
|
- console.log('Data Object Type ID is: ' + chalk.green(do_type_id));
|
|
|
+
|
|
|
+ console.log('Data Object Type ID is: ' + chalk.green(do_type_id))
|
|
|
|
|
|
// Generate content ID
|
|
|
// FIXME this require path is like this because of
|
|
|
// https://github.com/Joystream/apps/issues/207
|
|
|
- const { ContentId } = require('@joystream/types/lib/media');
|
|
|
- var cid = ContentId.generate();
|
|
|
- cid = cid.encode().toString();
|
|
|
- console.log('Generated content ID: ' + chalk.green(cid));
|
|
|
+ const { ContentId } = require('@joystream/types/lib/media')
|
|
|
+ var cid = ContentId.generate()
|
|
|
+ cid = cid.encode().toString()
|
|
|
+ console.log('Generated content ID: ' + chalk.green(cid))
|
|
|
|
|
|
// Create Data Object
|
|
|
const data_object = await runtime_api.assets.createDataObject(
|
|
|
- runtime_api.identities.key.address, cid, do_type_id, size);
|
|
|
- console.log('Data object created.');
|
|
|
+ runtime_api.identities.key.address, cid, do_type_id, size)
|
|
|
+ console.log('Data object created.')
|
|
|
|
|
|
// TODO in future, optionally contact liaison here?
|
|
|
- const request = require('request');
|
|
|
- url = `${url}asset/v0/${cid}`;
|
|
|
- console.log('Uploading to URL', chalk.green(url));
|
|
|
+ const request = require('request')
|
|
|
+ url = `${url}asset/v0/${cid}`
|
|
|
+ console.log('Uploading to URL', chalk.green(url))
|
|
|
|
|
|
- const f = fs.createReadStream(filename);
|
|
|
+ const f = fs.createReadStream(filename)
|
|
|
const opts = {
|
|
|
url: url,
|
|
|
headers: {
|
|
|
'content-type': '',
|
|
|
- 'content-length': `${size}`,
|
|
|
+ 'content-length': `${size}`
|
|
|
},
|
|
|
- json: true,
|
|
|
- };
|
|
|
+ json: true
|
|
|
+ }
|
|
|
return new Promise((resolve, reject) => {
|
|
|
const r = request.put(opts, (error, response, body) => {
|
|
|
if (error) {
|
|
|
- reject(error);
|
|
|
- return;
|
|
|
+ reject(error)
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
- if (response.statusCode / 100 != 2) {
|
|
|
- reject(new Error(`${response.statusCode}: ${body.message || 'unknown reason'}`));
|
|
|
- return;
|
|
|
+ if (response.statusCode / 100 !== 2) {
|
|
|
+ reject(new Error(`${response.statusCode}: ${body.message || 'unknown reason'}`))
|
|
|
+ return
|
|
|
}
|
|
|
- console.log('Upload successful:', body.message);
|
|
|
- resolve();
|
|
|
- });
|
|
|
- f.pipe(r);
|
|
|
- });
|
|
|
+ console.log('Upload successful:', body.message)
|
|
|
+ resolve()
|
|
|
+ })
|
|
|
+ f.pipe(r)
|
|
|
+ })
|
|
|
},
|
|
|
|
|
|
'download': async (runtime_api, url, content_id, filename) => {
|
|
|
- const request = require('request');
|
|
|
- url = `${url}asset/v0/${content_id}`;
|
|
|
- console.log('Downloading URL', chalk.green(url), 'to', chalk.green(filename));
|
|
|
+ const request = require('request')
|
|
|
+ url = `${url}asset/v0/${content_id}`
|
|
|
+ console.log('Downloading URL', chalk.green(url), 'to', chalk.green(filename))
|
|
|
|
|
|
- const f = fs.createWriteStream(filename);
|
|
|
+ const f = fs.createWriteStream(filename)
|
|
|
const opts = {
|
|
|
url: url,
|
|
|
- json: true,
|
|
|
- };
|
|
|
+ json: true
|
|
|
+ }
|
|
|
return new Promise((resolve, reject) => {
|
|
|
const r = request.get(opts, (error, response, body) => {
|
|
|
if (error) {
|
|
|
- reject(error);
|
|
|
- return;
|
|
|
+ reject(error)
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
- console.log('Downloading', chalk.green(response.headers['content-type']), 'of size', chalk.green(response.headers['content-length']), '...');
|
|
|
+ console.log('Downloading', chalk.green(response.headers['content-type']), 'of size', chalk.green(response.headers['content-length']), '...')
|
|
|
|
|
|
f.on('error', (err) => {
|
|
|
- reject(err);
|
|
|
- });
|
|
|
+ reject(err)
|
|
|
+ })
|
|
|
|
|
|
f.on('finish', () => {
|
|
|
- if (response.statusCode / 100 != 2) {
|
|
|
- reject(new Error(`${response.statusCode}: ${body.message || 'unknown reason'}`));
|
|
|
- return;
|
|
|
+ if (response.statusCode / 100 !== 2) {
|
|
|
+ reject(new Error(`${response.statusCode}: ${body.message || 'unknown reason'}`))
|
|
|
+ return
|
|
|
}
|
|
|
- console.log('Download completed.');
|
|
|
- resolve();
|
|
|
- });
|
|
|
- });
|
|
|
- r.pipe(f);
|
|
|
- });
|
|
|
+ console.log('Download completed.')
|
|
|
+ resolve()
|
|
|
+ })
|
|
|
+ })
|
|
|
+ r.pipe(f)
|
|
|
+ })
|
|
|
},
|
|
|
|
|
|
'head': async (runtime_api, url, content_id) => {
|
|
|
- const request = require('request');
|
|
|
- url = `${url}asset/v0/${content_id}`;
|
|
|
- console.log('Checking URL', chalk.green(url), '...');
|
|
|
+ const request = require('request')
|
|
|
+ url = `${url}asset/v0/${content_id}`
|
|
|
+ console.log('Checking URL', chalk.green(url), '...')
|
|
|
|
|
|
const opts = {
|
|
|
url: url,
|
|
|
- json: true,
|
|
|
- };
|
|
|
+ json: true
|
|
|
+ }
|
|
|
return new Promise((resolve, reject) => {
|
|
|
const r = request.head(opts, (error, response, body) => {
|
|
|
if (error) {
|
|
|
- reject(error);
|
|
|
- return;
|
|
|
+ reject(error)
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
- if (response.statusCode / 100 != 2) {
|
|
|
- reject(new Error(`${response.statusCode}: ${body.message || 'unknown reason'}`));
|
|
|
- return;
|
|
|
+ if (response.statusCode / 100 !== 2) {
|
|
|
+ reject(new Error(`${response.statusCode}: ${body.message || 'unknown reason'}`))
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
for (var propname in response.headers) {
|
|
|
- console.log(` ${chalk.yellow(propname)}: ${response.headers[propname]}`);
|
|
|
+ console.log(` ${chalk.yellow(propname)}: ${response.headers[propname]}`)
|
|
|
}
|
|
|
|
|
|
- resolve();
|
|
|
- });
|
|
|
- });
|
|
|
- },
|
|
|
-
|
|
|
-};
|
|
|
-
|
|
|
+ resolve()
|
|
|
+ })
|
|
|
+ })
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
-async function main()
|
|
|
-{
|
|
|
+async function main () {
|
|
|
// Key file is at the first instance.
|
|
|
- const key_file = cli.input[0];
|
|
|
- assert_file('key file', key_file);
|
|
|
+ const key_file = cli.input[0]
|
|
|
+ assert_file('key file', key_file)
|
|
|
|
|
|
// Create runtime API.
|
|
|
- const runtime_api = await RuntimeApi.create({ account_file: key_file });
|
|
|
+ const runtime_api = await RuntimeApi.create({ account_file: key_file })
|
|
|
|
|
|
// Simple CLI commands
|
|
|
- const command = cli.input[1];
|
|
|
+ const command = cli.input[1]
|
|
|
if (!command) {
|
|
|
- throw new Error('Need a command to run!');
|
|
|
+ throw new Error('Need a command to run!')
|
|
|
}
|
|
|
|
|
|
if (commands.hasOwnProperty(command)) {
|
|
|
// Command recognized
|
|
|
- const args = _.clone(cli.input).slice(2);
|
|
|
- await commands[command](runtime_api, ...args);
|
|
|
- }
|
|
|
- else {
|
|
|
- throw new Error(`Command "${command}" not recognized, aborting!`);
|
|
|
+ const args = _.clone(cli.input).slice(2)
|
|
|
+ await commands[command](runtime_api, ...args)
|
|
|
+ } else {
|
|
|
+ throw new Error(`Command "${command}" not recognized, aborting!`)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
main()
|
|
|
.then(() => {
|
|
|
- console.log('Process exiting gracefully.');
|
|
|
- process.exit(0);
|
|
|
+ console.log('Process exiting gracefully.')
|
|
|
+ process.exit(0)
|
|
|
})
|
|
|
.catch((err) => {
|
|
|
- console.error(chalk.red(err.stack));
|
|
|
- process.exit(-1);
|
|
|
- });
|
|
|
+ console.error(chalk.red(err.stack))
|
|
|
+ process.exit(-1)
|
|
|
+ })
|