forked from Saarryan/memphis-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·234 lines (220 loc) · 8.13 KB
/
index.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env node
// Credit for The NATS.IO Authors
// Copyright 2021-2022 The Memphis Authors
// Licensed under the Apache License, Version 2.0 (the “License”);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an “AS IS” BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.package server
const commander = require('commander');
const station = require('./actions/station');
const user = require('./actions/users');
const connect = require('./actions/connect');
const helper = require('./config/helper');
const producer = require('./actions/producer');
const consumer = require('./actions/consumer');
const init = require('./actions/init');
const cluster = require('./actions/cluster');
const packageDetails = require('./package.json');
const validateVersion = require('./utils/validateVersion');
const program = new commander.Command();
program
.version(`Memphis CLI version ${packageDetails.version}`)
.usage('<command> [options]')
// .description('Memphis CLI')
.addHelpText(
'after',
`
${helper.stationDesc}
${helper.stationHelp}
${helper.userDesc}
${helper.userHelp}
${helper.producerDesc}
${helper.producerHelp}
${helper.consumerDesc}
${helper.consumerHelp}
${helper.clusterDesc}
${helper.clusterHelp}
`
)
//TODO: add ${helper.hubDesc} ${helper.hubHelp}
.configureHelp({
sortSubcommands: false,
subcommandTerm: (cmd) => cmd.name() // Just show the name, instead of short usage.
});
program
.command('connect')
.description('Connection to Memphis')
.argument('[command]')
.option('-u, --user <user>', 'User')
.option('-p, --password <password>', 'Password')
.option('-s, --server <server>', 'Memphis')
.usage('<command> [options]')
.showHelpAfterError()
.addHelpText('after', helper.connectDesc)
.action(async function () {
await validateVersion();
if (Object.keys(this.opts()).length === 0 || !this.opts().user || !this.opts().password || !this.opts().server) {
console.log('\nUse command: mem connect --user <user> --password <password> --server <server>\n');
console.log('Example: mem connect -u root -p memphis -s <host>:<management_port>');
console.log(program.commands[0].help());
} else {
connect(this.opts());
}
});
program
.command('station')
.description('Stations usage commands')
.argument('<command>')
.option('-rt, --retentiontype <retention-type>', 'Retention type')
.option('-rv, --retentionvalue <retention-value>', 'Retention value')
.option('-s, --storage <storage-type>', 'Storage type')
.option('-r, --replicas <replicas>', 'Replicas')
.option('-ipw, --idempotency <idempotency-window-in-ms>', 'Idempotency window in ms')
.usage('<command> [options]')
.showHelpAfterError()
.configureHelp({
sortSubcommands: true,
subcommandTerm: (cmd) => cmd.name() // Just show the name, instead of short usage.
})
.addHelpText('before', helper.stationDesc)
.addHelpText('after', `\n${helper.stationHelp}`)
.action(async function () {
await validateVersion();
const stationActions = ['ls', 'create', 'info', 'del'];
if (!this.args?.length || !stationActions.includes(this.args[0])) {
console.log('');
console.log(program.commands[2].help());
} else {
station.stationMenu(this.args, this.opts());
}
});
program
.command('user')
.description('Users usage commands')
.argument('<command>')
.option('-u, --username <username>', 'Username')
.option('-p, --password <password>', 'User password')
.option('-t, --type <user-type>', 'User type', 'management')
// .option("-hu, --hubuser <hub-username>", "Hub username")
// .option("-hp, --hubpass <hub-password>", "Hub password")
.usage('<command> [options]')
.showHelpAfterError()
.configureHelp({
sortSubcommands: true,
subcommandTerm: (cmd) => cmd.name() // Just show the name, instead of short usage.
})
.addHelpText('before', helper.userDesc)
.addHelpText('after', `\n${helper.userHelp}`)
.action(async function () {
await validateVersion();
const userActions = ['ls', 'add', 'del'];
if (!this.args?.length || !userActions.includes(this.args[0])) {
console.log('');
console.log(program.commands[3].help());
} else {
user.userMenu(this.args, this.opts());
}
});
program
.command('producer')
.description('Producers usage commands')
.option('-s, --station <station-name>', 'Producers by station')
.option('--connected', 'Connected Producers')
.option('--disconnected', 'Disconnected Producers')
.option('--deleted', 'Deleted Producers')
.argument('<command>')
.usage('<command> [options]')
.showHelpAfterError()
.configureHelp({
sortSubcommands: true,
subcommandTerm: (cmd) => cmd.name() // Just show the name, instead of short usage.
})
.addHelpText('before', helper.producerDesc)
.addHelpText('after', `\n${helper.producerHelp}`)
.action(async function () {
await validateVersion();
const producerActions = ['ls'];
if (!this.args?.length || !producerActions.includes(this.args[0])) {
console.log('');
console.log(program.commands[4].help());
} else {
producer.producerMenu(this.args, this.opts());
}
});
program
.command('consumer')
.description('Consumer usage commands')
.option('-s, --station <station-name>', 'Consumers by station')
.option('--connected', 'Connected Producers')
.option('--disconnected', 'Disconnected Producers')
.option('--deleted', 'Deleted Producers')
.argument('<command>')
.usage('<command> [options]')
.showHelpAfterError()
.configureHelp({
sortSubcommands: true,
subcommandTerm: (cmd) => cmd.name() // Just show the name, instead of short usage.
})
.addHelpText('before', helper.consumerDesc)
.addHelpText('after', `\n${helper.consumerHelp}`)
.action(async function () {
await validateVersion();
const consumerActions = ['ls'];
if (!this.args?.length || !consumerActions.includes(this.args[0])) {
console.log('');
console.log(program.commands[5].help());
} else {
consumer.consumerMenu(this.args, this.opts());
}
});
program
.command('init')
.description('Creates an example project for working with Memphis')
.option('-l, --lang <language>', 'Language of project', 'nodejs')
.argument('[command]')
.usage('[options]')
.showHelpAfterError()
.action(async function () {
await validateVersion();
init.initMenu(this.args, this.opts());
});
program
.command('cluster')
.description('Memphis cluster commands')
.argument('info')
.showHelpAfterError()
.action(async function () {
await validateVersion();
cluster.clusterMenu(this.args);
});
//Prepare to hub command
// program
// .command('hub')
// .description('Memphis hub usage commands')
// .argument('<command>')
// .usage('<command> [options]')
// .showHelpAfterError()
// .configureHelp({
// sortSubcommands: true,
// subcommandTerm: (cmd) => cmd.name() // Just show the name, instead of short usage.
// })
// .addHelpText('before', helper.hubDesc)
// .addHelpText('after', `\n${helper.hubHelp}`)
// .action(async function () {
// await validateVersion();
// const userActions = ["login"]
// if (!this.args?.length || !userActions.includes(this.args[0])) {
// console.log(program.commands[4].help())
// }
// else {
// return
// }
// })
program.parse(process.argv);