Skip to content

Commit fbdc36b

Browse files
authored
feat: Added support to make profile input mandatory based on config property (#135)
* feat: Added support to make profile input mandatory based on config property * removing duplicate test
1 parent d243890 commit fbdc36b

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/base-commands/twilio-client-command.js

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ class TwilioClientCommand extends BaseCommand {
2424
async run() {
2525
await super.run();
2626

27+
// check if profile flag is required as per the config
28+
if (this.userConfig.requireProfileInput && !this.flags.profile) {
29+
throw new TwilioCliError(
30+
`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`,
31+
);
32+
}
2733
this.currentProfile = this.userConfig.getProfileById(this.flags.profile);
2834
let keytarFlag = false;
2935

src/services/config.js

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class ConfigData {
3232
this.projects = [];
3333
this.activeProfile = null;
3434
this.profiles = {};
35+
this.requireProfileInput = undefined;
3536
}
3637

3738
getProfileFromEnvironment() {
@@ -182,6 +183,7 @@ class ConfigData {
182183
loadFromObject(configObj) {
183184
this.edge = configObj.edge;
184185
this.email = configObj.email || {};
186+
this.requireProfileInput = configObj.requireProfileInput;
185187
this.prompts = configObj.prompts || {};
186188
// Note the historical 'projects' naming.
187189
configObj.projects = configObj.projects || [];
@@ -217,6 +219,7 @@ class Config {
217219
configData = {
218220
edge: configData.edge,
219221
email: configData.email,
222+
requireProfileInput: configData.requireProfileInput,
220223
prompts: configData.prompts,
221224
// Note the historical 'projects' naming.
222225
projects: configData.projects,

test/base-commands/twilio-client-command.test.js

+23
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ describe('base-commands', () => {
4242
envEdge,
4343
configRegion = 'configRegion',
4444
configEdge,
45+
configRequireProfileInput,
4546
} = {},
4647
) => {
4748
return test
4849
.do((ctx) => {
4950
ctx.userConfig = new ConfigData();
5051
ctx.userConfig.edge = configEdge;
52+
ctx.userConfig.requireProfileInput = configRequireProfileInput;
5153

5254
if (envRegion) {
5355
process.env.TWILIO_REGION = envRegion;
@@ -89,6 +91,27 @@ describe('base-commands', () => {
8991
});
9092
};
9193

94+
setUpTest([], { configRequireProfileInput: true })
95+
.exit(1)
96+
.it('should fail if requireProfileInput attribute in config is set but flag is not passed', (ctx) => {
97+
expect(ctx.stderr).to.contain('Error: Missing required flag:');
98+
expect(ctx.stderr).to.contain('-p, --profile PROFILE');
99+
});
100+
101+
setUpTest(['-p', ''], { configRequireProfileInput: true })
102+
.exit(1)
103+
.it('should fail if requireProfileInput attribute in config is set but flag is passed as empty string', (ctx) => {
104+
expect(ctx.stderr).to.contain('Error: Missing required flag:');
105+
expect(ctx.stderr).to.contain('-p, --profile PROFILE');
106+
});
107+
108+
setUpTest(['-p', 'region-edge-testing'], { configRequireProfileInput: true }).it(
109+
'should use the profile passed, when requireProfileInput flag is set in config and valid profile is passed',
110+
(ctx) => {
111+
expect(ctx.testCmd.currentProfile.id).to.equal('region-edge-testing');
112+
},
113+
);
114+
92115
setUpTest(['-l', 'debug']).it('should create a client for the active profile', (ctx) => {
93116
expect(ctx.stderr).to.contain('MyFirstProfile');
94117
expect(ctx.testCmd.twilioClient.accountSid).to.equal(constants.FAKE_ACCOUNT_SID);

0 commit comments

Comments
 (0)