Skip to content

Commit

Permalink
chore: Indicate environment variables being used in profiles:list (tw…
Browse files Browse the repository at this point in the history
…ilio#247)

* Fix profile list

* Refactor profile/list.js

* Added unit tests

* Display envProfile as first row in output

Co-authored-by: childish-sambino <sharrison@twilio.com>
  • Loading branch information
onuzbee and childish-sambino committed Apr 15, 2021
1 parent 985c916 commit cc67cd3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/commands/profiles/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@ 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)) {
this.userConfig.profiles.forEach((p) => delete p.region);
}
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 {
Expand Down
17 changes: 17 additions & 0 deletions test/commands/profiles/list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit cc67cd3

Please sign in to comment.