From edb28b6791d286a88c57d1435f8b2b7f4e621d91 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Tue, 2 Feb 2016 22:03:01 -0800 Subject: [PATCH] node-triton#86 "triton profiles" should not require a current profile --- CHANGES.md | 3 ++- lib/config.js | 20 +++++++++++++++----- lib/do_profile/do_list.js | 21 ++++++++++++++++++--- lib/do_profile/profilecommon.js | 15 ++++++++++++--- test/integration/cli-account.test.js | 4 ++-- 5 files changed, 49 insertions(+), 14 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 0bcfa05..00a1624 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,7 +2,8 @@ ## 4.4.3 (not yet released) -(nothing yet) +- #86 Ensure `triton profile ls` and `triton profile set-current` work + when there is no current profile. ## 4.4.2 diff --git a/lib/config.js b/lib/config.js index 6152eee..9517f3c 100644 --- a/lib/config.js +++ b/lib/config.js @@ -5,7 +5,7 @@ */ /* - * Copyright 2015 Joyent, Inc. + * Copyright 2016 Joyent, Inc. */ /* @@ -101,8 +101,13 @@ function loadConfig(opts) { var config = JSON.parse(c); if (configPath && fs.existsSync(configPath)) { c = fs.readFileSync(configPath, 'utf8'); - var _user = JSON.parse(c); - var userConfig = JSON.parse(c); + try { + var _user = JSON.parse(c); + var userConfig = JSON.parse(c); + } catch (userConfigParseErr) { + throw new errors.ConfigError( + format('"%s" is invalid JSON', configPath)); + } if (typeof (userConfig) !== 'object' || Array.isArray(userConfig)) { throw new errors.ConfigError( format('"%s" is not an object', configPath)); @@ -311,9 +316,14 @@ function loadProfile(opts) { assert.optionalString(opts.configDir, 'opts.configDir'); if (opts.name === 'env') { - return _loadEnvProfile(); + var envProfile = _loadEnvProfile(); + if (!envProfile) { + throw new errors.ConfigError('could not load "env" profile ' + + '(missing TRITON_*, or SDC_*, environment variables)'); + } + return envProfile; } else if (!opts.configDir) { - throw new errors.TritonError( + throw new errors.ConfigError( 'cannot load profiles (other than "env") without `opts.configDir`'); } else { var profilePath = path.resolve( diff --git a/lib/do_profile/do_list.js b/lib/do_profile/do_list.js index 1bc5813..c38e2de 100644 --- a/lib/do_profile/do_list.js +++ b/lib/do_profile/do_list.js @@ -1,7 +1,7 @@ /* - * Copyright (c) 2015 Joyent Inc. + * Copyright 2016 Joyent Inc. * - * `triton profiles ...` + * `triton profile list ...` */ var tabula = require('tabula'); @@ -38,9 +38,20 @@ function _listProfiles(cli, opts, args, cb) { } // Current profile: Set 'curr' field. Apply CLI overrides. + var currProfile; + try { + currProfile = cli.tritonapi.profile; + } catch (err) { + // Ignore inability to load a profile. + if (!(err instanceof errors.ConfigError)) { + throw err; + } + } + var haveCurr = false; for (i = 0; i < profiles.length; i++) { var profile = profiles[i]; - if (profile.name === cli.tritonapi.profile.name) { + if (currProfile && profile.name === currProfile.name) { + haveCurr = true; cli._applyProfileOverrides(profile); if (opts.json) { profile.curr = true; @@ -66,6 +77,10 @@ function _listProfiles(cli, opts, args, cb) { columns: columns, sort: sort }); + if (!haveCurr) { + process.stderr.write('\nWarning: There is no current profile. ' + + 'Use "triton profile set-current ..."\nto set one.\n'); + } } cb(); } diff --git a/lib/do_profile/profilecommon.js b/lib/do_profile/profilecommon.js index d650b6d..5edfba0 100644 --- a/lib/do_profile/profilecommon.js +++ b/lib/do_profile/profilecommon.js @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015 Joyent Inc. + * Copyright 2016 Joyent Inc. * * Shared stuff for `triton profile ...` handling. */ @@ -7,7 +7,7 @@ var assert = require('assert-plus'); var mod_config = require('../config'); - +var errors = require('../errors'); function setCurrentProfile(opts, cb) { @@ -25,7 +25,16 @@ function setCurrentProfile(opts, cb) { return cb(err); } - if (cli.tritonapi.profile.name === profile.name) { + var currProfile; + try { + currProfile = cli.tritonapi.profile; + } catch (err) { + // Ignore inability to load a profile. + if (!(err instanceof errors.ConfigError)) { + throw err; + } + } + if (currProfile && currProfile.name === profile.name) { console.log('"%s" is already the current profile', profile.name); return cb(); } diff --git a/test/integration/cli-account.test.js b/test/integration/cli-account.test.js index bcb251b..04b42b1 100644 --- a/test/integration/cli-account.test.js +++ b/test/integration/cli-account.test.js @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2015, Joyent, Inc. + * Copyright 2016, Joyent, Inc. */ /* @@ -47,7 +47,7 @@ test('triton account', function (tt) { }); tt.test(' triton account get', function (t) { - h.triton('account get', function (err, stdout, stderr) { + h.triton('-v account get', function (err, stdout, stderr) { if (h.ifErr(t, err)) return t.end(); t.ok(new RegExp(