Skip to content

Commit

Permalink
feat: Added support to make profile input mandatory based on config p…
Browse files Browse the repository at this point in the history
…roperty (#135)

* feat: Added support to make profile input mandatory based on config property

* removing duplicate test
  • Loading branch information
kridai committed Jul 29, 2021
1 parent d243890 commit fbdc36b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/base-commands/twilio-client-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ class TwilioClientCommand extends BaseCommand {
async run() {
await super.run();

// check if profile flag is required as per the config
if (this.userConfig.requireProfileInput && !this.flags.profile) {
throw new TwilioCliError(
`Error: Missing required flag:\n -p, --profile PROFILE ${TwilioClientCommand.flags.profile.description} To disable this check run:\n\n twilio config:set --no-require-profile-input`,
);
}
this.currentProfile = this.userConfig.getProfileById(this.flags.profile);
let keytarFlag = false;

Expand Down
3 changes: 3 additions & 0 deletions src/services/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ConfigData {
this.projects = [];
this.activeProfile = null;
this.profiles = {};
this.requireProfileInput = undefined;
}

getProfileFromEnvironment() {
Expand Down Expand Up @@ -182,6 +183,7 @@ class ConfigData {
loadFromObject(configObj) {
this.edge = configObj.edge;
this.email = configObj.email || {};
this.requireProfileInput = configObj.requireProfileInput;
this.prompts = configObj.prompts || {};
// Note the historical 'projects' naming.
configObj.projects = configObj.projects || [];
Expand Down Expand Up @@ -217,6 +219,7 @@ class Config {
configData = {
edge: configData.edge,
email: configData.email,
requireProfileInput: configData.requireProfileInput,
prompts: configData.prompts,
// Note the historical 'projects' naming.
projects: configData.projects,
Expand Down
23 changes: 23 additions & 0 deletions test/base-commands/twilio-client-command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ describe('base-commands', () => {
envEdge,
configRegion = 'configRegion',
configEdge,
configRequireProfileInput,
} = {},
) => {
return test
.do((ctx) => {
ctx.userConfig = new ConfigData();
ctx.userConfig.edge = configEdge;
ctx.userConfig.requireProfileInput = configRequireProfileInput;

if (envRegion) {
process.env.TWILIO_REGION = envRegion;
Expand Down Expand Up @@ -89,6 +91,27 @@ describe('base-commands', () => {
});
};

setUpTest([], { configRequireProfileInput: true })
.exit(1)
.it('should fail if requireProfileInput attribute in config is set but flag is not passed', (ctx) => {
expect(ctx.stderr).to.contain('Error: Missing required flag:');
expect(ctx.stderr).to.contain('-p, --profile PROFILE');
});

setUpTest(['-p', ''], { configRequireProfileInput: true })
.exit(1)
.it('should fail if requireProfileInput attribute in config is set but flag is passed as empty string', (ctx) => {
expect(ctx.stderr).to.contain('Error: Missing required flag:');
expect(ctx.stderr).to.contain('-p, --profile PROFILE');
});

setUpTest(['-p', 'region-edge-testing'], { configRequireProfileInput: true }).it(
'should use the profile passed, when requireProfileInput flag is set in config and valid profile is passed',
(ctx) => {
expect(ctx.testCmd.currentProfile.id).to.equal('region-edge-testing');
},
);

setUpTest(['-l', 'debug']).it('should create a client for the active profile', (ctx) => {
expect(ctx.stderr).to.contain('MyFirstProfile');
expect(ctx.testCmd.twilioClient.accountSid).to.equal(constants.FAKE_ACCOUNT_SID);
Expand Down

0 comments on commit fbdc36b

Please sign in to comment.