This repository has been archived on 2020-01-20. You can view files and clone it, but cannot push or open issues or pull requests.
node-spearhead/lib/do_completion.js

78 lines
2.1 KiB
JavaScript
Raw Normal View History

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 ...`
*/
var fs = require('fs');
var path = require('path');
var UPDATE_ACCOUNT_FIELDS
= require('./cloudapi2').CloudApi.prototype.UPDATE_ACCOUNT_FIELDS;
// 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;
});
}
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;
}
if (opts.raw) {
console.log(this.bashCompletionSpec());
} else {
var specExtraIn = fs.readFileSync(
path.join(__dirname, '../etc/triton-bash-completion-types.sh'),
'utf8');
var specExtra = renderTemplate(specExtraIn, {
UPDATE_ACCOUNT_FIELDS: Object.keys(UPDATE_ACCOUNT_FIELDS).sort()
.map(function (field) { return field + '='; }).join(' ')
});
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.'
},
{
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-01-14 19:22:13 +02:00
'Output bash completion. See help output for installation.',
2015-09-04 10:09:19 +03:00
'',
'Installation:',
2016-01-14 19:22:13 +02:00
' {{name}} completion > /usr/local/etc/bash_completion.d/{{name}} # Mac',
' sudo {{name}} completion > /etc/bash_completion.d/{{name}} # Linux',
'',
'Alternative installation:',
' {{name}} completion > ~/.{{name}}.completion',
' echo "source ~/.{{name}}.completion" >> ~/.bashrc',
2015-09-04 10:09:19 +03:00
'',
'{{options}}'
2015-09-04 10:09:19 +03:00
].join('\n');
module.exports = do_completion;