Revert back to commander

This commit is contained in:
Juan Cruz Viotti 2014-11-21 09:23:02 -04:00
parent 4b20ba435d
commit b53bc060a3
5 changed files with 60 additions and 184 deletions

View File

@ -15,6 +15,7 @@ exports.list = authHooks.failIfNotLoggedIn ->
, [ 'ID', 'Title' ]
exports.info = authHooks.failIfNotLoggedIn (id) ->
id = _.parseInt(id)
# TODO: We don't have a way to query a single ssh key yet.
# As a workaround, we request all of them, and filter

View File

@ -1,42 +1,83 @@
data = require('./data/data')
config = require('./config')
packageJSON = require('../package.json')
yargs = require('yargs')
yargs.command = require('./yargs-command/yargs-command')
yargs.usage('$0 [options] <command>')
program = require('commander')
program.version(packageJSON.version)
# ---------- Auth Module ----------
auth = require('./actions/auth')
# TODO: Re enable optional interactivity
yargs.command('login [username:password]', auth.login)
yargs.command('logout', auth.logout)
yargs.command('signup', auth.signup)
program
.command('login [username:password]')
.description('Login to resin.io')
.action(auth.login)
program
.command('logout')
.description('Logout from resin.io')
.action(auth.logout)
program
.command('signup')
.description('Signup to resin.io')
.action(auth.signup)
# ---------- App Module ----------
app = require('./actions/app')
yargs.command('apps', app.list)
yargs.command('app <id>', app.info)
yargs.command('app restart <id>', app.restart)
program
.command('apps')
.description('List your applications')
.action(app.list)
program
.command('app <id>')
.description('List a single application')
.action(app.info)
program
.command('app:restart <id>')
.description('Restart an application')
.action(app.restart)
# ---------- Device Module ----------
device = require('./actions/device')
yargs.command('devices <id>', device.list)
program
.command('devices <id>')
.description('Show devices for an application')
.action(device.list)
# ---------- Preferences Module ----------
preferences = require('./actions/preferences')
yargs.command('preferences', preferences.preferences)
program
.command('preferences')
.description('Open preferences form')
.action(preferences.preferences)
# ---------- Version Module ----------
version = require('./actions/version')
yargs.command('version', version.version)
program
.command('version')
.description('Show version')
.action(version.version)
# ---------- Keys Module ----------
keys = require('./actions/keys')
yargs.command('keys', keys.list)
yargs.command('key <id>', keys.info)
program
.command('keys')
.description('List all SSH keys')
.action(keys.list)
program
.command('key <id>')
.description('List a single SSH key')
.action(keys.info)
data.prefix.set config.dataPrefix, (error) ->
throw error if error?
yargs.command.run()
program.parse(process.argv)

View File

@ -1,50 +0,0 @@
yargs = require('yargs')
_ = require('lodash')
commandIndexBySignature = (command) ->
query = { signature: command.signature }
return _.findIndex(@command._commands, query)
isArgVariable = (word) ->
return /^(<.*>|\[.*\])$/.test(word)
splitSignature = (signature) ->
return signature.split(' ')
commandApplies = (command, args) ->
args._ ?= {}
splittedCommandSignature = splitSignature(command.signature)
if splittedCommandSignature.length isnt args._.length
return false
for word in splittedCommandSignature
index = splittedCommandSignature.indexOf(word)
if not isArgVariable(word) and word isnt args._[index]
return false
return true
run = ->
if not @command._matchedCommand?
return @showHelp()
signature = splitSignature(@command._matchedCommand.signature)
parameters = _.difference(@argv._, signature)
@command._matchedCommand.action.apply(this, parameters)
module.exports = (signature, action) ->
command = { signature, action }
@command._commands ?= []
@command.run ?= _.bind(run, this)
commandIndex = commandIndexBySignature.call(this, command)
if commandIndex is -1
@command._commands.push(command)
else
@command._commands.splice(commandIndex, 1, command)
if commandApplies(command, @argv)
@command._matchedCommand = command
return this

