From be85eb0468c395a3954f47d767b3979db279b618 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 12 Mar 2024 14:16:12 +0100 Subject: [PATCH] feat(cli-repl): make `--quiet` the default for non-interactive usage MONGOSH-1721 (#1869) This can be disabled through `--no-quiet` or `--verbose` (although `--verbose` may gain other meanings in the future depending on the outcome of MONGOSH-970). --- packages/cli-repl/src/cli-repl.spec.ts | 18 ++++++++++++++++-- packages/cli-repl/src/cli-repl.ts | 23 +++++++++++++++-------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/packages/cli-repl/src/cli-repl.spec.ts b/packages/cli-repl/src/cli-repl.spec.ts index 8ed447eeb..fbacb645f 100644 --- a/packages/cli-repl/src/cli-repl.spec.ts +++ b/packages/cli-repl/src/cli-repl.spec.ts @@ -609,6 +609,7 @@ describe('CliRepl', function () { 'hello1.js' ); cliReplOptions.shellCliOptions.fileNames = [filename1]; + cliReplOptions.shellCliOptions.quiet = false; cliRepl = new CliRepl(cliReplOptions); await startWithExpectedImmediateExit(cliRepl, ''); expect(output).to.include(`Loading file: ${filename1}`); @@ -634,6 +635,7 @@ describe('CliRepl', function () { 'hello2.js' ); cliReplOptions.shellCliOptions.fileNames = [filename1, filename2]; + cliReplOptions.shellCliOptions.quiet = false; cliRepl = new CliRepl(cliReplOptions); await startWithExpectedImmediateExit(cliRepl, ''); expect(output).to.include(`Loading file: ${filename1}`); @@ -643,7 +645,7 @@ describe('CliRepl', function () { expect(exitCode).to.equal(0); }); - it('does not print filenames if --quiet is passed', async function () { + it('does not print filenames if --quiet is implied', async function () { const filename1 = path.resolve( __dirname, '..', @@ -653,7 +655,6 @@ describe('CliRepl', function () { 'hello1.js' ); cliReplOptions.shellCliOptions.fileNames = [filename1]; - cliReplOptions.shellCliOptions.quiet = true; cliRepl = new CliRepl(cliReplOptions); await startWithExpectedImmediateExit(cliRepl, ''); expect(output).not.to.include('Loading file'); @@ -671,6 +672,7 @@ describe('CliRepl', function () { 'throw.js' ); cliReplOptions.shellCliOptions.fileNames = [filename1]; + cliReplOptions.shellCliOptions.quiet = false; cliRepl = new CliRepl(cliReplOptions); try { await cliRepl.start('', {}); @@ -1202,6 +1204,16 @@ describe('CliRepl', function () { }); it('has the full greeting if --quiet is not passed', async function () { + cliRepl = new CliRepl(cliReplOptions); + await cliRepl.start(await testServer.connectionString(), {}); + // Full greeting: + expect(output).to.match(/Current Mongosh Log ID:/); + expect(output).to.match(/Connecting to:/); + expect(output).to.match(/Using MongoDB:/); + expect(output).to.match(/For mongosh info see:/); + }); + + it('has the full greeting if --quiet is set to false', async function () { cliReplOptions.shellCliOptions.quiet = false; cliRepl = new CliRepl(cliReplOptions); await cliRepl.start(await testServer.connectionString(), {}); @@ -1641,6 +1653,7 @@ describe('CliRepl', function () { 'hello1.js' ); cliReplOptions.shellCliOptions.fileNames = [filename1]; + cliReplOptions.shellCliOptions.quiet = false; cliRepl = new CliRepl(cliReplOptions); await startWithExpectedImmediateExit( cliRepl, @@ -1669,6 +1682,7 @@ describe('CliRepl', function () { 'hello2.js' ); cliReplOptions.shellCliOptions.fileNames = [filename1, filename2]; + cliReplOptions.shellCliOptions.quiet = false; cliRepl = new CliRepl(cliReplOptions); await startWithExpectedImmediateExit( cliRepl, diff --git a/packages/cli-repl/src/cli-repl.ts b/packages/cli-repl/src/cli-repl.ts index ad30fb220..069ccff93 100644 --- a/packages/cli-repl/src/cli-repl.ts +++ b/packages/cli-repl/src/cli-repl.ts @@ -186,16 +186,16 @@ export class CliRepl implements MongoshIOProvider { }); let jsContext = this.cliOptions.jsContext; + const { willEnterInteractiveMode, quiet } = CliRepl.getFileAndEvalInfo( + this.cliOptions + ); if (jsContext === 'auto' || !jsContext) { - jsContext = CliRepl.getFileAndEvalInfo(this.cliOptions) - .willEnterInteractiveMode - ? 'repl' - : 'plain-vm'; + jsContext = willEnterInteractiveMode ? 'repl' : 'plain-vm'; } this.mongoshRepl = new MongoshNodeRepl({ ...options, - shellCliOptions: { ...this.cliOptions, jsContext }, + shellCliOptions: { ...this.cliOptions, jsContext, quiet }, nodeReplOptions: options.nodeReplOptions ?? { terminal: process.env.MONGOSH_FORCE_TERMINAL ? true : undefined, }, @@ -273,7 +273,8 @@ export class CliRepl implements MongoshIOProvider { await this.logManager.cleanupOldLogfiles(); markTime(TimingCategories.Logging, 'cleaned up log files'); const logger = await this.logManager.createLogWriter(); - if (!this.cliOptions.quiet) { + const { quiet } = CliRepl.getFileAndEvalInfo(this.cliOptions); + if (!quiet) { this.output.write(`Current Mongosh Log ID:\t${logger.logId}\n`); } this.logWriter = logger; @@ -493,6 +494,7 @@ export class CliRepl implements MongoshIOProvider { evalScripts: string[]; willExecuteCommandLineScripts: boolean; willEnterInteractiveMode: boolean; + quiet: boolean; } { const commandLineLoadFiles = cliOptions.fileNames ?? []; const evalScripts = cliOptions.eval ?? []; @@ -500,11 +502,14 @@ export class CliRepl implements MongoshIOProvider { commandLineLoadFiles.length > 0 || evalScripts.length > 0; const willEnterInteractiveMode = !willExecuteCommandLineScripts || !!cliOptions.shell; + const quiet = + cliOptions.quiet ?? !(cliOptions.verbose ?? willEnterInteractiveMode); return { commandLineLoadFiles, evalScripts, willEnterInteractiveMode, willExecuteCommandLineScripts, + quiet, }; } @@ -633,8 +638,9 @@ export class CliRepl implements MongoshIOProvider { markTime(TimingCategories.Eval, 'wrote eval output'); markTime(TimingCategories.EvalFile, 'start loading external files'); + const { quiet } = CliRepl.getFileAndEvalInfo(this.cliOptions); for (const file of files) { - if (!this.cliOptions.quiet) { + if (!quiet) { this.output.write( `Loading file: ${this.clr(file, 'mongosh:filename')}\n` ); @@ -786,7 +792,8 @@ export class CliRepl implements MongoshIOProvider { driverUri: string, driverOptions: DevtoolsConnectOptions ): Promise { - if (!this.cliOptions.nodb && !this.cliOptions.quiet) { + const { quiet } = CliRepl.getFileAndEvalInfo(this.cliOptions); + if (!this.cliOptions.nodb && !quiet) { this.output.write( i18n.__(CONNECTING) + '\t\t' +