Comparing version 1.2.0 to 1.3.0
@@ -37,7 +37,20 @@ var Command, Option, Signature, _; | ||
exports.permission = function(name, permissionFunction) { | ||
if (name == null) { | ||
throw new Error('Missing permission name'); | ||
} | ||
if (!_.isString(name)) { | ||
throw new Error('Invalid permission name'); | ||
} | ||
if (permissionFunction == null) { | ||
throw new Error('Missing permission function'); | ||
} | ||
if (!_.isFunction(permissionFunction)) { | ||
throw new Error('Invalid permission function'); | ||
} | ||
return exports.state.permissions[name] = permissionFunction; | ||
}; | ||
exports.execute = function(args, callback) { | ||
var command, error; | ||
if (callback == null) { | ||
callback = _.noop; | ||
} | ||
command = exports.state.getMatchCommand(args.command); | ||
@@ -51,3 +64,3 @@ if (command == null) { | ||
error = _error; | ||
return callback(error); | ||
return typeof callback === "function" ? callback(error) : void 0; | ||
} | ||
@@ -54,0 +67,0 @@ }; |
@@ -36,4 +36,23 @@ var Command, Option, Signature, parse, settings, state, _; | ||
Command.prototype.applyPermissions = function(callback) { | ||
var error, permissionFunction; | ||
if (this.permission == null) { | ||
return callback(); | ||
} | ||
permissionFunction = state.permissions[this.permission]; | ||
if (permissionFunction == null) { | ||
error = new Error("Permission not found: " + this.permission); | ||
return callback(error); | ||
} | ||
return permissionFunction.call(this, callback); | ||
}; | ||
Command.prototype._parseOptions = function(options) { | ||
var allOptions, parsedOptions; | ||
allOptions = _.union(state.globalOptions, this.options); | ||
return parsedOptions = parse.parseOptions(allOptions, options); | ||
}; | ||
Command.prototype.execute = function(args, callback) { | ||
var allOptions, params, parsedOptions; | ||
var params, parsedOptions; | ||
if (args == null) { | ||
@@ -43,8 +62,14 @@ args = {}; | ||
params = this.signature.compileParameters(args.command); | ||
allOptions = _.union(state.globalOptions, this.options); | ||
parsedOptions = parse.parseOptions(allOptions, args.options); | ||
this.action.call(this, params, parsedOptions, callback); | ||
if (this.action.length < 3) { | ||
return typeof callback === "function" ? callback() : void 0; | ||
} | ||
parsedOptions = this._parseOptions(args.options); | ||
return this.applyPermissions((function(_this) { | ||
return function(error) { | ||
if (error != null) { | ||
return typeof callback === "function" ? callback(error) : void 0; | ||
} | ||
_this.action(params, parsedOptions, callback); | ||
if (_this.action.length < 3) { | ||
return typeof callback === "function" ? callback() : void 0; | ||
} | ||
}; | ||
})(this)); | ||
}; | ||
@@ -51,0 +76,0 @@ |
@@ -11,2 +11,4 @@ var settings, _; | ||
exports.permissions = {}; | ||
exports.findCommandBySignature = function(signature) { | ||
@@ -13,0 +15,0 @@ return _.findWhere(exports.commands, function(command) { |
{ | ||
"name": "capitano", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Powerful, non opitionated command line parser for serious applications", | ||
@@ -5,0 +5,0 @@ "main": "build/capitano.js", |
@@ -12,2 +12,6 @@ Capitano | ||
capitano.permission 'jviotti', (done) -> | ||
return done() if process.env.USER is 'jviotti' | ||
done(new Error('You are not jviotti!')) | ||
capitano.command | ||
@@ -20,2 +24,3 @@ signature: 'utils print <title> [words...]' | ||
] | ||
permission: 'jviotti' | ||
action: (params, options) -> | ||
@@ -55,2 +60,3 @@ log = '' | ||
- No built-in commands, you have a high degree of control over your app. | ||
- Permission support. | ||
@@ -87,2 +93,8 @@ Installation | ||
### permission (string) | ||
Require a certain previously registered permission by name. If the permission requirements are not met, the command action will not be called and `capitano.execute()`, or `capitano.run()` will get the error you passed in from the permission function in their callbacks. | ||
Notice that Capitano doesn't currently supports passing an array of permissions. If you have that specific use case, you'll have to create a new permission that combines the other ones. | ||
## capitano.globalOption(options) | ||
@@ -106,3 +118,2 @@ | ||
### alias (string|[string]) | ||
@@ -112,2 +123,8 @@ | ||
## capitano.permission(name, function) | ||
It registers a permission function under a certain name. The permission function is passed a `done()` function that accepts an error instance in case the user doesn't fits the permission requirements. Pass nothing if the permission requirement was matched. | ||
**Note:** You must call the `done()` function, even if your permission function is synchronous, in order for Capitano to continue. | ||
## capitano.run(argv, callback) | ||
@@ -342,2 +359,6 @@ | ||
### 1.3.0 | ||
- Implement permission support. Discussed in [#15](https://github.com/resin-io/capitano/issues/15). | ||
### 1.2.0 | ||
@@ -344,0 +365,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
108553
640
412