From 989a43f367fef12d96d5c6e5cc2f5433591c11e6 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Mon, 18 Jan 2016 11:58:01 -0800 Subject: [PATCH] bash completion for 'triton account update ' --- CHANGES.md | 3 +++ etc/triton-bash-completion-types.sh | 7 +++++++ lib/common.js | 3 ++- lib/do_account/do_update.js | 2 ++ lib/do_completion.js | 17 ++++++++++++++++- package.json | 2 +- 6 files changed, 31 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 49cbd90..ea4b67e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,9 @@ ## 4.2.0 (not yet released) +- Bash completion: Add completion for *args* to `triton account update `. + This isn't perfect because a space is added after completion of "FIELD=", + but hopefully is helpful. - #75 `triton account update ...` diff --git a/etc/triton-bash-completion-types.sh b/etc/triton-bash-completion-types.sh index 36f2fc9..2ae1e7f 100644 --- a/etc/triton-bash-completion-types.sh +++ b/etc/triton-bash-completion-types.sh @@ -6,3 +6,10 @@ function complete_tritonprofile { | sed -E 's/^.*\/([^\/]+)\.json$/\1/') compgen $compgen_opts -W "$candidates" -- "$word" } + +function complete_tritonupdateaccountfield { + local word="$1" + local candidates + candidates="{{UPDATE_ACCOUNT_FIELDS}}" + compgen $compgen_opts -W "$candidates" -- "$word" +} \ No newline at end of file diff --git a/lib/common.js b/lib/common.js index 5969c8f..d2a1312 100644 --- a/lib/common.js +++ b/lib/common.js @@ -915,7 +915,8 @@ function objFromKeyValueArgs(args, opts) args.forEach(function (arg) { var kv = strsplit(arg, '=', 2); if (kv.length < 2) { - throw new TypeError(format('invalid key=value argument: "%s"')); + throw new TypeError(format('invalid key=value argument: "%s"', + arg)); } var k = kv[0]; diff --git a/lib/do_account/do_update.js b/lib/do_account/do_update.js index c10f18c..ea0c0c4 100644 --- a/lib/do_account/do_update.js +++ b/lib/do_account/do_update.js @@ -169,4 +169,6 @@ do_update.help = [ /* END JSSTYLED */ ].join('\n'); +do_update.completionArgtypes = ['tritonupdateaccountfield']; + module.exports = do_update; diff --git a/lib/do_completion.js b/lib/do_completion.js index 888379e..f6426da 100644 --- a/lib/do_completion.js +++ b/lib/do_completion.js @@ -13,6 +13,17 @@ 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; + }); +} + function do_completion(subcmd, opts, args, cb) { if (opts.help) { @@ -23,9 +34,13 @@ function do_completion(subcmd, opts, args, cb) { if (opts.raw) { console.log(this.bashCompletionSpec()); } else { - var specExtra = fs.readFileSync( + 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})); } cb(); diff --git a/package.json b/package.json index 6390ac3..27acacd 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "backoff": "2.4.1", "bigspinner": "3.1.0", "bunyan": "1.5.1", - "cmdln": "3.5.2", + "cmdln": "3.5.3", "extsprintf": "1.0.2", "lomstream": "1.1.0", "mkdirp": "0.5.1",