Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

fix(cli): add output for cli init #475

Merged
merged 1 commit into from
Sep 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/cli/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ module.exports = {
handler (argv) {
const path = utils.getRepoPath()

const log = utils.createLogger(true)
log(`initializing ipfs node at ${path}`)

const repo = new IpfsRepo(path, {
stores: Store
})
Expand All @@ -41,7 +44,8 @@ module.exports = {
ipfs.init({
bits: argv.bits,
force: argv.force,
emptyRepo: argv.emptyRepo
emptyRepo: argv.emptyRepo,
log
}, function (err) {
if (err) {
console.error(err.toString())
Expand Down
15 changes: 15 additions & 0 deletions src/cli/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,18 @@ exports.getIPFS = (callback) => {
exports.getRepoPath = () => {
return process.env.IPFS_PATH || os.homedir() + '/.ipfs'
}

exports.createLogger = (visible) => {
return (msg, newline) => {
if (newline === undefined) {
newline = true
}
if (visible) {
if (msg === undefined) {
msg = ''
}
msg = newline ? msg + '\n' : msg
process.stdout.write(msg)
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this just a console.log? All invisible logs should be through debug (http://npmjs.org/debug)

Copy link
Member Author

@victorb victorb Sep 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost, it's console log with the ability of deciding if next message should go on a new line or not. Necessary for the generating keys message, which prints "done" after it's done.

14 changes: 14 additions & 0 deletions src/core/ipfs/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const fs = require('fs')
const importer = require('ipfs-unixfs-engine').importer
const pull = require('pull-stream')
const file = require('pull-file')
const mh = require('multihashes')

module.exports = function init (self) {
return (opts, callback) => {
Expand All @@ -19,6 +20,7 @@ module.exports = function init (self) {

opts.emptyRepo = opts.emptyRepo || false
opts.bits = opts.bits || 2048
opts.log = opts.log || function () {}

let config
// Pre-set config values.
Expand Down Expand Up @@ -47,13 +49,16 @@ module.exports = function init (self) {

// Generate peer identity keypair + transform to desired format + add to config.
function generateAndSetKeypair () {
opts.log(`generating ${opts.bits}-bit RSA keypair...`, false)
var keys = peerId.create({
bits: opts.bits
})
config.Identity = {
PeerID: keys.toB58String(),
PrivKey: keys.privKey.bytes.toString('base64')
}
opts.log('done')
opts.log('peer identity: ' + config.Identity.PeerID)

writeVersion()
}
Expand Down Expand Up @@ -108,6 +113,15 @@ module.exports = function init (self) {
}),
pull.filter(Boolean),
importer(dag),
pull.through((el) => {
if (el.path === 'files/init-docs/docs') {
const hash = mh.toB58String(el.multihash)
opts.log('to get started, enter:')
opts.log()
opts.log(`\t jsipfs files cat /ipfs/${hash}/readme`)
opts.log()
}
}),
pull.onEnd((err) => {
if (err) return callback(err)

Expand Down