2014-11-27 13:10:51 +00:00
|
|
|
_ = require('lodash')
|
2014-11-26 19:05:00 +00:00
|
|
|
program = require('commander')
|
2014-11-27 13:10:51 +00:00
|
|
|
pluralize = require('pluralize')
|
|
|
|
indefiniteArticle = require('indefinite-article')
|
2014-12-05 15:51:52 +00:00
|
|
|
resin = require('../resin')
|
2014-11-27 14:06:11 +00:00
|
|
|
cliPermissions = require('./cli-permissions')
|
2014-11-26 19:05:00 +00:00
|
|
|
|
2014-11-29 00:18:59 +00:00
|
|
|
exports.getArgument = (name, coerceFunction) ->
|
|
|
|
argument = program[name]
|
|
|
|
return if not argument?
|
|
|
|
|
|
|
|
if _.isFunction(coerceFunction)
|
|
|
|
argument = coerceFunction(argument)
|
|
|
|
|
|
|
|
return argument
|
2014-11-26 19:05:00 +00:00
|
|
|
|
|
|
|
exports.setVersion = (version) ->
|
|
|
|
program.version(version)
|
|
|
|
|
|
|
|
# Set version command automatically
|
2014-11-27 13:28:24 +00:00
|
|
|
exports.addCommand
|
|
|
|
command: 'version'
|
|
|
|
description: 'show version'
|
|
|
|
action: ->
|
2014-12-05 15:51:52 +00:00
|
|
|
resin.log.out(version)
|
2014-11-26 19:05:00 +00:00
|
|
|
|
2014-11-27 14:06:11 +00:00
|
|
|
applyPermissions = (permission, action, onError) ->
|
|
|
|
permissionFunction = cliPermissions[permission]
|
|
|
|
if not _.isFunction(permissionFunction)
|
|
|
|
throw new Error("Invalid permission #{permission}")
|
|
|
|
return permissionFunction(action, onError)
|
|
|
|
|
2014-11-27 13:28:24 +00:00
|
|
|
exports.addCommand = (options = {}) ->
|
2014-11-27 14:06:11 +00:00
|
|
|
|
2014-11-27 14:07:59 +00:00
|
|
|
_.defaults options,
|
2014-12-05 15:51:52 +00:00
|
|
|
onError: resin.errors.handle
|
2014-11-27 14:07:59 +00:00
|
|
|
|
2014-11-27 14:06:11 +00:00
|
|
|
if options.permission?
|
|
|
|
action = applyPermissions(options.permission, options.action, options.onError)
|
|
|
|
else
|
|
|
|
action = options.action
|
|
|
|
|
2014-11-26 19:05:00 +00:00
|
|
|
program
|
2014-11-27 13:28:24 +00:00
|
|
|
.command(options.command)
|
|
|
|
.description(options.description)
|
2014-11-27 14:06:11 +00:00
|
|
|
.action(action)
|
2014-11-26 19:05:00 +00:00
|
|
|
|
|
|
|
return program
|
|
|
|
|
2014-11-27 13:28:24 +00:00
|
|
|
exports.addOption = (options = {}) ->
|
|
|
|
program.option(options.option, options.description, options.coerce)
|
|
|
|
|
|
|
|
exports.addResource = (options = {}) ->
|
|
|
|
options.displayName ?= options.name
|
|
|
|
nameArticle = indefiniteArticle(options.displayName)
|
|
|
|
|
|
|
|
pluralizedName = pluralize(options.name)
|
|
|
|
pluralizedDisplayName = pluralize(options.displayName)
|
|
|
|
|
|
|
|
if _.isFunction(options.actions.create)
|
|
|
|
exports.addCommand
|
|
|
|
command: "#{options.name}:create <name>"
|
|
|
|
description: "create #{nameArticle} #{options.displayName}"
|
|
|
|
action: options.actions.create
|
2014-11-27 14:06:11 +00:00
|
|
|
permission: options.permission
|
2014-11-27 13:28:24 +00:00
|
|
|
|
|
|
|
if _.isFunction(options.actions.list)
|
|
|
|
exports.addCommand
|
|
|
|
command: "#{pluralizedName}"
|
|
|
|
description: "list all #{pluralizedDisplayName}"
|
|
|
|
action: options.actions.list
|
2014-11-27 14:06:11 +00:00
|
|
|
permission: options.permission
|
2014-11-27 13:28:24 +00:00
|
|
|
|
|
|
|
if _.isFunction(options.actions.info)
|
|
|
|
exports.addCommand
|
|
|
|
command: "#{options.name} <id>"
|
|
|
|
description: "list a single #{options.displayName}"
|
|
|
|
action: options.actions.info
|
2014-11-27 14:06:11 +00:00
|
|
|
permission: options.permission
|
2014-11-27 13:28:24 +00:00
|
|
|
|
|
|
|
if _.isFunction(options.actions.remove)
|
|
|
|
exports.addCommand
|
|
|
|
command: "#{options.name}:rm <id>"
|
|
|
|
description: "remove #{nameArticle} #{options.displayName}"
|
|
|
|
action: options.actions.remove
|
2014-11-27 14:06:11 +00:00
|
|
|
permission: options.permission
|
2014-11-27 13:10:51 +00:00
|
|
|
|
2014-11-26 19:05:00 +00:00
|
|
|
exports.parse = (argv) ->
|
2014-12-10 14:29:23 +00:00
|
|
|
|
2014-12-10 17:44:56 +00:00
|
|
|
# Matches unknown commands
|
2014-12-10 14:29:23 +00:00
|
|
|
exports.addCommand
|
|
|
|
command: '*'
|
|
|
|
action: ->
|
|
|
|
program.outputHelp()
|
|
|
|
|
2014-11-26 19:05:00 +00:00
|
|
|
program.parse(argv)
|
2014-12-10 17:44:56 +00:00
|
|
|
|
|
|
|
# Show help if no command is passed
|
|
|
|
if _.isEmpty(program.args)
|
|
|
|
program.outputHelp()
|