2016-01-04 03:58:51 +00:00
|
|
|
###
|
2017-06-14 21:20:15 +00:00
|
|
|
Copyright 2016-2017 Resin.io
|
2016-01-04 03:58:51 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
###
|
|
|
|
|
2014-12-12 21:20:29 +00:00
|
|
|
_ = require('lodash')
|
|
|
|
capitano = require('capitano')
|
2015-08-13 12:11:58 +00:00
|
|
|
columnify = require('columnify')
|
2016-01-12 14:39:29 +00:00
|
|
|
messages = require('../utils/messages')
|
2018-04-17 13:17:48 +00:00
|
|
|
{ exitWithExpectedError } = require('../utils/patterns')
|
2014-12-12 21:20:29 +00:00
|
|
|
|
2015-08-13 12:11:58 +00:00
|
|
|
parse = (object) ->
|
2017-08-18 17:41:29 +00:00
|
|
|
return _.fromPairs _.map object, (item) ->
|
2014-12-12 21:20:29 +00:00
|
|
|
|
2015-08-13 12:11:58 +00:00
|
|
|
# Hacky way to determine if an object is
|
|
|
|
# a function or a command
|
|
|
|
if item.alias?
|
|
|
|
signature = item.toString()
|
|
|
|
else
|
|
|
|
signature = item.signature.toString()
|
2014-12-12 21:20:29 +00:00
|
|
|
|
2015-08-13 12:11:58 +00:00
|
|
|
return [
|
|
|
|
signature
|
|
|
|
item.description
|
|
|
|
]
|
2014-12-12 21:20:29 +00:00
|
|
|
|
2015-08-13 12:11:58 +00:00
|
|
|
indent = (text) ->
|
2017-11-23 13:02:47 +00:00
|
|
|
text = _.map text.split('\n'), (line) ->
|
2015-08-13 12:11:58 +00:00
|
|
|
return ' ' + line
|
|
|
|
return text.join('\n')
|
2014-12-12 21:20:29 +00:00
|
|
|
|
2015-08-13 12:11:58 +00:00
|
|
|
print = (data) ->
|
|
|
|
console.log indent columnify data,
|
|
|
|
showHeaders: false
|
|
|
|
minWidth: 35
|
2014-12-12 21:20:29 +00:00
|
|
|
|
2015-08-13 12:11:58 +00:00
|
|
|
general = (params, options, done) ->
|
2015-02-26 15:38:48 +00:00
|
|
|
console.log('Usage: resin [COMMAND] [OPTIONS]\n')
|
2016-01-12 14:39:29 +00:00
|
|
|
console.log(messages.reachingOut)
|
|
|
|
console.log('\nPrimary commands:\n')
|
2014-12-12 21:20:29 +00:00
|
|
|
|
2015-08-13 12:11:58 +00:00
|
|
|
# We do not want the wildcard command
|
|
|
|
# to be printed in the help screen.
|
|
|
|
commands = _.reject capitano.state.commands, (command) ->
|
2017-03-24 09:48:14 +00:00
|
|
|
return command.hidden or command.isWildcard()
|
2015-08-13 12:11:58 +00:00
|
|
|
|
2015-10-01 14:39:36 +00:00
|
|
|
groupedCommands = _.groupBy commands, (command) ->
|
2017-03-24 09:48:14 +00:00
|
|
|
if command.primary
|
2015-10-01 14:39:36 +00:00
|
|
|
return 'primary'
|
|
|
|
return 'secondary'
|
|
|
|
|
|
|
|
print(parse(groupedCommands.primary))
|
|
|
|
|
|
|
|
if options.verbose
|
|
|
|
console.log('\nAdditional commands:\n')
|
|
|
|
print(parse(groupedCommands.secondary))
|
|
|
|
else
|
|
|
|
console.log('\nRun `resin help --verbose` to list additional commands')
|
2014-12-12 21:20:29 +00:00
|
|
|
|
2015-08-12 12:17:46 +00:00
|
|
|
if not _.isEmpty(capitano.state.globalOptions)
|
|
|
|
console.log('\nGlobal Options:\n')
|
2015-08-13 12:11:58 +00:00
|
|
|
print(parse(capitano.state.globalOptions))
|
2014-12-12 21:20:29 +00:00
|
|
|
|
2015-08-13 12:11:58 +00:00
|
|
|
return done()
|
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()
|
2018-04-17 13:17:48 +00:00
|
|
|
exitWithExpectedError("Command not found: #{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?
|
2017-11-23 13:02:47 +00:00
|
|
|
console.log("\n#{_.capitalize(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')
|
2015-08-13 12:11:58 +00:00
|
|
|
print(parse(command.options))
|
2015-02-10 18:44:39 +00:00
|
|
|
|
|
|
|
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-10-01 14:39:36 +00:00
|
|
|
primary: true
|
|
|
|
options: [
|
|
|
|
signature: 'verbose'
|
|
|
|
description: 'show additional commands'
|
|
|
|
boolean: true
|
|
|
|
alias: 'v'
|
|
|
|
]
|
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)
|