Option based addOption/addCommand/addResource

This commit is contained in:
Juan Cruz Viotti 2014-11-27 09:28:24 -04:00
parent a5791d080f
commit 507ff16050
2 changed files with 99 additions and 36 deletions

View File

@ -6,36 +6,84 @@ actions = require('./actions')
resin.cli.setVersion(packageJSON.version)
# ---------- Options ----------
resin.cli.addOption('-y, --yes', 'confirm non interactively')
resin.cli.addOption('-v, --verbose', 'increase verbosity')
resin.cli.addOption('-q, --quiet', 'quiet (no output)')
resin.cli.addOption('-t, --type <type>', 'specify a type when creating an application')
resin.cli.addOption
option: '-y, --yes'
description: 'confirm non interactively'
resin.cli.addOption
option: '-v, --verbose'
description: 'increase verbosity'
resin.cli.addOption
option: '-q, --quiet'
description: 'quiet (no output)'
resin.cli.addOption
option: '-t, --type <type>'
description: 'specify a type when creating an application'
# TODO: I have to use 'application' instead of 'app' here
# as Commander gets confused with the app command
resin.cli.addOption('-a, --application <app>', 'application id', _.parseInt)
resin.cli.addOption
option: '-a, --application <app>'
description: 'application id'
coerce: _.parseInt
# ---------- Auth Module ----------
resin.cli.addCommand('login [username:password]', 'login to resin.io', actions.auth.login)
resin.cli.addCommand('logout', 'logout from resin.io', actions.auth.logout)
resin.cli.addCommand('signup', 'signup to resin.io', actions.auth.signup)
resin.cli.addCommand
command: 'login [username:password]'
description: 'login to resin.io'
action: actions.auth.login
resin.cli.addCommand
command: 'logout'
description: 'logout from resin.io'
action: actions.auth.logout
resin.cli.addCommand
command: 'signup'
description: 'signup to resin.io'
action: actions.auth.signup
# ---------- App Module ----------
resin.cli.addResource('app', 'application', actions.app)
resin.cli.addCommand('app:restart <id>', 'restart an application', actions.app.restart)
resin.cli.addResource
name: 'app'
displayName: 'application'
actions: actions.app
resin.cli.addCommand
command: 'app:restart <id>'
description: 'restart an application'
action: actions.app.restart
# ---------- Device Module ----------
resin.cli.addResource('device', 'device', actions.device)
resin.cli.addCommand('device:identify <uuid>', 'identify a device with a UUID', actions.device.identify)
resin.cli.addResource
name: 'device'
displayName: 'device'
actions: actions.device
resin.cli.addCommand
command: 'device:identify <uuid>'
description: 'identify a device with a UUID'
action: actions.device.identify
# ---------- Preferences Module ----------
resin.cli.addCommand('preferences', 'open preferences form', actions.preferences.preferences)
resin.cli.addCommand
command: 'preferences'
description: 'open preferences form'
action: actions.preferences.preferences
# ---------- Keys Module ----------
resin.cli.addResource('key', 'ssh key', actions.keys)
resin.cli.addResource
name: 'key'
displayName: 'ssh key'
actions: actions.keys
# ---------- Env Module ----------
resin.cli.addResource('env', 'environment variable', actions.env)
resin.cli.addResource
name: 'env'
displayName: 'environment variable'
actions: actions.env
resin.data.prefix.set resin.config.dataPrefix, (error) ->
resin.errors.handle(error) if error?

View File

@ -11,38 +11,53 @@ exports.setVersion = (version) ->
program.version(version)
# Set version command automatically
exports.addCommand 'version', 'show version', ->
log.out(version)
exports.addCommand
command: 'version'
description: 'show version'
action: ->
log.out(version)
exports.addCommand = (command, description, action) ->
exports.addCommand = (options = {}) ->
program
.command(command)
.description(description)
.action(action)
.command(options.command)
.description(options.description)
.action(options.action)
return program
exports.addOption = (option, description, coerceFunction) ->
program.option(option, description, coerceFunction)
exports.addOption = (options = {}) ->
program.option(options.option, options.description, options.coerce)
exports.addResource = (name, displayName, actions = {}) ->
displayName ?= name
nameArticle = indefiniteArticle(displayName)
exports.addResource = (options = {}) ->
options.displayName ?= options.name
nameArticle = indefiniteArticle(options.displayName)
pluralizedName = pluralize(name)
pluralizedDisplayName = pluralize(displayName)
pluralizedName = pluralize(options.name)
pluralizedDisplayName = pluralize(options.displayName)
if _.isFunction(actions.create)
exports.addCommand("#{name}:create <name>", "create #{nameArticle} #{displayName}", actions.create)
if _.isFunction(options.actions.create)
exports.addCommand
command: "#{options.name}:create <name>"
description: "create #{nameArticle} #{options.displayName}"
action: options.actions.create
if _.isFunction(actions.list)
exports.addCommand("#{pluralizedName}", "list all #{pluralizedDisplayName}", actions.list)
if _.isFunction(options.actions.list)
exports.addCommand
command: "#{pluralizedName}"
description: "list all #{pluralizedDisplayName}"
action: options.actions.list
if _.isFunction(actions.info)
exports.addCommand("#{name} <id>", "list a single #{displayName}", actions.info)
if _.isFunction(options.actions.info)
exports.addCommand
command: "#{options.name} <id>"
description: "list a single #{options.displayName}"
action: options.actions.info
if _.isFunction(actions.remove)
exports.addCommand("#{name}:rm <id>", "remove #{nameArticle} #{displayName}", actions.remove)
if _.isFunction(options.actions.remove)
exports.addCommand
command: "#{options.name}:rm <id>"
description: "remove #{nameArticle} #{options.displayName}"
action: options.actions.remove
exports.parse = (argv) ->
program.parse(argv)