-
Notifications
You must be signed in to change notification settings - Fork 39
/
do_completion.js
86 lines (77 loc) · 2.7 KB
/
do_completion.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* 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/.
*/
/*
* Copyright 2020 Joyent, Inc.
*
* `triton completion ...`
*/
var fs = require('fs');
var path = require('path');
var CloudApi = require('./cloudapi2').CloudApi;
var UPDATE_ACCOUNT_FIELDS = CloudApi.prototype.UPDATE_ACCOUNT_FIELDS;
var UPDATE_FWRULE_FIELDS = CloudApi.prototype.UPDATE_FWRULE_FIELDS;
var UPDATE_IMAGE_FIELDS = CloudApi.prototype.UPDATE_IMAGE_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;
});
}
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(' '),
UPDATE_FWRULE_FIELDS: Object.keys(UPDATE_FWRULE_FIELDS).sort()
.map(function (field) { return field + '='; }).join(' '),
UPDATE_IMAGE_FIELDS: Object.keys(UPDATE_IMAGE_FIELDS).sort()
.map(function (field) { return field + '='; }).join(' ')
});
console.log(this.bashCompletion({specExtra: specExtra}));
}
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.'
}
];
do_completion.help = [
'Emit bash completion. See help for installation.',
'',
'Installation (Mac):',
' {{name}} completion > /usr/local/etc/bash_completion.d/{{name}} \\',
' && source /usr/local/etc/bash_completion.d/{{name}}',
'',
'Installation (Linux):',
' {{name}} completion | sudo tee /etc/bash_completion.d/{{name}} \\',
' && source /etc/bash_completion.d/{{name}}',
'',
'Alternative installation:',
' {{name}} completion > ~/.{{name}}.completion # or to whatever path',
' echo "source ~/.{{name}}.completion" >> ~/.bashrc',
'',
'{{options}}'
].join('\n');
module.exports = do_completion;