From 3b505f691ba52c4dad9a72eb110de1dc8ef270a1 Mon Sep 17 00:00:00 2001 From: Joe Hand Date: Fri, 9 Dec 2016 16:12:18 -0800 Subject: [PATCH] Add options (#20) * add version, quiet, port, utp options * make quiet, utp booleans * start with tests * fix create. add test for port * add usage * skip port tests for now * add usage & doctor tests --- bin/cli.js | 17 +++++++++++++++-- lib/commands/create.js | 3 ++- lib/commands/sync.js | 2 +- lib/download.js | 3 ++- lib/share.js | 3 ++- lib/usage.js | 11 ++++++++++- tests/clone.js | 11 +++++++++++ tests/create.js | 16 ++++++++++++++++ tests/doctor.js | 29 +++++++++++++++++++++++++++++ tests/misc/usage.js | 0 tests/sync-owner.js | 29 +++++++++++++++++++++++++++++ tests/usage.js | 36 ++++++++++++++++++++++++++++++++++++ 12 files changed, 153 insertions(+), 7 deletions(-) create mode 100644 tests/doctor.js delete mode 100644 tests/misc/usage.js create mode 100644 tests/usage.js diff --git a/bin/cli.js b/bin/cli.js index 9af7959..111547e 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -7,9 +7,22 @@ process.title = 'dat-next' var config = { defaults: [ { name: 'dir', default: process.cwd() }, - { name: 'logspeed', default: 200 } + { name: 'logspeed', default: 200 }, + { name: 'port', default: 3282 }, + { name: 'quiet', default: false, boolean: true }, + { name: 'utp', default: true, boolean: true } ], - root: require('../lib/usage'), + root: { + options: [ + { + name: 'version', + boolean: true, + default: false, + abbr: 'v' + } + ], + command: require('../lib/usage') + }, none: require('../lib/usage'), commands: [ require('../lib/commands/clone'), diff --git a/lib/commands/create.js b/lib/commands/create.js index 3acb9f3..cde1a90 100644 --- a/lib/commands/create.js +++ b/lib/commands/create.js @@ -27,7 +27,7 @@ function create (opts) { '', // Importing Progress '' // Total Size ] - var log = logger(output, {debug: false, quiet: false}) + var log = logger(output, {debug: false, quiet: opts.quiet}) // UI Elements var importUI = ui.importProgress() @@ -46,6 +46,7 @@ function create (opts) { output[0] = `Dat ${opts.live !== false ? '' : 'Snapshot'} Archive initialized: ${dat.path}` if (dat.key) output[1] = ui.link(dat.key) + '\n' else output[1] = 'Creating link...' + '\n' + if (opts.quiet && dat.key) process.stdout.write(ui.link(dat.key)) // Not importing files. Just create .dat, print info, and exit. if (!opts.import) return exit() diff --git a/lib/commands/sync.js b/lib/commands/sync.js index fb595b1..79b7e61 100644 --- a/lib/commands/sync.js +++ b/lib/commands/sync.js @@ -16,7 +16,7 @@ module.exports = { // Gets overwritten by logger. // Logging starts after Dat cb for lib/download sync // So we need this to show something right away - process.stdout.write('Starting Dat...') + if (!opts.quiet) process.stdout.write('Starting Dat...') // Set default options (some of these may be exposed to CLI eventually) opts.resume = true // sync must always be a resumed archive diff --git a/lib/download.js b/lib/download.js index 9c4e1f0..d1e8b8c 100644 --- a/lib/download.js +++ b/lib/download.js @@ -32,7 +32,7 @@ module.exports = function (type, opts, dat) { [] // Shows network information ] var progressOutput = output[0] // shortcut for progress output - var log = logger(output, {debug: false, quiet: false}) + var log = logger(output, {debug: false, quiet: opts.quiet}) // UI Elements var bar = ui.bar() @@ -58,6 +58,7 @@ module.exports = function (type, opts, dat) { var niceType = (type === 'clone') ? 'Cloning' : type.charAt(0).toUpperCase() + type.slice(1) + 'ing' progressOutput[0] = `${niceType} Dat Archive: ${dat.path}` progressOutput[1] = ui.link(dat.key) + '\n' + if (opts.quiet && type !== 'clone') process.stdout.write(ui.link(dat.key)) // Stats stats = dat.trackStats() diff --git a/lib/share.js b/lib/share.js index d0edbb7..9017dca 100644 --- a/lib/share.js +++ b/lib/share.js @@ -26,7 +26,7 @@ module.exports = function sync (type, opts, dat) { [] // Shows network information ] var progressOutput = output[0] // shortcut for progress output - var log = logger(output, {debug: false, quiet: false}) + var log = logger(output, {debug: false, quiet: opts.quiet}) // UI Elements var importUI = ui.importProgress() @@ -49,6 +49,7 @@ module.exports = function sync (type, opts, dat) { // General Archive Info progressOutput[0] = `Syncing Dat Archive: ${dat.path}` progressOutput[1] = ui.link(dat.key) + '\n' + if (opts.quiet) process.stdout.write(ui.link(dat.key)) // Stats (used for network + download) stats = dat.trackStats() diff --git a/lib/usage.js b/lib/usage.js index 5fcec41..b832459 100644 --- a/lib/usage.js +++ b/lib/usage.js @@ -1,4 +1,9 @@ -module.exports = function () { +module.exports = function (opts) { + if (opts.version) { + var pkg = require('../package.json') + console.error(pkg.version) + process.exit(1) + } console.error('Usage: dat-next [options]') console.error(' dat-next create create a local dat') console.error(' dat-next snapshot create a local dat snapshot') @@ -8,8 +13,12 @@ module.exports = function () { console.error(' dat-next doctor run the dat network doctor') console.error(' dat-next register register for an account') console.error(' dat-next login login to an account') + console.error(' dat-next --version,-v get your dat-next version') console.error('') console.error(' --dir= set directory (all commands)') console.error(' --no-import do not import files (create, sync)') + console.error(' --quiet only print out the dat link') + console.error(' --port=3282 set the port for discovery') + console.error(' --no-utp do not use utp for network connections') process.exit(1) } diff --git a/tests/clone.js b/tests/clone.js index 92d7a7e..cecde02 100644 --- a/tests/clone.js +++ b/tests/clone.js @@ -87,6 +87,17 @@ test('clone - specify dir', function (t) { st.end() }) +test('clone - quiet', function (t) { + var key = shareDat.key.toString('hex') + var customDir = 'shhhh_dir' + var cmd = dat + ' clone ' + key + ' ' + customDir + ' --quiet' + var st = spawn(t, cmd, {cwd: baseTestDir}) + st.succeeds('exits after finishing download') + st.stdout.empty() + st.stderr.empty() + st.end() +}) + test('close sharer', function (t) { shareDat.close(function () { rimraf.sync(path.join(shareDat.path, '.dat')) diff --git a/tests/create.js b/tests/create.js index 37d8978..1359bd1 100644 --- a/tests/create.js +++ b/tests/create.js @@ -69,6 +69,22 @@ test('create - sync after create ok', function (t) { st.end() }) +test('create - quiet only prints link', function (t) { + rimraf.sync(path.join(fixtures, '.dat')) + var cmd = dat + ' create --quiet' + var st = spawn(t, cmd, {cwd: fixtures}) + + st.stderr.empty() + st.stdout.match(function (output) { + var link = help.matchLink(output) + if (!link) return false + t.ok(link, 'prints link') + st.kill() + return true + }) + st.end() +}) + test.onFinish(function () { rimraf.sync(path.join(fixtures, '.dat')) }) diff --git a/tests/doctor.js b/tests/doctor.js new file mode 100644 index 0000000..f084235 --- /dev/null +++ b/tests/doctor.js @@ -0,0 +1,29 @@ +var path = require('path') +var test = require('tape') +var spawn = require('./helpers/spawn.js') +var help = require('./helpers') + +var dat = path.resolve(path.join(__dirname, '..', 'bin', 'cli.js')) + +test('misc - doctor option works ', function (t) { + var st = spawn(t, dat + ' doctor', {end: false}) + st.stdout.match(function (output) { + var key = help.matchLink(output) + if (!key) return false + startPhysiciansAssistant(key) + return true + }, 'doctor started') + + function startPhysiciansAssistant (link) { + var assist = spawn(t, dat + ' doctor ' + link, {end: false}) + assist.stdout.match(function (output) { + if (output.indexOf('Public IP:') > -1) { + st.kill() + return true + } + }, 'download one started') + assist.end(function () { + t.end() + }) + } +}) diff --git a/tests/misc/usage.js b/tests/misc/usage.js deleted file mode 100644 index e69de29..0000000 diff --git a/tests/sync-owner.js b/tests/sync-owner.js index 4b323b8..ec686ea 100644 --- a/tests/sync-owner.js +++ b/tests/sync-owner.js @@ -1,4 +1,5 @@ var fs = require('fs') +var net = require('net') var path = require('path') var test = require('tape') var mkdirp = require('mkdirp') @@ -129,6 +130,34 @@ test('sync-owner - imports after no-import create', function (t) { st.end() }) +test('sync-owner - port and utp options', function (t) { + var port = 3281 + var cmd = dat + ' sync --port ' + port + ' --no-utp' + var st = spawn(t, cmd, {cwd: fixtures, end: false}) + st.stderr.empty() + + var server = net.createServer() + server.once('error', function (err) { + if (err.code !== 'EADDRINUSE') return t.error(err) + t.skip('TODO: correct port in use') + done() + }) + server.once('listening', function () { + t.skip(`TODO: port ${server.address().port} should be in use`) + done() + }) + server.listen(port) + + t.skip('TODO: check utp option') // TODO: how to check utp? + + function done () { + server.close(function () { + st.kill() + t.end() + }) + } +}) + test.onFinish(function () { rimraf.sync(path.join(fixtures, '.dat')) }) diff --git a/tests/usage.js b/tests/usage.js new file mode 100644 index 0000000..b89f0c9 --- /dev/null +++ b/tests/usage.js @@ -0,0 +1,36 @@ +var path = require('path') +var test = require('tape') +var spawn = require('./helpers/spawn.js') + +var dat = path.resolve(path.join(__dirname, '..', 'bin', 'cli.js')) +var version = require('../package.json').version + +test('misc - prints usage', function (t) { + var d = spawn(t, dat) + d.stderr.match(function (output) { + var usage = output.indexOf('Usage') > -1 + if (!usage) return false + return true + }) + d.end() +}) + +test('misc - prints version', function (t) { + var d = spawn(t, dat + ' -v') + d.stderr.match(function (output) { + var ver = output.indexOf(version) > -1 + if (!ver) return false + return true + }) + d.end() +}) + +test('misc - also prints version', function (t) { + var d = spawn(t, dat + ' -v') + d.stderr.match(function (output) { + var ver = output.indexOf(version) > -1 + if (!ver) return false + return true + }) + d.end() +})