From a48d7629dd47182d6be5112c772e602546a627b3 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Fri, 22 Apr 2016 12:55:32 -0700 Subject: [PATCH] joyent/node-triton#111 want `triton env --unset,-u` option emit env to unset relevant envvars --- CHANGES.md | 6 +- TODO.txt | 7 +- lib/cli.js | 3 +- lib/do_env.js | 94 +++++++++++++++++++----- package.json | 2 +- test/integration/cli-subcommands.test.js | 2 + 6 files changed, 86 insertions(+), 28 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index bb88fe5..f5efc7c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -5,9 +5,11 @@ Known issues: - `triton ssh ...` disables ssh ControlMaster to avoid issue #52. -## 4.10.1 (not yet released) +## 4.11.0 (not yet released) -(nothing yet) +- [#111] `triton env --unset,-u` option to emit environment commands to *unset* + relevant envvars. +- Unhide `triton env` from `triton --help` output. ## 4.10.0 diff --git a/TODO.txt b/TODO.txt index 14e3de3..94c391c 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,9 +1,4 @@ -test suite: -- all the commands: test/integration/cli-*.test.js -- TritonApi testing: test/integration/api-*.test.js -- more test/unit/... - -sub-user support (profiles, `triton account`, env, auth) +triton create affinity support for tag matching, globs, regex note in README that full UUIDs is much faster in the API diff --git a/lib/cli.js b/lib/cli.js index 6928aa2..0499760 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -207,8 +207,9 @@ function CLI() { }, helpSubcmds: [ 'help', - 'completion', 'profile', + 'env', + 'completion', { group: 'Instances (aka VMs/Machines/Containers)' }, 'instance', 'instances', diff --git a/lib/do_env.js b/lib/do_env.js index 9fb6211..9c15584 100644 --- a/lib/do_env.js +++ b/lib/do_env.js @@ -45,6 +45,9 @@ function do_env(subcmd, opts, args, cb) { shortOpts += 's'; clientTypes.push('smartdc'); } + if (opts.unset) { + shortOpts += 'u'; + } if (clientTypes.length === 0) { explicit = false; clientTypes = allClientTypes; @@ -68,7 +71,20 @@ function do_env(subcmd, opts, args, cb) { clientTypes.forEach(function (clientType) { switch (clientType) { case 'triton': - p('export TRITON_PROFILE="%s"', profile.name); + if (opts.unset) { + [ + 'TRITON_PROFILE', + 'TRITON_URL', + 'TRITON_ACCOUNT', + 'TRITON_USER', + 'TRITON_KEY_ID', + 'TRITON_TLS_INSECURE' + ].forEach(function (key) { + p('unset %s', key); + }); + } else { + p('export TRITON_PROFILE="%s"', profile.name); + } break; case 'docker': var setupJson = path.resolve(self.configDir, 'docker', @@ -85,12 +101,21 @@ function do_env(subcmd, opts, args, cb) { } Object.keys(setup.env).forEach(function (key) { var val = setup.env[key]; - if (val === null) { + if (opts.unset || val === null) { p('unset %s', key); } else { p('export %s=%s', key, val); } }); + } else if (opts.unset) { + [ + 'DOCKER_HOST', + 'DOCKER_CERT_PATH', + 'DOCKER_TLS_VERIFY', + 'COMPOSE_HTTP_TIMEOUT' + ].forEach(function (key) { + p('unset %s', key); + }); } else if (explicit) { cb(new errors.ConfigError(format('could not find Docker ' + 'environment setup for profile "%s":\n Run `triton ' @@ -99,18 +124,30 @@ function do_env(subcmd, opts, args, cb) { } break; case 'smartdc': - p('export SDC_URL="%s"', profile.url); - p('export SDC_ACCOUNT="%s"', profile.account); - if (profile.user) { - p('export SDC_USER="%s"', profile.user); + if (opts.unset) { + [ + 'SDC_URL', + 'SDC_ACCOUNT', + 'SDC_USER', + 'SDC_KEY_ID', + 'SDC_TESTING' + ].forEach(function (key) { + p('unset %s', key); + }); } else { - p('unset SDC_USER'); - } - p('export SDC_KEY_ID="%s"', profile.keyId); - if (profile.insecure) { - p('export SDC_TESTING="%s"', profile.insecure); - } else { - p('unset SDC_TESTING'); + p('export SDC_URL="%s"', profile.url); + p('export SDC_ACCOUNT="%s"', profile.account); + if (profile.user) { + p('export SDC_USER="%s"', profile.user); + } else { + p('unset SDC_USER'); + } + p('export SDC_KEY_ID="%s"', profile.keyId); + if (profile.insecure) { + p('export SDC_TESTING="%s"', profile.insecure); + } else { + p('unset SDC_TESTING'); + } } break; default: @@ -132,6 +169,9 @@ do_env.options = [ type: 'bool', help: 'Show this help.' }, + { + group: '' + }, { names: ['triton', 't'], type: 'bool', @@ -147,13 +187,21 @@ do_env.options = [ names: ['smartdc', 's'], type: 'bool', help: 'Emit environment for node-smartdc (i.e. the "SDC_*" variables).' + }, + { + group: '' + }, + { + names: ['unset', 'u'], + type: 'bool', + help: 'Emit environment to *unset* the relevant environment variables.' } ]; // TODO: support env for docker usage. do_env.help = [ /* BEGIN JSSTYLED */ - 'Emit shell environment commands to setup clients for a particular CLI profile.', + 'Emit shell commands to setup environment.', '', 'Supported "clients" here are: node-smartdc (i.e. the `sdc-*` tools),', 'and node-triton itself. By default this emits the environment for all', @@ -162,11 +210,21 @@ do_env.help = [ 'Usage:', ' {{name}} env [PROFILE]', '', - '{{options}}' + '{{options}}', + 'If no options are given, environment variables are emitted for all clients.', + 'If PROFILE is not given, the current profile is used.', + '', + 'The following Bash function can be added to one\'s "~/.bashrc" to quickly', + 'change between Triton profiles:', + ' triton-select () { eval "$(triton env $1)"; }', + 'for example:', + ' $ triton-select west1', + ' $ triton profile get | grep name', + ' name: west1', + ' $ triton-select east1', + ' $ triton profile get | grep name', + ' name: east1' /* END JSSTYLED */ ].join('\n'); - -do_env.hidden = true; - module.exports = do_env; diff --git a/package.json b/package.json index 9f12d08..5e8926a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "triton", "description": "Joyent Triton CLI and client (https://www.joyent.com/triton)", - "version": "4.10.1", + "version": "4.11.0", "author": "Joyent (joyent.com)", "dependencies": { "assert-plus": "0.2.0", diff --git a/test/integration/cli-subcommands.test.js b/test/integration/cli-subcommands.test.js index 1086341..d13f439 100644 --- a/test/integration/cli-subcommands.test.js +++ b/test/integration/cli-subcommands.test.js @@ -28,6 +28,8 @@ var subs = [ ['profile create'], ['profile edit'], ['profile delete', 'profile rm'], + ['env'], + ['completion'], ['account'], ['account get'], ['account update'],