Skip to content

Commit 8df4d3e

Browse files
committed
MONGOSH-301 - Support raw command execution methods
1 parent 620b5e2 commit 8df4d3e

File tree

7 files changed

+154
-0
lines changed

7 files changed

+154
-0
lines changed

packages/browser-repl/src/components/shell-output-line.spec.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,20 @@ describe('<ShellOutputLine />', () => {
153153
expect(wrapper.text()).to.include('metadata');
154154
});
155155

156+
it('renders ListCommandsResult', () => {
157+
const wrapper = mount(<ShellOutputLine entry={{
158+
format: 'output',
159+
type: 'ListCommandsResult',
160+
value: {
161+
c1: { metadata: 1, help: 'help string' },
162+
}
163+
}}/>);
164+
165+
expect(wrapper.text()).to.include('help string');
166+
expect(wrapper.text()).to.include('c1');
167+
expect(wrapper.text()).to.include('metadata');
168+
});
169+
156170
it('renders an error', () => {
157171
const err = new Error('x');
158172
const wrapper = shallow(<ShellOutputLine entry={{ format: 'output', value: err }} />);

packages/browser-repl/src/components/shell-output-line.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable complexity */
12
import React, { Component } from 'react';
23
import classnames from 'classnames';
34

@@ -64,6 +65,10 @@ export class ShellOutputLine extends Component<ShellOutputLineProps> {
6465
return <SimpleTypeOutput value={res} />;
6566
}
6667

68+
if (type === 'ListCommandsResult)') {
69+
return <SimpleTypeOutput value={value} />;
70+
}
71+
6772
if (type === 'ShowCollectionsResult') {
6873
return <ShowCollectionsOutput value={value} />;
6974
}

packages/cli-repl/src/format-output.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,20 @@ describe('formatOutput', () => {
129129
});
130130
});
131131

