-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcli.js
executable file
·206 lines (184 loc) · 4.97 KB
/
cli.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env node
const pkg = require('./package.json');
const config = require('./config');
const program = require('commander');
const invite = require('./invite');
const redis = require('./redis');
const Table = require('cli-table');
const web = require('./web');
const pubsub = require('./pubsub');
const logger = require('./logger');
const { db, Configuration, Installation, Team, User } = require('./models');
// Makes the script crash on unhandled rejections instead of silently
// ignoring them. In the future, promise rejections that are not handled will
// terminate the Node.js process with a non-zero exit code.
process.on('unhandledRejection', err => {
throw err;
});
// Set the package version.
program.version(pkg.version);
program
.command('serve')
.description('starts the application server')
.option(
'-w, --web_only',
'only starts the web server, does not start the pubsub subscriber'
)
.option(
'-p, --pubsub_only',
'only starts the pubsub subscriber, does not start the web server'
)
.action(options => {
try {
if (options.pubsub_only || !options.web_only) {
pubsub.subscribe();
logger.debug('subscribed to the pubsub topic', {
web_only: options.web_only,
pubsub_only: options.pubsub_only,
});
}
if (options.web_only || !options.pubsub_only) {
web.listen();
logger.debug('started the web server', {
web_only: options.web_only,
pubsub_only: options.pubsub_only,
});
}
} catch (err) {
logger.error('error during server start', { err: err.message });
process.exit(1);
}
});
program
.command('invite <domain>')
.description('creates a slack domain join link')
.action(async domain => {
try {
const token = await invite.create(domain);
console.log('Invite token');
console.log(config.get('root_url') + 'invite/' + token);
redis.disconnect();
db.close();
} catch (err) {
console.error(err);
process.exit(1);
}
});
program
.command('stats')
.description('show current stats')
.action(async () => {
try {
const [configs, installs, teams, users] = await Promise.all([
Configuration.find().count(),
Installation.find().count(),
Team.find().count(),
User.find().count(),
]);
const table = new Table();
table.push(
{ Configurations: configs },
{ Installations: installs },
{ Teams: teams },
{ Users: users }
);
console.log(table.toString());
redis.disconnect();
db.close();
} catch (err) {
console.error(err);
process.exit(1);
}
});
program
.command('disable <teamID>')
.description(
'disables a team from logging in and having their comments processed'
)
.action(async teamID => {
try {
const team = await Team.findOneAndUpdate(
{ id: teamID },
{ $set: { disabled: true } }
);
if (!team) {
throw new Error(`Team ${teamID} not found`);
}
console.log(`Team ${teamID} was disabled.`);
redis.disconnect();
db.close();
} catch (err) {
console.error(err);
process.exit(1);
}
});
program
.command('enable <teamID>')
.description(
'enables a team so they can log in and have their comments processed'
)
.action(async teamID => {
try {
const team = await Team.findOneAndUpdate(
{ id: teamID },
{ $set: { disabled: false } }
);
if (!team) {
throw new Error(`Team ${teamID} not found`);
}
console.log(`Team ${teamID} was enabled.`);
redis.disconnect();
db.close();
} catch (err) {
console.error(err);
process.exit(1);
}
});
program
.command('teams')
.description('show all the existing teams')
.option('-a, --show_all', 'Shows all teams (default shows just enabled)')
.action(async options => {
try {
const query = Team.where({});
if (!options.show_all) {
query.merge({
disabled: false,
});
}
const teams = await query.sort('ID');
if (teams.length !== 0) {
const table = new Table({
head: ['ID', 'Name', 'Domain', 'Created', 'Enabled'],
});
for (const team of teams) {
table.push([
team.id,
team.name,
team.domain,
team.created_at.toString(),
team.disabled ? 'No' : 'Yes',
]);
}
console.log(table.toString());
} else {
console.log('There are no teams to display.');
}
redis.disconnect();
db.close();
} catch (err) {
console.error(err);
process.exit(1);
}
});
program.command('*', '', { noHelp: true, isDefault: true }).action(function() {
redis.disconnect();
db.close();
program.outputHelp();
});
program.parse(process.argv);
if (!process.argv.slice(2).length) {
redis.disconnect();
db.close();
program.outputHelp();
}