2015-09-04 10:09:19 +03:00
|
|
|
/*
|
2015-09-04 21:12:20 +03:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2016-01-14 19:22:13 +02:00
|
|
|
* Copyright 2016 Joyent, Inc.
|
2015-09-04 10:09:19 +03:00
|
|
|
*
|
|
|
|
* `triton completion ...`
|
|
|
|
*/
|
|
|
|
|
2016-01-08 21:07:43 +02:00
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
|
2016-02-04 15:39:50 +02:00
|
|
|
var CloudApi = require('./cloudapi2').CloudApi;
|
|
|
|
var UPDATE_ACCOUNT_FIELDS = CloudApi.prototype.UPDATE_ACCOUNT_FIELDS;
|
|
|
|
var UPDATE_FWRULE_FIELDS = CloudApi.prototype.UPDATE_FWRULE_FIELDS;
|
2016-01-18 21:58:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Replace {{variable}} in `s` with the template data in `d`.
|
|
|
|
function renderTemplate(s, d) {
|
|
|
|
return s.replace(/{{([a-zA-Z_]+)}}/g, function (match, key) {
|
|
|
|
return d.hasOwnProperty(key) ? d[key] : match;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-01-08 21:07:43 +02:00
|
|
|
|
2015-09-04 10:09:19 +03:00
|
|
|
function do_completion(subcmd, opts, args, cb) {
|
|
|
|
if (opts.help) {
|
|
|
|
this.do_help('help', {}, [subcmd], cb);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-08 21:07:43 +02:00
|
|
|
if (opts.raw) {
|
|
|
|
console.log(this.bashCompletionSpec());
|
|
|
|
} else {
|
2016-01-18 21:58:01 +02:00
|
|
|
var specExtraIn = fs.readFileSync(
|
2016-01-08 21:07:43 +02:00
|
|
|
path.join(__dirname, '../etc/triton-bash-completion-types.sh'),
|
|
|
|
'utf8');
|
2016-01-18 21:58:01 +02:00
|
|
|
var specExtra = renderTemplate(specExtraIn, {
|
|
|
|
UPDATE_ACCOUNT_FIELDS: Object.keys(UPDATE_ACCOUNT_FIELDS).sort()
|
2016-02-04 15:39:50 +02:00
|
|
|
.map(function (field) { return field + '='; }).join(' '),
|
|
|
|
UPDATE_FWRULE_FIELDS: Object.keys(UPDATE_FWRULE_FIELDS).sort()
|
2016-01-18 21:58:01 +02:00
|
|
|
.map(function (field) { return field + '='; }).join(' ')
|
|
|
|
});
|
2016-01-08 21:07:43 +02:00
|
|
|
console.log(this.bashCompletion({specExtra: specExtra}));
|
|
|
|
}
|
2015-09-04 10:09:19 +03:00
|
|
|
cb();
|
|
|
|
}
|
|
|
|
|
|
|
|
do_completion.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
2016-01-08 21:07:43 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['raw'],
|
|
|
|
type: 'bool',
|
|
|
|
hidden: true,
|
|
|
|
help: 'Only output the Bash completion "spec". ' +
|
|
|
|
'This is only useful for debugging.'
|
2015-09-04 10:09:19 +03:00
|
|
|
}
|
|
|
|
];
|
|
|
|
do_completion.help = [
|
2016-02-04 15:39:50 +02:00
|
|
|
'Emit bash completion. See help for installation.',
|
2015-09-04 10:09:19 +03:00
|
|
|
'',
|
2016-03-02 09:02:40 +02:00
|
|
|
'Installation (Mac):',
|
|
|
|
' {{name}} completion > /usr/local/etc/bash_completion.d/{{name}}',
|
|
|
|
' source /usr/local/etc/bash_completion.d/{{name}}',
|
|
|
|
'',
|
|
|
|
'Installation (Linux):',
|
2016-01-14 19:22:13 +02:00
|
|
|
' sudo {{name}} completion > /etc/bash_completion.d/{{name}} # Linux',
|
2016-03-02 09:02:40 +02:00
|
|
|
' source /etc/bash_completion.d/{{name}}',
|
2016-01-08 21:07:43 +02:00
|
|
|
'',
|
|
|
|
'Alternative installation:',
|
2016-03-02 09:02:40 +02:00
|
|
|
' {{name}} completion > ~/.{{name}}.completion # or to whatever path',
|
2016-01-08 21:07:43 +02:00
|
|
|
' echo "source ~/.{{name}}.completion" >> ~/.bashrc',
|
2015-09-04 10:09:19 +03:00
|
|
|
'',
|
2016-01-08 21:07:43 +02:00
|
|
|
'{{options}}'
|
2015-09-04 10:09:19 +03:00
|
|
|
].join('\n');
|
|
|
|
|
|
|
|
module.exports = do_completion;
|