Skip to content

Commit

Permalink
chore: Add none output and silent flag (twilio#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anuj Badhwar authored Aug 5, 2021
1 parent e88136d commit d650f8e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/base-commands/base-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ class BaseCommand extends Command {
this.flags = flags;
await this.loadConfig();

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

this.logger = logger;
this.logger.config.level = LoggingLevel[flags['cli-log-level'] || DEFAULT_LOG_LEVEL];
// Give precedence to silent flag
this.logger.config.level = this.flags.silent
? LoggingLevel.none
: LoggingLevel[flags['cli-log-level'] || DEFAULT_LOG_LEVEL];

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

Expand Down Expand Up @@ -113,6 +118,11 @@ class BaseCommand extends Command {
}

output(fullData, properties, options) {
if (!this.outputProcessor) {
// Silenced output
return;
}

const dataArray = fullData.constructor === Array ? fullData : [fullData];

if (dataArray.length === 0) {
Expand Down Expand Up @@ -204,6 +214,11 @@ BaseCommand.flags = {
options: Object.keys(OutputFormats),
description: 'Format of command output.',
}),

silent: oclifFlags.boolean({
description: 'Suppress output and logs. This is a shorthand for "-l none -o none".',
default: false,
}),
};

module.exports = BaseCommand;
1 change: 1 addition & 0 deletions src/services/output-formats/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const OutputFormats = {
columns: require('./columns'),
json: require('./json'),
tsv: require('./tsv'),
none: undefined,
};

module.exports = {
Expand Down
32 changes: 32 additions & 0 deletions test/base-commands/base-command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,38 @@ describe('base-commands', () => {
ctx.testCmd.output(testData);
expect(ctx.stdout).to.contain('FOO\tBAR\nfoo\tbar\n2\t2');
});

test
.twilioCliEnv(Config)
.do(async (ctx) => {
ctx.testCmd = new BaseCommand(['-o', 'none'], ctx.fakeConfig);
await ctx.testCmd.run();
})
.stdout()
.it('should not output to stdout with none flag', (ctx) => {
const testData = [
{ foo: 'foo', bar: 'bar' },
{ foo: 'test', bar: 'test' },
];
ctx.testCmd.output(testData);
expect(ctx.stdout).to.be.empty;
expect(ctx.testCmd.outputProcessor).to.be.undefined;
});

test
.twilioCliEnv(Config)
.do(async (ctx) => {
ctx.testCmd = new BaseCommand(['--silent'], ctx.fakeConfig);
await ctx.testCmd.run();
})
.stdout()
.it('should not output to stdout with silent flag', (ctx) => {
const testData = [{ foo: 'foo', bar: 'bar' }];
ctx.testCmd.output(testData);
expect(ctx.stdout).to.be.empty;
expect(ctx.testCmd.logger.config.level).to.equal(LoggingLevel.none);
expect(ctx.testCmd.outputProcessor).to.be.undefined;
});
});

describe('getPromptMessage', () => {
Expand Down

0 comments on commit d650f8e

Please sign in to comment.