Skip to content

Commit d650f8e

Browse files
author
Anuj Badhwar
authored
chore: Add none output and silent flag (twilio#139)
1 parent e88136d commit d650f8e

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

src/base-commands/base-command.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,15 @@ class BaseCommand extends Command {
3737
this.flags = flags;
3838
await this.loadConfig();
3939

40-
this.outputProcessor = OutputFormats[this.flags['cli-output-format'] || DEFAULT_OUTPUT_FORMAT];
40+
this.outputProcessor = this.flags.silent
41+
? OutputFormats.none
42+
: OutputFormats[this.flags['cli-output-format'] || DEFAULT_OUTPUT_FORMAT];
4143

4244
this.logger = logger;
43-
this.logger.config.level = LoggingLevel[flags['cli-log-level'] || DEFAULT_LOG_LEVEL];
45+
// Give precedence to silent flag
46+
this.logger.config.level = this.flags.silent
47+
? LoggingLevel.none
48+
: LoggingLevel[flags['cli-log-level'] || DEFAULT_LOG_LEVEL];
4449

4550
this.logger.debug(`Config File: ${this.configFile.filePath}`);
4651

@@ -113,6 +118,11 @@ class BaseCommand extends Command {
113118
}
114119

115120
output(fullData, properties, options) {
121+
if (!this.outputProcessor) {
122+
// Silenced output
123+
return;
124+
}
125+
116126
const dataArray = fullData.constructor === Array ? fullData : [fullData];
117127

118128
if (dataArray.length === 0) {
@@ -204,6 +214,11 @@ BaseCommand.flags = {
204214
options: Object.keys(OutputFormats),
205215
description: 'Format of command output.',
206216
}),
217+
218+
silent: oclifFlags.boolean({
219+
description: 'Suppress output and logs. This is a shorthand for "-l none -o none".',
220+
default: false,
221+
}),
207222
};
208223

209224
module.exports = BaseCommand;

src/services/output-formats/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const OutputFormats = {
22
columns: require('./columns'),
33
json: require('./json'),
44
tsv: require('./tsv'),
5+
none: undefined,
56
};
67

78
module.exports = {

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,38 @@ describe('base-commands', () => {
219219
ctx.testCmd.output(testData);
220220
expect(ctx.stdout).to.contain('FOO\tBAR\nfoo\tbar\n2\t2');
221221
});
222+
223+
test
224+
.twilioCliEnv(Config)
225+
.do(async (ctx) => {
226+
ctx.testCmd = new BaseCommand(['-o', 'none'], ctx.fakeConfig);
227+
await ctx.testCmd.run();
228+
})
229+
.stdout()
230+
.it('should not output to stdout with none flag', (ctx) => {
231+
const testData = [
232+
{ foo: 'foo', bar: 'bar' },
233+
{ foo: 'test', bar: 'test' },
234+
];
235+
ctx.testCmd.output(testData);
236+
expect(ctx.stdout).to.be.empty;
237+
expect(ctx.testCmd.outputProcessor).to.be.undefined;
238+
});
239+
240+
test
241+
.twilioCliEnv(Config)
242+
.do(async (ctx) => {
243+
ctx.testCmd = new BaseCommand(['--silent'], ctx.fakeConfig);
244+
await ctx.testCmd.run();
245+
})
246+
.stdout()
247+
.it('should not output to stdout with silent flag', (ctx) => {
248+
const testData = [{ foo: 'foo', bar: 'bar' }];
249+
ctx.testCmd.output(testData);
250+
expect(ctx.stdout).to.be.empty;
251+
expect(ctx.testCmd.logger.config.level).to.equal(LoggingLevel.none);
252+
expect(ctx.testCmd.outputProcessor).to.be.undefined;
253+
});
222254
});
223255

224256
describe('getPromptMessage', () => {

0 commit comments

Comments
 (0)