balena-cli/man/resin-plugins.1
2015-03-02 10:40:43 -04:00

197 lines
6.2 KiB
Groff

.TH "RESIN\-PLUGINS" "1" "March 2015" "" ""
.SH "NAME"
\fBresin-plugins\fR \- Creating Resin CLI plugins
.SH DESCRIPTION
.P
Resin CLI plugins are managed by NPM\. Installing an NPM module that starts with \fBresin\-plugin\-*\fR globally will automatically make it available to the Resin CLI\.
.SH TUTORIAL
.P
In this guide, we'll create a simple hello plugin that greets the user\.
.P
Create a directory called \fBresin\-plugin\-hello\fR, containing a single \fBindex\.js\fR file\.
.P
Within the new project, run \fBnpm init\fR and make sure the package name is set to \fBresin\-plugin\-hello\fR as well\.
.P
Also make sure that you have a \fBmain\fR field in \fBpackage\.json\fR that points to the \fBindex\.js\fR file you created above\.
.P
Your \fBpackage\.json\fR should look something like this:
.P
.RS 2
.nf
{
"name": "resin\-plugin\-hello",
"version": "1\.0\.0",
"main": "index\.js",
"description": "My first Resin plugin",
"license": "MIT"
}
.fi
.RE
.P
Your index file should export an object (if exposing a single command) or an array of objects (if exposing multiple commands)\.
.P
Notice that is very important that your \fBpackage\.json\fR \fBmain\fR field points to the file that is exporting the commands for the plugin to work correctly\.
.P
Each object describes a single command\. The accepted fields are:
.RS 0
.IP \(bu 2
\fBsignature\fR: A Capitano \fIhttps://github\.com/resin\-io/capitano\fR signature\.
.IP \(bu 2
\fBdescription\fR: A string containing a short description of the command\. This will be shown on the Resin general help\.
.IP \(bu 2
\fBhelp\fR: A string containing an usage help page\. This will be shown when passing the signature to the \fBhelp\fR command\.
.IP \(bu 2
\fBaction\fR: A function that defines the action to take when the command is matched\. The function will be given 3 arguments (\fBparams\fR, \fBoptions\fR, \fBdone\fR)\.
.IP \(bu 2
\fBpermission\fR: A string describing the required permissions to run the command\.
.IP \(bu 2
\fBoptions\fR: An array of Capitano \fIhttps://github\.com/resin\-io/capitano\fR options\.
.RE
.P
The \fBindex\.js\fR file should look something like:
.P
.RS 2
.nf
module\.exports = [
{
signature: 'hello <name>',
description: 'example plugin',
help: 'This is an example plugin\.',
action: function(params, options, done) {
console\.log('Hey there ' + params\.name + '!');
done();
}
}
]
.fi
.RE
.P
As we're only exporting a single command, we can export the object directly:
.P
.RS 2
.nf
module\.exports = {
signature: 'hello <name>',
description: 'example plugin',
help: 'This is an example plugin',
action: function(params, options, done) {
console\.log('Hey there ' + params\.name + '!');
done();
}
}
.fi
.RE
.P
This example will register a \fBhello\fR command which requires a \fBname\fR parameter, and greets the user in result\.
.P
To test the plugin, first create a global link by running the following command inside your plugin directory:
.P
.RS 2
.nf
$ npm link
.fi
.RE
.P
Now if you run \fB$ resin help\fR you should see your new command at the bottom of the list\.
.P
Try it out:
.P
.RS 2
.nf
$ resin hello Juan
Hey there Juan!
.fi
.RE
.SH DONE CALLBACK
.P
It's very important that you call the \fBdone()\fR callback after your action finishes\. If you pass an \fBError\fR instance to \fBdone()\fR, its message will be displayed by the Resin CLI, exiting with an error code 1\.
.P
If your action is synchronous and doesn't return any error, you can omit the \fBdone()\fR callback all together\. For example:
.P
.RS 2
.nf
module\.exports = {
signature: 'hello <name>',
description: 'example plugin',
help: 'This is an example plugin',
action: function(params, options) {
console\.log('Hey there ' + params\.name + '!');
}
}
.fi
.RE
.SH PERMISSIONS
.P
You can set a command permission to restrict access to the commands\. Currently, the only registered permission is \fBuser\fR, which requires the user to log in to Resin from the CLI\.
.P
To require the user to login before calling our hello plugin, we can add \fBpermission: 'user'\fR to the command description:
.P
.RS 2
.nf
module\.exports = {
signature: 'hello <name>',
description: 'example plugin',
help: 'This is an example plugin',
permission: 'user',
action: function(params, options, done) {
console\.log('Hey there ' + params\.name + '!');
done();
}
}
.fi
.RE
.P
Now if the user attempts to call our command without being logged in, a nice error message asking him to login will be shown instead\.
.SH OPTIONS
.P
You can define certain options that your command accepts\. Notice these are per command, and thus are not available to other command that doesn't declares them as well\.
.P
Let's say we want to allow the user to configure the greeting language\. For example:
.P
.RS 2
.nf
$ resin hello Juan \-\-language spanish
.fi
.RE
.P
We first need to register the \fBlanguage option\fR:
.P
.RS 2
.nf
module\.exports = {
signature: 'hello <name>',
description: 'example plugin',
help: 'This is an example plugin',
options: [
{
signature: 'language',
parameter: 'language',
description: 'the greeting language',
alias: 'l'
}
],
action: function(params, options, done) {
if(options\.language === 'spanish') {
console\.log('Hola ' + params\.name + '!');
} else {
console\.log('Hey there ' + params\.name + '!');
}
done();
}
}
.fi
.RE
.P
Here, we declared an option with a signature of \fBlanguage\fR (so we can use it as \fB\-\-language\fR), a parameter name of \fBlanguage\fR as well (this means we'll be able to access the option as the \fBlanguage\fR key: \fBoptions\.language\fR), a nice description and an alias \fBl\fR (which means we can use \fB\-l <language>\fR too)\.
.SH COFFEESCRIPT
.P
We have CoffeeScript support out of the box\. Implement your commands in \fBindex\.coffee\fR and point \fBpackage\.json\fR \fBmain\fR to that file\.
.SH RESIN\-SDK
.P
You can use the Resin SDK NodeJS module within your own plugins to communicate with Resin\.
.SH RESIN\-CLI\-VISUALS
.P
Use the Resin CLI Visuals module to make use of the widgets used by the built\-in CLI commands\.