clistyle: Much improved bash completions (importantly for the clistyle changes, it supports subsubcommands)

This commit is contained in:
Trent Mick 2016-01-08 11:07:43 -08:00
parent ea759565c7
commit 922ca13816
7 changed files with 55 additions and 18 deletions

View File

@ -2,6 +2,8 @@
## 4.0.0 (not yet released) ## 4.0.0 (not yet released)
- Much improved Bash completion.
- XXX changes in `triton instance,key,network,account`. - XXX changes in `triton instance,key,network,account`.
Drops `triton whoami`. Want to keep that? Drops `triton whoami`. Want to keep that?

View File

@ -61,17 +61,24 @@ for example:
### Bash completion ### Bash completion
You can quickly source `triton` bash completions in your current Install Bash completion with
shell with:
source <(triton completion) ```bash
For a more permanent installation:
triton completion >> ~/.bashrc
# Or maybe:
triton completion > /usr/local/etc/bash_completion.d/triton triton completion > /usr/local/etc/bash_completion.d/triton
```
Alternatively, if you don't have or don't want to use a "bash\_completion.d"
dir, then something like this would work:
```bash
triton completion > ~/.triton.completion
echo "source ~/.triton.completion" >> ~/.bashrc
```
Then open a new shell or manually `source FILE` that completion file, and
play with the bash completions:
triton <TAB>
## `triton` CLI Usage ## `triton` CLI Usage

View File

@ -0,0 +1,8 @@
# Functions for Bash completion of some 'triton' option/arg types.
function complete_tritonprofile {
local word="$1"
local candidates
candidates=$(ls -1 ~/.triton/profiles.d/*.json 2>/dev/null \
| sed -E 's/^.*\/([^\/]+)\.json$/\1/')
compgen $compgen_opts -W "$candidates" -- "$word"
}

View File

@ -17,7 +17,6 @@ var child_process = require('child_process'),
exec = child_process.exec; exec = child_process.exec;
var cmdln = require('cmdln'), var cmdln = require('cmdln'),
Cmdln = cmdln.Cmdln; Cmdln = cmdln.Cmdln;
var fs = require('fs');
var mkdirp = require('mkdirp'); var mkdirp = require('mkdirp');
var util = require('util'), var util = require('util'),
format = util.format; format = util.format;
@ -57,6 +56,7 @@ var OPTIONS = [
{ {
names: ['profile', 'p'], names: ['profile', 'p'],
type: 'string', type: 'string',
completionType: 'tritonprofile',
env: 'TRITON_PROFILE', env: 'TRITON_PROFILE',
helpArg: 'NAME', helpArg: 'NAME',
help: 'Triton client profile to use.' help: 'Triton client profile to use.'

View File

@ -10,13 +10,24 @@
* `triton completion ...` * `triton completion ...`
*/ */
var fs = require('fs');
var path = require('path');
function do_completion(subcmd, opts, args, cb) { function do_completion(subcmd, opts, args, cb) {
if (opts.help) { if (opts.help) {
this.do_help('help', {}, [subcmd], cb); this.do_help('help', {}, [subcmd], cb);
return; return;
} }
console.log(this.bashCompletion()); if (opts.raw) {
console.log(this.bashCompletionSpec());
} else {
var specExtra = fs.readFileSync(
path.join(__dirname, '../etc/triton-bash-completion-types.sh'),
'utf8');
console.log(this.bashCompletion({specExtra: specExtra}));
}
cb(); cb();
} }
@ -25,16 +36,26 @@ do_completion.options = [
names: ['help', 'h'], names: ['help', 'h'],
type: 'bool', type: 'bool',
help: 'Show this help.' help: 'Show this help.'
},
{
names: ['raw'],
type: 'bool',
hidden: true,
help: 'Only output the Bash completion "spec". ' +
'This is only useful for debugging.'
} }
]; ];
do_completion.help = [ do_completion.help = [
'Output bash completion code for the `triton` CLI.', 'Output bash completion code for the `triton` CLI.',
'', '',
'Installation:', 'Installation:',
' triton completion >> ~/.bashrc', ' {{name}} completion > /usr/local/etc/bash_completion.d/{{name}}',
'', '',
'Or maybe:', 'Alternative installation:',
' triton completion > /usr/local/etc/bash_completion.d/triton' ' {{name}} completion > ~/.{{name}}.completion',
' echo "source ~/.{{name}}.completion" >> ~/.bashrc',
'',
'{{options}}'
].join('\n'); ].join('\n');
do_completion.hidden = true; do_completion.hidden = true;

View File

@ -8,7 +8,7 @@
"backoff": "2.4.1", "backoff": "2.4.1",
"bigspinner": "3.1.0", "bigspinner": "3.1.0",
"bunyan": "1.5.1", "bunyan": "1.5.1",
"cmdln": "3.4.2", "cmdln": "3.5.0",
"extsprintf": "1.0.2", "extsprintf": "1.0.2",
"lomstream": "1.1.0", "lomstream": "1.1.0",
"mkdirp": "0.5.1", "mkdirp": "0.5.1",

View File

@ -43,8 +43,8 @@ test('triton account', function (tt) {
}); });
}); });
tt.test(' triton account', function (t) { tt.test(' triton account get', function (t) {
h.triton('account', function (err, stdout, stderr) { h.triton('account get', function (err, stdout, stderr) {
if (h.ifErr(t, err)) if (h.ifErr(t, err))
return t.end(); return t.end();
t.ok(new RegExp( t.ok(new RegExp(
@ -53,8 +53,8 @@ test('triton account', function (tt) {
}); });
}); });
tt.test(' triton account -j', function (t) { tt.test(' triton account get -j', function (t) {
h.triton('account -j', function (err, stdout, stderr) { h.triton('account get -j', function (err, stdout, stderr) {
if (h.ifErr(t, err)) if (h.ifErr(t, err))
return t.end(); return t.end();
var account = JSON.parse(stdout); var account = JSON.parse(stdout);
@ -62,5 +62,4 @@ test('triton account', function (tt) {
t.end(); t.end();
}); });
}); });
}); });