Clean up logs, add debug option

This commit is contained in:
Basti 2019-07-02 18:41:48 +02:00
parent 4fb03c5677
commit 7cd569ee21
No known key found for this signature in database
GPG Key ID: BE4634D632D39B67
4 changed files with 29 additions and 18 deletions

View File

@ -1,5 +1,4 @@
#! /usr/bin/env node const debug = require('debug')('ipfs-pinner');
const Kredits = require('kredits-contracts'); const Kredits = require('kredits-contracts');
const IpfsPinner = require('./lib/ipfs-pinner'); const IpfsPinner = require('./lib/ipfs-pinner');
@ -32,18 +31,24 @@ console.log(`Using IPFS:`, ipfsConfig);
{ network: argv.network, rpcUrl: argv.rpcUrl }, { network: argv.network, rpcUrl: argv.rpcUrl },
{ apm: argv.apm, ipfsConfig: ipfsConfig, addresses: { Kernel: argv.daoAddress } } { apm: argv.apm, ipfsConfig: ipfsConfig, addresses: { Kernel: argv.daoAddress } }
).init(); ).init();
await kredits.ipfs._ipfsAPI.id(); // check the connection to the IPFS client TODO: redesign IPFS wrapper API and not use an internal attribute
// check the connection to the IPFS client
// TODO redesign IPFS wrapper API and do not use an internal attribute
await kredits.ipfs._ipfsAPI.id();
const ipfsPinner = new IpfsPinner(kredits); const ipfsPinner = new IpfsPinner(kredits);
ipfsPinner.pinAll().then(pins => { ipfsPinner.pinAll().then(pins => {
console.log('Pinned', JSON.stringify(pins, null, 2)); console.log(`Pinned ${pins.length} existing documents`);
}); });
ipfsPinner.monitor((pin) => {
console.log('Pinned', JSON.stringify(pin)); ipfsPinner.monitor(pin => {
console.log('Pinned a new document:', pin[0]["hash"]);
}); });
console.log(`Subscribed to DAO: ${kredits.Kernel.contract.address}`); console.log(`Subscribed to DAO: ${kredits.Kernel.contract.address}`);
} catch(e) { } catch(e) {
console.log('Failed to start pinner'); console.log('Failed to start');
console.log(e); console.log(e);
process.exit(1); process.exit(1);
} }

View File

@ -1,15 +1,18 @@
const debug = require('debug')('ipfs-pinner');
class IpfsPinner { class IpfsPinner {
constructor (kredits, ipfsApi) { constructor (kredits, ipfsApi) {
this.kredits = kredits; this.kredits = kredits;
this.ipfsApi = ipfsApi || this.kredits.ipfs; this.ipfsApi = ipfsApi || this.kredits.ipfs;
} }
pinAll () { async pinAll () {
return Promise.all([ const contributorHashes = await this._pinAllFromContract(this.kredits.Contributor);
this._pinAllFromContract(this.kredits.Contributor), const contributionHashes = await this._pinAllFromContract(this.kredits.Contribution);
this._pinAllFromContract(this.kredits.Contribution), const proposalHashes = await this._pinAllFromContract(this.kredits.Proposal);
this._pinAllFromContract(this.kredits.Proposal)
]); return contributorHashes.concat(contributionHashes)
.concat(proposalHashes);
} }
monitor (callback) { monitor (callback) {
@ -31,13 +34,13 @@ class IpfsPinner {
} }
_pinAllFromContract (contract) { _pinAllFromContract (contract) {
console.log(`Pinning data from ${contract.constructor.name}...`); debug(`Pinning data from ${contract.constructor.name}...`);
return contract.count.then(count => { return contract.count.then(count => {
let promises = [...Array(count).keys()].map(i => { let promises = [...Array(count).keys()].map(i => {
let id = i + 1; // 0 => 1 - ids start with 1 and not with 0 let id = i + 1; // 0 => 1 - ids start with 1 and not with 0
console.log(`Loading ${contract.constructor.name} #${id}`); debug(`Loading ${contract.constructor.name} #${id}`);
return contract.getData(id).then(data => { return contract.getData(id).then(data => {
console.log(`Pinning ${contract.constructor.name} #${id}`); debug(`Pinning ${contract.constructor.name} #${id}`);
return this.ipfsApi.pin(data); return this.ipfsApi.pin(data);
}); });
}); });

2
package-lock.json generated
View File

@ -1,5 +1,5 @@
{ {
"name": "kredits-ipfs-pinner", "name": "@67p/kredits-ipfs-pinner",
"version": "1.0.0", "version": "1.0.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,

View File

@ -1,9 +1,11 @@
{ {
"name": "@67p/kredits-ipfs-pinner", "name": "@67p/kredits-ipfs-pinner",
"version": "1.0.0", "version": "1.0.0",
"description": "Tool to pin IPFS data of a kredits organization on a connected IPFS node.", "description": "Pins IPFS data of a Kredits organization on an IPFS node",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"start": "node index.js",
"debug": "DEBUG=ipfs-pinner node index.js",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"bin": { "bin": {
@ -18,6 +20,7 @@
], ],
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"debug": "^4.1.1",
"kredits-contracts": "^5.4.0", "kredits-contracts": "^5.4.0",
"yargs": "^13.2.4" "yargs": "^13.2.4"
} }