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

Refactor cli to use yargs #390

Merged
merged 5 commits into from
Aug 10, 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
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,26 @@
"libp2p-ipfs-browser": "^0.12.1",
"lodash.get": "^4.4.0",
"lodash.set": "^4.3.0",
"lodash.sortby": "^4.6.1",
"mafmt": "^2.1.1",
"multihashes": "^0.2.2",
"map-limit": "0.0.1",
"multiaddr": "^2.0.2",
"multihashes": "^0.2.2",
"path-exists": "^3.0.0",
"peer-book": "^0.3.0",
"peer-id": "^0.7.0",
"peer-info": "^0.7.0",
"promisify-es6": "^1.0.1",
"read-pkg-up": "^1.0.1",
"readable-stream": "1.1.13",
"ronin": "^0.3.11",
"run-parallel": "^1.1.6",
"run-parallel-limit": "^1.0.3",
"run-series": "^1.1.4",
"run-waterfall": "^1.1.3",
"temp": "^0.8.3",
"through2": "^2.0.1"
"through2": "^2.0.1",
"update-notifier": "^1.0.2",
"yargs": "^4.8.1"
},
"contributors": [
"Andrew de Andrade <andrew@deandrade.com.br>",
Expand Down
22 changes: 15 additions & 7 deletions src/cli/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

'use strict'

const ronin = require('ronin')
const yargs = require('yargs')
const updateNotifier = require('update-notifier')
const readPkgUp = require('read-pkg-up')

const cli = ronin(__dirname)
const pkg = readPkgUp.sync().pkg
updateNotifier({
pkg,
updateCheckInterval: 1000 * 60 * 60 * 24 * 7 // 1 week
}).notify()
Copy link
Member

Choose a reason for hiding this comment

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

👍


cli.run()

// cli.autoupdate(function () {
// cli.run()
// })
yargs
.commandDir('commands')
.demand(1)
.help()
.strict()
.completion()
.argv
15 changes: 15 additions & 0 deletions src/cli/commands/bitswap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

module.exports = {
command: 'bitswap',

description: 'A set of commands to manipulate the bitswap agent.',

builder (yargs) {
return yargs
.commandDir('bitswap')
Copy link
Member

Choose a reason for hiding this comment

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

Is building the subcommands inside the main command a best practice? Didn't see it on the module description.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes this this is the recommended (and as far as I can tell only way) to do this.

},

handler (argv) {
}
}
14 changes: 7 additions & 7 deletions src/cli/commands/bitswap/stat.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict'

const Command = require('ronin').Command
const utils = require('../../utils')

module.exports = Command.extend({
desc: 'Show some diagnostic information on the bitswap agent.',
module.exports = {
command: 'stat',

options: {
},
describe: 'Show some diagnostic information on the bitswap agent.',

run: () => {
builder: {},

handler () {
utils.getIPFS((err, ipfs) => {
if (err) {
throw err
Expand All @@ -35,4 +35,4 @@ bitswap status
})
})
}
})
}
15 changes: 5 additions & 10 deletions src/cli/commands/bitswap/unwant.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
'use strict'

const Command = require('ronin').Command
const utils = require('../../utils')

module.exports = Command.extend({
desc: 'Remove a given block from your wantlist.',
module.exports = {
command: 'unwant <key>',

options: {
key: {
required: true
}
},
describe: 'Remove a given block from your wantlist.',

run: (key) => {
handler (argv) {
utils.getIPFS((err, ipfs) => {
if (err) {
throw err
Expand All @@ -21,4 +16,4 @@ module.exports = Command.extend({
throw new Error('Not implemented yet')
})
}
})
}
19 changes: 13 additions & 6 deletions src/cli/commands/bitswap/wantlist.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
'use strict'

const Command = require('ronin').Command
const utils = require('../../utils')

module.exports = Command.extend({
desc: 'Print out all blocks currently on the bitswap wantlist for the local peer.',
module.exports = {
command: 'wantlist',

options: {
describe: 'Print out all blocks currently on the bitswap wantlist for the local peer.',

builder: {
peer: {
alias: 'p',
describe: 'Specify which peer to show wantlist for.',
type: 'string'
}
},

run: () => {
handler (argv) {
// TODO: handle argv.peer
utils.getIPFS((err, ipfs) => {
if (err) {
throw err
Expand All @@ -23,4 +30,4 @@ module.exports = Command.extend({
})
})
}
})
}
15 changes: 15 additions & 0 deletions src/cli/commands/block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

module.exports = {
command: 'block',

description: 'Manipulate raw IPFS blocks.',

builder (yargs) {
return yargs
.commandDir('block')
},

handler (argv) {
}
}
19 changes: 8 additions & 11 deletions src/cli/commands/block/get.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
'use strict'

const Command = require('ronin').Command
const utils = require('../../utils')
const bs58 = require('bs58')
const debug = require('debug')
const log = debug('cli:block')
log.error = debug('cli:block:error')

module.exports = Command.extend({
desc: 'Get a raw IPFS block',
module.exports = {
command: 'get <key>',

options: {},
describe: 'Get a raw IPFS block',

run: (key) => {
if (!key) {
throw new Error("Argument 'key' is required")
}
builder: {},

handler (argv) {
utils.getIPFS((err, ipfs) => {
if (err) {
throw err
}

const mh = utils.isDaemonOn()
? key
: new Buffer(bs58.decode(key))
? argv.key
: new Buffer(bs58.decode(argv.key))

ipfs.block.get(mh, (err, block) => {
if (err) {
Expand All @@ -40,4 +37,4 @@ module.exports = Command.extend({
})
})
}
})
}
17 changes: 9 additions & 8 deletions src/cli/commands/block/put.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const Command = require('ronin').Command
const utils = require('../../utils')
const bs58 = require('bs58')
const bl = require('bl')
Expand Down Expand Up @@ -38,14 +37,16 @@ function addBlock (buf) {
})
}

module.exports = Command.extend({
desc: 'Stores input as an IPFS block',
module.exports = {
command: 'put [data]',

options: {},
describe: 'Stores input as an IPFS block',

run: (filePath) => {
if (filePath) {
return addBlock(fs.readFileSync(filePath))
builder: {},

handler (argv) {
if (argv.data) {
return addBlock(fs.readFileSync(argv.data))
}

process.stdin.pipe(bl((err, input) => {
Expand All @@ -56,4 +57,4 @@ module.exports = Command.extend({
addBlock(input)
}))
}
})
}
19 changes: 8 additions & 11 deletions src/cli/commands/block/rm.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
'use strict'

const Command = require('ronin').Command
const utils = require('../../utils')
const bs58 = require('bs58')
const debug = require('debug')
const log = debug('cli:block')
log.error = debug('cli:block:error')

module.exports = Command.extend({
desc: 'Remove a raw IPFS block',
module.exports = {
command: 'rm <key>',

options: {},
describe: 'Remove a raw IPFS block',

run: (key) => {
if (!key) {
throw new Error("Argument 'key' is required")
}
builder: {},

handler (argv) {
utils.getIPFS((err, ipfs) => {
if (err) {
throw err
Expand All @@ -27,15 +24,15 @@ module.exports = Command.extend({
throw new Error('rm block with daemon running is not yet implemented')
}

const mh = new Buffer(bs58.decode(key))
const mh = new Buffer(bs58.decode(argv.key))

ipfs.block.del(mh, (err) => {
if (err) {
throw err
}

console.log('removed', key)
console.log('removed', argv.key)
})
})
}
})
}
19 changes: 8 additions & 11 deletions src/cli/commands/block/stat.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
'use strict'

const Command = require('ronin').Command
const utils = require('../../utils')
const bs58 = require('bs58')
const debug = require('debug')
const log = debug('cli:block')
log.error = debug('cli:block:error')

module.exports = Command.extend({
desc: 'Print information of a raw IPFS block',
module.exports = {
command: 'stat <key>',

options: {},
describe: 'Print information of a raw IPFS block',

run: (key) => {
if (!key) {
throw new Error("Argument 'key' is required")
}
builder: {},

handler (argv) {
utils.getIPFS((err, ipfs) => {
if (err) {
throw err
}

const mh = utils.isDaemonOn()
? key
: new Buffer(bs58.decode(key))
? argv.key
: new Buffer(bs58.decode(argv.key))

ipfs.block.stat(mh, (err, block) => {
if (err) {
Expand All @@ -41,4 +38,4 @@ module.exports = Command.extend({
})
})
}
})
}
15 changes: 15 additions & 0 deletions src/cli/commands/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

module.exports = {
command: 'bootstrap',

description: 'Show or edit the list of bootstrap peers.',

builder (yargs) {
return yargs
.commandDir('bootstrap')
},

handler (argv) {
}
}
Loading