Skip to content
This repository was archived by the owner on Dec 20, 2023. It is now read-only.

Commit f6307d0

Browse files
fedotovLoneRifle
authored andcommitted
Passing download parameters for cli commands through ENV variables (#55)
1 parent 18e5337 commit f6307d0

16 files changed

+70
-119
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ Complete list of programs:
5454

5555
Works on Mac, Windows, Linux and Solaris OSes that MongoDB supports.
5656

57+
## CLI usage
58+
The latest version of MongoDB is used by default for your OS and `~/.mongodb-prebuilt` for downloading MongoDB binary.
59+
You can set desired version, download folder, architecture and platform through environment variables:
60+
```
61+
MONGODB_VERSION
62+
MONGODB_DOWNLOADDIR
63+
MONGODB_ARCH
64+
MONGODB_PLATFORM
65+
```
66+
For example:
67+
```
68+
export MONGODB_DOWNLOADDIR='./' MONGODB_VERSION=3.4.10
69+
mongod --port 27018 --dbpath ./mongodb --logpath /dev/stdout
70+
```
5771
## Programmatic usage
5872

5973
```javascript

src/bin/bsondump.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
#!/usr/bin/env node
2+
import { runCommand } from './runCommandHelper';
23

3-
import {MongoBins} from '../mongo-bins';
4-
const COMMAND: string = "bsondump";
5-
6-
let mongoBin: MongoBins = new MongoBins(COMMAND, process.argv.slice(2), {stdio: 'inherit'});
7-
mongoBin.run().then(() => {
8-
//console.log(`${COMMAND} is now running`);
9-
}, (e) => {
10-
console.error(`unable to launch ${COMMAND}`, e);
11-
});
4+
runCommand('bsondump');

src/bin/mongo.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
#!/usr/bin/env node
2+
import { runCommand } from './runCommandHelper';
23

3-
import {MongoBins} from '../mongo-bins';
4-
const COMMAND: string = "mongo";
5-
6-
let mongoBin: MongoBins = new MongoBins(COMMAND, process.argv.slice(2), {stdio: 'inherit'});
7-
mongoBin.run().then(() => {
8-
//console.log(`${COMMAND} is now running`);
9-
console.log('command is running');
10-
}, (e) => {
11-
console.error(`unable to launch ${COMMAND}`, e);
12-
});
4+
runCommand('mongo');

src/bin/mongod.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
#!/usr/bin/env node
2+
import { runCommand } from './runCommandHelper';
23

3-
import {MongoBins} from '../mongo-bins';
4-
const COMMAND: string = "mongod";
5-
6-
let mongoBin: MongoBins = new MongoBins(COMMAND, process.argv.slice(2), {stdio: 'inherit'});
7-
mongoBin.run().then(() => {
8-
//console.log(`${COMMAND} is now running`);
9-
}, (e) => {
10-
console.error(`unable to launch ${COMMAND}`, e);
11-
});
12-
4+
runCommand('mongod');

src/bin/mongodump.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
#!/usr/bin/env node
2+
import { runCommand } from './runCommandHelper';
23

3-
import {MongoBins} from '../mongo-bins';
4-
const COMMAND: string = "mongodump";
5-
6-
let mongoBin: MongoBins = new MongoBins(COMMAND, process.argv.slice(2), {stdio: 'inherit'});
7-
mongoBin.run().then(() => {
8-
//console.log(`${COMMAND} is now running`);
9-
}, (e) => {
10-
console.error(`unable to launch ${COMMAND}`, e);
11-
});
4+
runCommand('mongodump');

src/bin/mongoexport.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
#!/usr/bin/env node
2+
import { runCommand } from './runCommandHelper';
23

3-
import {MongoBins} from '../mongo-bins';
4-
const COMMAND: string = "mongoexport";
5-
6-
let mongoBin: MongoBins = new MongoBins(COMMAND, process.argv.slice(2), {stdio: 'inherit'});
7-
mongoBin.run().then(() => {
8-
//console.log(`${COMMAND} is now running`);
9-
}, (e) => {
10-
console.error(`unable to launch ${COMMAND}`, e);
11-
});
4+
runCommand('mongoexport');

src/bin/mongofiles.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
#!/usr/bin/env node
2-
import {MongoBins} from '../mongo-bins';
3-
const COMMAND: string = "mongofiles";
2+
import { runCommand } from './runCommandHelper';
43

5-
let mongoBin: MongoBins = new MongoBins(COMMAND, process.argv.slice(2), {stdio: 'inherit'});
6-
mongoBin.run().then(() => {
7-
//console.log(`${COMMAND} is now running`);
8-
}, (e) => {
9-
console.error(`unable to launch ${COMMAND}`, e);
10-
});
4+
runCommand('mongofiles');

src/bin/mongoimport.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
#!/usr/bin/env node
2-
import {MongoBins} from '../mongo-bins';
3-
const COMMAND: string = "mongoimport";
2+
import { runCommand } from './runCommandHelper';
43

5-
let mongoBin: MongoBins = new MongoBins(COMMAND, process.argv.slice(2), {stdio: 'inherit'});
6-
mongoBin.run().then(() => {
7-
//console.log(`${COMMAND} is now running`);
8-
}, (e) => {
9-
console.error(`unable to launch ${COMMAND}`, e);
10-
});
4+
runCommand('mongoimport');

src/bin/mongooplog.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
#!/usr/bin/env node
2-
import {MongoBins} from '../mongo-bins';
3-
const COMMAND: string = "mongooplog";
2+
import { runCommand } from './runCommandHelper';
43

5-
let mongoBin: MongoBins = new MongoBins(COMMAND, process.argv.slice(2), {stdio: 'inherit'});
6-
mongoBin.run().then(() => {
7-
//console.log(`${COMMAND} is now running`);
8-
}, (e) => {
9-
console.error(`unable to launch ${COMMAND}`, e);
10-
});
4+
runCommand('mongooplog');

src/bin/mongoperf.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
#!/usr/bin/env node
2-
import {MongoBins} from '../mongo-bins';
3-
const COMMAND: string = "mongoperf";
2+
import { runCommand } from './runCommandHelper';
43

5-
let mongoBin: MongoBins = new MongoBins(COMMAND, process.argv.slice(2), {stdio: 'inherit'});
6-
mongoBin.run().then(() => {
7-
//console.log(`${COMMAND} is now running`);
8-
}, (e) => {
9-
console.error(`unable to launch ${COMMAND}`, e);
10-
});
4+
runCommand('mongoperf');

src/bin/mongorestore.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
#!/usr/bin/env node
2-
import {MongoBins} from '../mongo-bins';
3-
const COMMAND: string = "mongorestore";
2+
import { runCommand } from './runCommandHelper';
43

5-
let mongoBin: MongoBins = new MongoBins(COMMAND, process.argv.slice(2), {stdio: 'inherit'});
6-
mongoBin.run().then(() => {
7-
//console.log(`${COMMAND} is now running`);
8-
}, (e) => {
9-
console.error(`unable to launch ${COMMAND}`, e);
10-
});
4+
runCommand('mongorestore');

src/bin/mongos.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
#!/usr/bin/env node
2-
import {MongoBins} from '../mongo-bins';
3-
const COMMAND: string = "mongos";
2+
import { runCommand } from './runCommandHelper';
43

5-
let mongoBin: MongoBins = new MongoBins(COMMAND, process.argv.slice(2), {stdio: 'inherit'});
6-
mongoBin.run().then(() => {
7-
//console.log(`${COMMAND} is now running`);
8-
}, (e) => {
9-
console.error(`unable to launch ${COMMAND}`, e);
10-
});
4+
runCommand('mongos');

src/bin/mongosniff.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
#!/usr/bin/env node
2-
import {MongoBins} from '../mongo-bins';
3-
const COMMAND: string = "mongosniff";
2+
import { runCommand } from './runCommandHelper';
43

5-
let mongoBin: MongoBins = new MongoBins(COMMAND, process.argv.slice(2), {stdio: 'inherit'});
6-
mongoBin.run().then(() => {
7-
//console.log(`${COMMAND} is now running`);
8-
}, (e) => {
9-
console.error(`unable to launch ${COMMAND}`, e);
10-
});
4+
runCommand('mongosniff');

src/bin/mongostat.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
#!/usr/bin/env node
2-
import {MongoBins} from '../mongo-bins';
3-
const COMMAND: string = "mongostat";
2+
import { runCommand } from './runCommandHelper';
43

5-
let mongoBin: MongoBins = new MongoBins(COMMAND, process.argv.slice(2), {stdio: 'inherit'});
6-
mongoBin.run().then(() => {
7-
//console.log(`${COMMAND} is now running`);
8-
}, (e) => {
9-
console.error(`unable to launch ${COMMAND}`, e);
10-
});
4+
runCommand('mongostat');

src/bin/mongotop.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
#!/usr/bin/env node
2-
import {MongoBins} from '../mongo-bins';
3-
const COMMAND: string = "mongotop";
2+
import { runCommand } from './runCommandHelper';
43

5-
let mongoBin: MongoBins = new MongoBins(COMMAND, process.argv.slice(2), {stdio: 'inherit'});
6-
mongoBin.run().then(() => {
7-
//console.log(`${COMMAND} is now running`);
8-
}, (e) => {
9-
console.error(`unable to launch ${COMMAND}`, e);
10-
});
4+
runCommand('mongotop');

src/bin/runCommandHelper.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { MongoBins } from '../mongo-bins';
2+
import { IMongoDBDownloadOptions } from 'mongodb-download';
3+
4+
export function runCommand(command: string) {
5+
const downloadOpts = getDownloadOpts();
6+
const mongoBin: MongoBins = new MongoBins(command, process.argv.slice(2), { stdio: 'inherit' }, downloadOpts);
7+
8+
mongoBin.run()
9+
// .then(() => console.log(`${command} is now running`))
10+
.catch((error) => console.error(`unable to launch ${command}`, error));
11+
}
12+
13+
function getDownloadOpts() : Partial<IMongoDBDownloadOptions> {
14+
const opts: Partial<IMongoDBDownloadOptions> = {};
15+
setIfExists(opts, 'version');
16+
setIfExists(opts, 'downloadDir');
17+
setIfExists(opts, 'arch');
18+
setIfExists(opts, 'platform');
19+
20+
return Object.keys(opts).length > 0 ? opts : undefined;
21+
}
22+
23+
function setIfExists(obj: Partial<IMongoDBDownloadOptions>, property: keyof IMongoDBDownloadOptions): void {
24+
const value = process.env['MONGODB_' + property.toUpperCase()];
25+
if (value) {
26+
obj[property] = value;
27+
}
28+
}

0 commit comments

Comments
 (0)