Skip to content

Commit

Permalink
Add options (#20)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
joehand authored Dec 10, 2016
1 parent f6964df commit 3b505f6
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 7 deletions.
17 changes: 15 additions & 2 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
3 changes: 2 additions & 1 deletion lib/commands/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion lib/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
11 changes: 10 additions & 1 deletion lib/usage.js
Original file line number Diff line number Diff line change
@@ -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 <cmd> [options]')
console.error(' dat-next create create a local dat')
console.error(' dat-next snapshot create a local dat snapshot')
Expand All @@ -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=<folder> 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)
}
11 changes: 11 additions & 0 deletions tests/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
16 changes: 16 additions & 0 deletions tests/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
})
29 changes: 29 additions & 0 deletions tests/doctor.js
Original file line number Diff line number Diff line change
@@ -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()
})
}
})
Empty file removed tests/misc/usage.js
Empty file.
29 changes: 29 additions & 0 deletions tests/sync-owner.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var fs = require('fs')
var net = require('net')
var path = require('path')
var test = require('tape')
var mkdirp = require('mkdirp')
Expand Down Expand Up @@ -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'))
})
36 changes: 36 additions & 0 deletions tests/usage.js
Original file line number Diff line number Diff line change
@@ -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()
})

0 comments on commit 3b505f6

Please sign in to comment.