balena-cli/lib/cli/cli.coffee

102 lines
2.6 KiB
CoffeeScript
Raw Normal View History

2014-11-27 13:10:51 +00:00
_ = require('lodash')
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-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
exports.setVersion = (version) ->
program.version(version)
# Set version command automatically
exports.addCommand
command: 'version'
description: 'show version'
action: ->
2014-12-05 15:51:52 +00:00
resin.log.out(version)
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)
exports.addCommand = (options = {}) ->
2014-11-27 14:06:11 +00:00
_.defaults options,
2014-12-05 15:51:52 +00:00
onError: resin.errors.handle
2014-11-27 14:06:11 +00:00
if options.permission?
action = applyPermissions(options.permission, options.action, options.onError)
else
action = options.action
program
.command(options.command)
.description(options.description)
2014-11-27 14:06:11 +00:00
.action(action)
return program
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
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
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
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
exports.parse = (argv) ->
2014-12-10 17:44:56 +00:00
# Matches unknown commands
exports.addCommand
command: '*'
2014-12-10 18:34:22 +00:00
description: ''
action: ->
program.outputHelp()
program.parse(argv)
2014-12-10 17:44:56 +00:00
# Show help if no command is passed
if _.isEmpty(program.args)
program.outputHelp()