diff --git a/src/commands/profiles/list.js b/src/commands/profiles/list.js index 6f1f9fbe9..9c7e3c139 100644 --- a/src/commands/profiles/list.js +++ b/src/commands/profiles/list.js @@ -4,6 +4,18 @@ const { BaseCommand } = require('@twilio/cli-core').baseCommands; class ProfilesList extends BaseCommand { async run() { await super.run(); + const envProfile = this.userConfig.getProfileFromEnvironment(); + // If environment profile exists, add required details to userConfig.profiles, and mark as active. + if (envProfile) { + const { accountSid: ENVIRONMENT_ACCOUNT_SID, region: ENVIRONMENT_REGION } = envProfile; + const strippedEnvProfile = { + id: '[env]', + accountSid: ENVIRONMENT_ACCOUNT_SID, + region: ENVIRONMENT_REGION, + }; + this.userConfig.profiles.unshift(strippedEnvProfile); + this.userConfig.setActiveProfile(strippedEnvProfile.id); + } if (this.userConfig.profiles.length > 0) { // If none of the profiles have a region, delete it from all of them so it doesn't show up in the output. if (!this.userConfig.profiles.some((p) => p.region)) { @@ -11,9 +23,7 @@ class ProfilesList extends BaseCommand { } const activeProfile = this.userConfig.getActiveProfile(); this.userConfig.profiles.forEach((p) => { - if (p.id === activeProfile.id) { - p.active = true; - } + p.active = p.id === activeProfile.id; }); this.output(this.userConfig.profiles); } else { diff --git a/test/commands/profiles/list.test.js b/test/commands/profiles/list.test.js index 40c03c93b..e97510c20 100644 --- a/test/commands/profiles/list.test.js +++ b/test/commands/profiles/list.test.js @@ -33,6 +33,23 @@ describe('commands', () => { expect(ctx.stderr).to.equal(''); }); + test + .do(() => { + process.env.TWILIO_ACCOUNT_SID = constants.FAKE_ACCOUNT_SID; + process.env.TWILIO_AUTH_TOKEN = constants.FAKE_API_SECRET; + }) + .twilioCliEnv(Config) + .stdout() + .stderr() + .twilioCommand(ProfilesList, []) + .it('runs profiles:list with environment variables set', (ctx) => { + expect(ctx.stdout).to.contain('[env]'); + expect(ctx.stdout).to.contain(constants.FAKE_ACCOUNT_SID); + expect(ctx.stdout).to.not.contain('Region'); + expect(ctx.stdout.match(/true/g)).to.have.length(1); + expect(ctx.stderr).to.equal(''); + }); + test .do((ctx) => { ctx.userConfig = new ConfigData();