2014-12-12 21:20:29 +00:00
|
|
|
_ = require('lodash')
|
|
|
|
_.str = require('underscore.string')
|
2015-01-08 12:04:37 +00:00
|
|
|
resin = require('resin-sdk')
|
2014-12-12 21:20:29 +00:00
|
|
|
capitano = require('capitano')
|
|
|
|
|
|
|
|
# TODO: Refactor this terrible mess
|
|
|
|
|
|
|
|
PADDING_INITIAL = ' '
|
|
|
|
PADDING_MIDDLE = '\t'
|
|
|
|
|
|
|
|
getFieldMaxLength = (array, field) ->
|
|
|
|
return _.max _.map array, (item) ->
|
|
|
|
return item[field].toString().length
|
|
|
|
|
|
|
|
buildHelpString = (firstColumn, secondColumn) ->
|
|
|
|
result = "#{PADDING_INITIAL}#{firstColumn}"
|
|
|
|
result += "#{PADDING_MIDDLE}#{secondColumn}"
|
|
|
|
return result
|
|
|
|
|
|
|
|
addOptionPrefix = (option) ->
|
|
|
|
return if option.length <= 0
|
|
|
|
if option.length is 1
|
|
|
|
return "-#{option}"
|
|
|
|
else
|
|
|
|
return "--#{option}"
|
|
|
|
|
|
|
|
addAlias = (alias) ->
|
|
|
|
return ", #{addOptionPrefix(alias)}"
|
|
|
|
|
|
|
|
buildOptionSignatureHelp = (option) ->
|
|
|
|
result = addOptionPrefix(option.signature.toString())
|
|
|
|
|
|
|
|
if _.isString(option.alias)
|
|
|
|
result += addAlias(option.alias)
|
|
|
|
else if _.isArray(option.alias)
|
|
|
|
for alias in option.alias
|
|
|
|
result += addAlias(alias)
|
|
|
|
|
|
|
|
if option.parameter?
|
|
|
|
result += " <#{option.parameter}>"
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
getCommandHelp = (command) ->
|
|
|
|
maxSignatureLength = getFieldMaxLength(capitano.state.commands, 'signature')
|
|
|
|
commandSignature = _.str.rpad(command.signature.toString(), maxSignatureLength, ' ')
|
|
|
|
return buildHelpString(commandSignature, command.description)
|
|
|
|
|
|
|
|
getOptionsParsedSignatures = (optionsHelp) ->
|
|
|
|
maxLength = _.max _.map optionsHelp, (signature) ->
|
|
|
|
return signature.length
|
|
|
|
|
|
|
|
return _.map optionsHelp, (signature) ->
|
|
|
|
return _.str.rpad(signature, maxLength, ' ')
|
|
|
|
|
|
|
|
getOptionHelp = (option, maxLength) ->
|
|
|
|
result = PADDING_INITIAL
|
|
|
|
result += _.str.rpad(option.signature, maxLength, ' ')
|
|
|
|
result += PADDING_MIDDLE
|
|
|
|
result += option.description
|
|
|
|
return result
|
|
|
|
|
2015-03-03 14:14:16 +00:00
|
|
|
general = ->
|
2015-02-26 15:38:48 +00:00
|
|
|
console.log('Usage: resin [COMMAND] [OPTIONS]\n')
|
2015-01-15 14:36:43 +00:00
|
|
|
console.log('Commands:\n')
|
2014-12-12 21:20:29 +00:00
|
|
|
|
|
|
|
for command in capitano.state.commands
|
|
|
|
continue if command.isWildcard()
|
2015-01-15 14:36:43 +00:00
|
|
|
console.log(getCommandHelp(command))
|
2014-12-12 21:20:29 +00:00
|
|
|
|
2015-01-15 14:36:43 +00:00
|
|
|
console.log('\nGlobal Options:\n')
|
2014-12-12 21:20:29 +00:00
|
|
|
|
|
|
|
options = _.map capitano.state.globalOptions, (option) ->
|
|
|
|
option.signature = buildOptionSignatureHelp(option)
|
|
|
|
return option
|
|
|
|
|
|
|
|
optionSignatureMaxLength = _.max _.map options, (option) ->
|
|
|
|
return option.signature.length
|
|
|
|
|
|
|
|
for option in options
|
2015-01-15 14:36:43 +00:00
|
|
|
console.log(getOptionHelp(option, optionSignatureMaxLength))
|
2014-12-12 21:20:29 +00:00
|
|
|
|
2015-01-15 14:36:43 +00:00
|
|
|
console.log()
|
2014-12-12 21:20:29 +00:00
|
|
|
|
2015-03-03 14:14:16 +00:00
|
|
|
command = (params, options, done) ->
|
2015-02-10 18:44:39 +00:00
|
|
|
capitano.state.getMatchCommand params.command, (error, command) ->
|
|
|
|
return done(error) if error?
|
2014-12-12 21:20:29 +00:00
|
|
|
|
2015-02-10 18:44:39 +00:00
|
|
|
if not command? or command.isWildcard()
|
|
|
|
return capitano.defaults.actions.commandNotFound(params.command)
|
2014-12-12 21:20:29 +00:00
|
|
|
|
2015-02-10 18:44:39 +00:00
|
|
|
console.log("Usage: #{command.signature}")
|
2014-12-12 21:20:29 +00:00
|
|
|
|
2015-02-10 18:44:39 +00:00
|
|
|
if command.help?
|
|
|
|
console.log("\n#{command.help}")
|
|
|
|
else if command.description?
|
|
|
|
console.log("\n#{_.str.humanize(command.description)}")
|
2014-12-12 21:20:29 +00:00
|
|
|
|
2015-02-10 18:44:39 +00:00
|
|
|
if not _.isEmpty(command.options)
|
|
|
|
console.log('\nOptions:\n')
|
2014-12-12 21:20:29 +00:00
|
|
|
|
2015-02-10 18:44:39 +00:00
|
|
|
options = _.map command.options, (option) ->
|
|
|
|
option.signature = buildOptionSignatureHelp(option)
|
|
|
|
return option
|
2014-12-12 21:20:29 +00:00
|
|
|
|
2015-02-10 18:44:39 +00:00
|
|
|
optionSignatureMaxLength = _.max _.map options, (option) ->
|
|
|
|
return option.signature.toString().length
|
2014-12-12 21:20:29 +00:00
|
|
|
|
2015-02-10 18:44:39 +00:00
|
|
|
for option in options
|
|
|
|
console.log(getOptionHelp(option, optionSignatureMaxLength))
|
2014-12-12 21:20:29 +00:00
|
|
|
|
2015-02-10 18:44:39 +00:00
|
|
|
console.log()
|
|
|
|
|
|
|
|
return done()
|
2015-01-15 14:18:34 +00:00
|
|
|
|
2015-01-15 17:10:14 +00:00
|
|
|
exports.help =
|
|
|
|
signature: 'help [command...]'
|
|
|
|
description: 'show help'
|
2015-03-03 14:14:16 +00:00
|
|
|
help: '''
|
|
|
|
Get detailed help for an specific command.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
$ resin help apps
|
|
|
|
$ resin help os download
|
|
|
|
'''
|
2015-02-10 18:44:39 +00:00
|
|
|
action: (params, options, done) ->
|
2015-01-15 17:10:14 +00:00
|
|
|
if params.command?
|
2015-03-03 14:14:16 +00:00
|
|
|
command(params, options, done)
|
2015-01-15 17:10:14 +00:00
|
|
|
else
|
2015-03-03 14:14:16 +00:00
|
|
|
general(params, options, done)
|