View File

@ -1,115 +0,0 @@
chai = require('chai')
sinon = require('sinon')
chai.use(require('chai-things'))
chai.use(require('sinon-chai'))
expect = chai.expect
_ = require('lodash')
yargs = require('yargs')
yargs.command = require('./yargs-command')
COMMANDS =
appList:
signature: 'app list <id>'
action: (id) -> "App List: #{id}"
action2: (id) -> "App List: #{id}!"
deviceList:
signature: 'device list <id>'
action: (id) -> "Device: #{id}"
ARGS =
appList:
_: [ 'app', 'list', '7' ]
deviceList:
_: [ 'device', 'list', '7' ]
noMatch:
_: [ 'foo', 'bar', 'baz' ]
# Hacky way to check that both functions are equal
# as mocha crashes when trying equal or deep equal
# directly on the functions for some reason
areFunctionsEqual = (fn1, fn2) ->
return fn1.toString() is fn2.toString()
describe 'Yargs Command:', ->
cleanUpYargs = ->
yargs.command._commands = [] if yargs.command._commands
yargs.argv = {}
delete yargs.command._matchedCommand
beforeEach ->
cleanUpYargs()
afterEach ->
cleanUpYargs()
it 'should expose a public command function', ->
expect(yargs.command).to.be.an.instanceof(Function)
it 'should return yargs to enable chaining', ->
result = yargs.command(COMMANDS.appList.signature, COMMANDS.appList.action)
expect(result).to.equal(yargs)
it 'should contain an empty _commands array', ->
expect(yargs.command._commands).to.deep.equal([])
it 'should push a command to _commands', ->
yargs.command(COMMANDS.appList.signature, COMMANDS.appList.action)
expect(yargs.command._commands).to.contain.something.that.deep.equals
signature: COMMANDS.appList.signature
action: COMMANDS.appList.action
describe 'if adding the same command signature twice', ->
beforeEach ->
yargs.command(COMMANDS.appList.signature, COMMANDS.appList.action)
yargs.command(COMMANDS.appList.signature, COMMANDS.appList.action2)
it 'should contain only one command', ->
expect(yargs.command._commands).to.have.length(1)
it 'should make the last action override the first one', ->
firstCommandAction = yargs.command._commands[0].action
expect(areFunctionsEqual(firstCommandAction, COMMANDS.appList.action2)).to.be.true
describe 'given various registered commands', ->
registerCommands = ->
yargs.command(COMMANDS.appList.signature, COMMANDS.appList.action)
yargs.command(COMMANDS.deviceList.signature, COMMANDS.deviceList.action)
testCommand = (name) ->
yargs.argv = ARGS[name]
registerCommands()
matchedCommand = yargs.command._matchedCommand
expect(matchedCommand.signature).to.equal(COMMANDS[name].signature)
it 'should choose the right command', ->
for command in [
'appList'
'deviceList'
]
testCommand(command)
it 'should be undefined if no match', ->
yargs.argv = ARGS.noMatch
registerCommands()
expect(yargs.command._matchedCommand).to.not.exist
it 'should have a run public function', ->
expect(yargs.command.run).to.be.an.instanceof(Function)
describe '#run()', ->
it 'should call the action with the correct parameters', ->
yargs.argv = ARGS.appList
callback = sinon.spy()
yargs.command(COMMANDS.appList.signature, callback)
yargs.command.run()
expect(callback).to.have.been.calledWith(_.last(ARGS.appList._))
it 'should print help if no matches', ->
helpSpy = sinon.spy(yargs, 'showHelp')
yargs.command.run()
expect(helpSpy).to.have.been.called

View File

@ -40,7 +40,6 @@
"open": "0.0.5",
"inquirer": "~0.8.0",
"cliff": "~0.1.9",
"underscore.string": "~2.4.0",
"yargs": "~1.3.3"
"underscore.string": "~2.4.0"
}
}