132+
context('when the result is ListCommandsResult', () => {
133+
it('returns the formatted list', () => {
134+
const output = stripAnsiColors(format({
135+
value: {
136+
c1: { metadata1: 1, help: 'help1' },
137+
c2: { metadata2: 2, help: 'help2' }
138+
},
139+
type: 'ListCommandsResult'
140+
}));
141+
142+
expect(output).to.contain('c1: metadata1\nhelp1\n\nc2: metadata2\nhelp2');
143+
});
144+
});
145+
132146
context('when the result is Help', () => {
133147
it('returns help text', () => {
134148
const output = stripAnsiColors(format({

packages/cli-repl/src/format-output.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ export default function formatOutput(evaluationResult: EvaluationResult): string
4949
return formatStats(value);
5050
}
5151

52+
if (type === 'ListCommandsResult') {
53+
return formatListCommands(value);
54+
}
55+
5256
if (type === 'Error') {
5357
return formatError(value);
5458
}
@@ -81,6 +85,23 @@ function formatStats(output): string {
8185
}).join('\n---\n');
8286
}
8387

88+
function formatListCommands(output): string {
89+
const tableEntries = Object.keys(output).map(
90+
(cmd) => {
91+
const val = output[cmd];
92+
let result = Object.keys(val).filter(k => k !== 'help').reduce((str, k) => {
93+
if (val[k]) {
94+
return `${str} ${clr(k, ['bold', 'white'])}`;
95+
}
96+
return str;
97+
}, `${clr(cmd, ['bold', 'yellow'])}: `);
98+
result += val.help ? `\n${clr(val.help, 'green')}` : '';
99+
return result;
100+
}
101+
);
102+
return tableEntries.join('\n\n');
103+
}
104+
84105
export function formatError(error): string {
85106
let result = '';
86107
if (error.name) result += `\r${clr(error.name, ['bold', 'red'])}: `;

packages/i18n/src/locales/en_US.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,16 @@ const translations = {
10201020
link: 'https://docs.mongodb.com/manual/reference/method/db.getLogComponents',
10211021
description: 'returns the db getLogComponents. uses the getParameter command',
10221022
example: 'db.getLogComponents()'
1023+
},
1024+
listCommands: {
1025+
link: 'https://docs.mongodb.com/manual/reference/method/db.listCommands',
1026+
description: 'Calls the listCommands command',
1027+
example: 'db.listCommands()'
1028+
},
1029+
commandHelp: {
1030+
link: 'https://docs.mongodb.com/manual/reference/method/db.commandHelp',
1031+
description: 'returns the db commandHelp. uses the passed in command with help: true',
1032+
example: 'db.commandHelp(<command>)',
10231033
}
10241034
}
10251035
}

packages/shell-api/src/database.spec.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,64 @@ describe('Database', () => {
19361936
expect(catchedError).to.equal(expectedError);
19371937
});
19381938
});
1939+
1940+
describe('commandHelp', () => {
1941+
it('calls serviceProvider.runCommand on the database with options', async() => {
1942+
await database.commandHelp('listDatabases');
1943+
1944+
expect(serviceProvider.runCommand).to.have.been.calledWith(
1945+
database._name,
1946+
{
1947+
listDatabases: 1,
1948+
help: true
1949+
}
1950+
);
1951+
});
1952+
1953+
it('returns whatever serviceProvider.runCommand().help returns', async() => {
1954+
const expectedResult = { ok: 1, help: 'help string' };
1955+
serviceProvider.runCommand.resolves(expectedResult);
1956+
const result = await database.commandHelp('listDatabases');
1957+
expect(result).to.deep.equal('help string');
1958+
});
1959+
1960+
it('throws if serviceProvider.runCommand rejects', async() => {
1961+
const expectedError = new Error();
1962+
serviceProvider.runCommand.rejects(expectedError);
1963+
const catchedError = await database.commandHelp('listDatabases')
1964+
.catch(e => e);
1965+
expect(catchedError).to.equal(expectedError);
1966+
});
1967+
});
1968+
1969+
describe('listCommands', () => {
1970+
it('calls serviceProvider.runCommand on the database', async() => {
1971+
await database.listCommands();
1972+
1973+
expect(serviceProvider.runCommand).to.have.been.calledWith(
1974+
database._name,
1975+
{
1976+
listCommands: 1
1977+
}
1978+
);
1979+
});
1980+
1981+
it('returns ListCommandsResult', async() => {
1982+
const expectedResult = { ok: 1, commands: { c1: { requiresAuth: false, slaveOk: true, adminOnly: false, help: 'help string' } } };
1983+
serviceProvider.runCommand.resolves(expectedResult);
1984+
const result = await database.listCommands();
1985+
expect(result.value).to.deep.equal(expectedResult.commands);
1986+
expect(result.type).to.equal('ListCommandsResult');
1987+
});
1988+
1989+
it('throws if serviceProvider.runCommand rejects', async() => {
1990+
const expectedError = new Error();
1991+
serviceProvider.runCommand.rejects(expectedError);
1992+
const catchedError = await database.listCommands()
1993+
.catch(e => e);
1994+
expect(catchedError).to.equal(expectedError);
1995+
});
1996+
});
19391997
});
19401998
});
19411999

packages/shell-api/src/database.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,4 +917,36 @@ export default class Database extends ShellApiClass {
917917
}
918918
return result.logComponentVerbosity;
919919
}
920+
921+
async commandHelp(name: string): Promise<any> {
922+
assertArgsDefined(name);
923+
this._emitDatabaseApiCall('commandHelp', { name: name });
924+
const command = {} as any;
925+
command[name] = 1;
926+
command.help = true;
927+
928+
const result = await this._mongo._serviceProvider.runCommand(
929+
this._name,
930+
command
931+
);
932+
if (!result || !result.ok) {
933+
throw new MongoshRuntimeError(`Error running command listComands ${result ? result.errmsg || '' : ''}`);
934+
}
935+
return result.help;
936+
}
937+
938+
@returnsPromise
939+
async listCommands(): Promise<any> {
940+
this._emitDatabaseApiCall('listCommands', {});
941+
const result = await this._mongo._serviceProvider.runCommand(
942+
this._name,
943+
{
944+
listCommands: 1,
945+
}
946+
);
947+
if (!result || !result.ok) {
948+
throw new MongoshRuntimeError(`Error running command listCommands ${result ? result.errmsg || '' : ''}`);
949+
}
950+
return new CommandResult('ListCommandsResult', result.commands);
951+
}
920952
}

0 commit comments

Comments
 (0)