Skip to content

Commit

Permalink
feat(cli-repl): make --quiet the default for non-interactive usage M…
Browse files Browse the repository at this point in the history
…ONGOSH-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).
  • Loading branch information
addaleax authored Mar 12, 2024
1 parent 5557024 commit be85eb0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
18 changes: 16 additions & 2 deletions packages/cli-repl/src/cli-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand All @@ -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}`);
Expand All @@ -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,
'..',
Expand All @@ -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');
Expand All @@ -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('', {});
Expand Down Expand Up @@ -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(), {});
Expand Down Expand Up @@ -1641,6 +1653,7 @@ describe('CliRepl', function () {
'hello1.js'
);
cliReplOptions.shellCliOptions.fileNames = [filename1];
cliReplOptions.shellCliOptions.quiet = false;
cliRepl = new CliRepl(cliReplOptions);
await startWithExpectedImmediateExit(
cliRepl,
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 15 additions & 8 deletions packages/cli-repl/src/cli-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -493,18 +494,22 @@ export class CliRepl implements MongoshIOProvider {
evalScripts: string[];
willExecuteCommandLineScripts: boolean;
willEnterInteractiveMode: boolean;
quiet: boolean;
} {
const commandLineLoadFiles = cliOptions.fileNames ?? [];
const evalScripts = cliOptions.eval ?? [];
const willExecuteCommandLineScripts =
commandLineLoadFiles.length > 0 || evalScripts.length > 0;
const willEnterInteractiveMode =
!willExecuteCommandLineScripts || !!cliOptions.shell;
const quiet =
cliOptions.quiet ?? !(cliOptions.verbose ?? willEnterInteractiveMode);
return {
commandLineLoadFiles,
evalScripts,
willEnterInteractiveMode,
willExecuteCommandLineScripts,
quiet,
};
}

Expand Down Expand Up @@ -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`
);
Expand Down Expand Up @@ -786,7 +792,8 @@ export class CliRepl implements MongoshIOProvider {
driverUri: string,
driverOptions: DevtoolsConnectOptions
): Promise<CliServiceProvider> {
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' +
Expand Down

0 comments on commit be85eb0

Please sign in to comment.