From 7cd569ee212bc29ee1d0a850d4dea5e39a491030 Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Tue, 2 Jul 2019 18:41:48 +0200 Subject: [PATCH 1/3] Clean up logs, add debug option --- index.js | 19 ++++++++++++------- lib/ipfs-pinner.js | 21 ++++++++++++--------- package-lock.json | 2 +- package.json | 5 ++++- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 1ac7318..77969a5 100755 --- a/index.js +++ b/index.js @@ -1,5 +1,4 @@ -#! /usr/bin/env node - +const debug = require('debug')('ipfs-pinner'); const Kredits = require('kredits-contracts'); const IpfsPinner = require('./lib/ipfs-pinner'); @@ -32,18 +31,24 @@ console.log(`Using IPFS:`, ipfsConfig); { network: argv.network, rpcUrl: argv.rpcUrl }, { apm: argv.apm, ipfsConfig: ipfsConfig, addresses: { Kernel: argv.daoAddress } } ).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); 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}`); } catch(e) { - console.log('Failed to start pinner'); + console.log('Failed to start'); console.log(e); process.exit(1); } diff --git a/lib/ipfs-pinner.js b/lib/ipfs-pinner.js index 3df2b75..48765f2 100644 --- a/lib/ipfs-pinner.js +++ b/lib/ipfs-pinner.js @@ -1,15 +1,18 @@ +const debug = require('debug')('ipfs-pinner'); + class IpfsPinner { constructor (kredits, ipfsApi) { this.kredits = kredits; this.ipfsApi = ipfsApi || this.kredits.ipfs; } - pinAll () { - return Promise.all([ - this._pinAllFromContract(this.kredits.Contributor), - this._pinAllFromContract(this.kredits.Contribution), - this._pinAllFromContract(this.kredits.Proposal) - ]); + async pinAll () { + const contributorHashes = await this._pinAllFromContract(this.kredits.Contributor); + const contributionHashes = await this._pinAllFromContract(this.kredits.Contribution); + const proposalHashes = await this._pinAllFromContract(this.kredits.Proposal); + + return contributorHashes.concat(contributionHashes) + .concat(proposalHashes); } monitor (callback) { @@ -31,13 +34,13 @@ class IpfsPinner { } _pinAllFromContract (contract) { - console.log(`Pinning data from ${contract.constructor.name}...`); + debug(`Pinning data from ${contract.constructor.name}...`); return contract.count.then(count => { let promises = [...Array(count).keys()].map(i => { 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 => { - console.log(`Pinning ${contract.constructor.name} #${id}`); + debug(`Pinning ${contract.constructor.name} #${id}`); return this.ipfsApi.pin(data); }); }); diff --git a/package-lock.json b/package-lock.json index 5e6f386..1a2c284 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "kredits-ipfs-pinner", + "name": "@67p/kredits-ipfs-pinner", "version": "1.0.0", "lockfileVersion": 1, "requires": true, diff --git a/package.json b/package.json index cd57abb..52408c3 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,11 @@ { "name": "@67p/kredits-ipfs-pinner", "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", "scripts": { + "start": "node index.js", + "debug": "DEBUG=ipfs-pinner node index.js", "test": "echo \"Error: no test specified\" && exit 1" }, "bin": { @@ -18,6 +20,7 @@ ], "license": "MIT", "dependencies": { + "debug": "^4.1.1", "kredits-contracts": "^5.4.0", "yargs": "^13.2.4" } From 43a2cdc949b2844e23d3ce124e87b2e5b16efa80 Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Tue, 2 Jul 2019 18:45:24 +0200 Subject: [PATCH 2/3] Update README --- README.md | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index faaafc3..a233720 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,24 @@ -# Kredits IPFS pinner +# Kredits IPFS Pinner -This tool pins the IPFS hashes of a Kredits organisation on a IPFS node. +This tool pins the IPFS hashes of a Kredits organisation on an IPFS node. ## Usage - $ kredits-ipfs-pinner --help +### With NPX magic + npx @67p/kredits-ipfs-pinner + +### Global install + + npm install -g kredits-ipfs-pinner + kredits-ipfs-pinner + +### From repo + +Clone the Git repository, then use npm scripts from its root directory. + + npm start + +Or with debug output: + + npm run debug From 4d5976bcef18af495218554fe868f4e6ed3fba51 Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Tue, 2 Jul 2019 18:51:29 +0200 Subject: [PATCH 3/3] Add note about IPFS --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index a233720..334be2d 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,9 @@ This tool pins the IPFS hashes of a Kredits organisation on an IPFS node. ## Usage +Make sure you have a local IPFS node running. (See `--help` for custom IPFS +config flags, in case it is not running on localhost with default ports.) + ### With NPX magic npx @67p/kredits-ipfs-pinner