Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

const reportUnconfigured = (verb, message = '', commandName = 'create') => {
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