-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathcmd.js
More file actions
93 lines (84 loc) · 2.53 KB
/
cmd.js
File metadata and controls
93 lines (84 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env node
'use strict';
const chalk = require('chalk');
const { exec } = require('child_process');
const homedir = require('os').homedir();
const fs = require('fs');
const Configstore = require('configstore');
const path = require('path');
const pkg = require('../../../../package.json');
const common = require('../common/cmd');
const removeContainer = require('../common/removeContainer');
const dockerImage = `cloudlibz/clocal-gcp-memory-store:latest`;
const defaultPort = 7070;
const action = (cmd, first, second) => {
switch (cmd) {
case 'start':
start();
break;
case 'stop':
stop();
break;
default:
console.log(`command invalid ${cmd} ${first} ${second}`);
}
};
const start = () => {
console.log(common.figlet());
try {
console.log(chalk.blueBright('starting gcp memory store ...'));
const config = new Configstore(path.join(pkg.name, '.containerList'));
exec(
`docker run -d -p ${defaultPort}:7070 ${dockerImage}`,
(err, stdout, stderr) => {
config.set('memorystore', stdout.trim());
const dockerId = config.get('memorystore');
if (err){
removeContainer(dockerId);
return console.log(chalk.bgRed(`failed to start\n${stderr}`));
}
console.log(
chalk.green.bgWhiteBright(
`gcp memory store started. Listening on ${defaultPort}`
)
);
}
);
} catch (err) {
console.log(chalk.blueBright.bgRed(err));
}
};
const stop = () => {
try {
console.log(chalk.blueBright('stopping memory store ...'));
const config = new Configstore(path.join(pkg.name, '.containerList'));
const dockerId = config.get('memorystore');
if (dockerId) {
exec(`docker container rm -f ${dockerId}`, (err, stdout, stderr) => {
if (err) console.log(chalk.bgRed(`failed to stop\n${stderr}`));
if (dockerId === stdout.trim()) {
console.log(
chalk.green.bgWhiteBright(`stopped memory store. ${dockerId}`)
);
} else {
console.log(
chalk.blueBright.bgRed(
'something unintended happen. Try to manually stop the docker containers ...'
)
);
}
config.delete('memorystore');
});
} else {
console.log(
chalk.blueBright.bgRed('clocal gcp memory store is not running ...')
);
}
} catch (err) {
console.log(chalk.blueBright.bgRed(err));
}
};
module.exports = {
commandName: 'mem <cmd> [first] [second]',
action: action,
};