diff --git a/lib/app.coffee b/lib/app.coffee index fd821051..5de772c6 100644 --- a/lib/app.coffee +++ b/lib/app.coffee @@ -21,28 +21,21 @@ resin.cli.addCommand('logout', 'logout from resin.io', actions.auth.logout) resin.cli.addCommand('signup', 'signup to resin.io', actions.auth.signup) # ---------- App Module ---------- -resin.cli.addCommand('app:create ', 'create a resin.io application', actions.app.create) -resin.cli.addCommand('apps', 'list your applications', actions.app.list) -resin.cli.addCommand('app ', 'list a single application', actions.app.info) +resin.cli.addResource('app', 'application', actions.app) resin.cli.addCommand('app:restart ', 'restart an application', actions.app.restart) -resin.cli.addCommand('app:rm ', 'remove an application', actions.app.remove) # ---------- Device Module ---------- -resin.cli.addCommand('devices ', 'show devices for an application', actions.device.list) -resin.cli.addCommand('device:rm ', 'remove a device', actions.device.remove) +resin.cli.addResource('device', 'device', actions.device) resin.cli.addCommand('device:identify ', 'identify a device with a UUID', actions.device.identify) # ---------- Preferences Module ---------- resin.cli.addCommand('preferences', 'open preferences form', actions.preferences.preferences) # ---------- Keys Module ---------- -resin.cli.addCommand('keys', 'list all SSH keys', actions.keys.list) -resin.cli.addCommand('key ', 'list a single SSH key', actions.keys.info) -resin.cli.addCommand('key:rm ', 'remove a SSH key', actions.keys.remove) +resin.cli.addResource('key', 'ssh key', actions.keys) # ---------- Env Module ---------- -resin.cli.addCommand('envs', 'list all environment variables', actions.env.list) -resin.cli.addCommand('env:rm ', 'remove environment variable', actions.env.remove) +resin.cli.addResource('env', 'environment variable', actions.env) resin.data.prefix.set resin.config.dataPrefix, (error) -> resin.errors.handle(error) if error? diff --git a/lib/resin/cli/cli.coffee b/lib/resin/cli/cli.coffee index dd0e2693..7b5c60c2 100644 --- a/lib/resin/cli/cli.coffee +++ b/lib/resin/cli/cli.coffee @@ -1,4 +1,7 @@ +_ = require('lodash') program = require('commander') +pluralize = require('pluralize') +indefiniteArticle = require('indefinite-article') log = require('../log/log') exports.getArgument = (name) -> @@ -22,5 +25,24 @@ exports.addCommand = (command, description, action) -> exports.addOption = (option, description, coerceFunction) -> program.option(option, description, coerceFunction) +exports.addResource = (name, displayName, actions = {}) -> + displayName ?= name + nameArticle = indefiniteArticle(displayName) + + pluralizedName = pluralize(name) + pluralizedDisplayName = pluralize(displayName) + + if _.isFunction(actions.create) + exports.addCommand("#{name}:create ", "create #{nameArticle} #{displayName}", actions.create) + + if _.isFunction(actions.list) + exports.addCommand("#{pluralizedName}", "list all #{pluralizedDisplayName}", actions.list) + + if _.isFunction(actions.info) + exports.addCommand("#{name} ", "list a single #{displayName}", actions.info) + + if _.isFunction(actions.remove) + exports.addCommand("#{name}:rm ", "remove #{nameArticle} #{displayName}", actions.remove) + exports.parse = (argv) -> program.parse(argv) diff --git a/package.json b/package.json index f79856c2..4851d103 100644 --- a/package.json +++ b/package.json @@ -3,9 +3,9 @@ "version": "0.0.1", "description": "Git Push to your devices", "main": "./lib/app.js", - "bin": { - "resin": "./bin/resin" - }, + "bin": { + "resin": "./bin/resin" + }, "scripts": { "test": "cult test" }, @@ -48,6 +48,8 @@ "cliff": "~0.1.9", "underscore.string": "~2.4.0", "typed-error": "~0.1.0", - "is-online": "~3.0.0" + "is-online": "~3.0.0", + "pluralize": "~1.1.0", + "indefinite-article": "0.0.2" } }