frm-bin 0.11.0

Frakking RabbitMQ version Manager
Documentation
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
// Copyright (c) 2025-2026 Michael S. Klishin and Contributors
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use clap::{Arg, ArgAction, Command, ValueEnum};

use crate::commands::{CONFIG_FILES, RABBITMQ_TOOLS};
use crate::shell::Shell;

#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum CompletionShell {
    Bash,
    Elvish,
    Fish,
    #[value(name = "nushell", alias = "nu")]
    Nushell,
    #[value(name = "powershell")]
    PowerShell,
    Zsh,
}

pub fn build_cli() -> Command {
    Command::new("frm")
        .version(env!("CARGO_PKG_VERSION"))
        .author("Michael S. Klishin")
        .about("Frakking RabbitMQ version Manager")
        .arg_required_else_help(true)
        .subcommand(releases_command())
        .subcommand(alphas_command())
        .subcommand(tanzu_command())
        .subcommand(conf_command())
        .subcommand(use_command())
        .subcommand(default_command())
        .subcommand(cli_command())
        .subcommand(fg_command())
        .subcommand(inspect_command())
        .subcommand(shell_command())
}

fn releases_command() -> Command {
    Command::new("releases")
        .about("Install or manage RabbitMQ releases (GA, RCs, betas); for alphas, see the 'alphas' command group")
        .arg_required_else_help(true)
        .subcommand(releases_list_command())
        .subcommand(releases_path_command())
        .subcommand(releases_logs_command())
        .subcommand(releases_install_command())
        .subcommand(releases_reinstall_command())
        .subcommand(releases_uninstall_command())
        .subcommand(releases_completions_command())
}

fn releases_completions_command() -> Command {
    Command::new("completions")
        .about("Output installed release versions for shell completion")
        .hide(true)
        .arg(
            Arg::new("shell")
                .long("shell")
                .short('s')
                .help("Shell type (bash, zsh, nu)")
                .value_parser(clap::value_parser!(Shell)),
        )
}

fn releases_list_command() -> Command {
    Command::new("list")
        .visible_alias("ls")
        .about("List installed stable RabbitMQ releases")
}

fn releases_path_command() -> Command {
    Command::new("path")
        .about("Show the local path of an installed release")
        .long_about(
            "Show the local path of an installed release.\n\n\
            If no version is specified, tries to use the local .tool-versions file.",
        )
        .arg(version_arg())
}

fn releases_logs_command() -> Command {
    Command::new("logs")
        .about("Show RabbitMQ log file information for a release")
        .arg_required_else_help(true)
        .subcommand(
            Command::new("path")
                .about("Show the path to the log file")
                .arg(version_arg()),
        )
        .subcommand(
            Command::new("tail")
                .about("Show the last lines of the log file")
                .arg(version_arg())
                .arg(
                    Arg::new("lines")
                        .long("lines")
                        .short('n')
                        .help("Number of lines to show")
                        .default_value("10")
                        .value_parser(clap::value_parser!(usize)),
                ),
        )
}

fn releases_install_command() -> Command {
    Command::new("install")
        .visible_alias("i")
        .about("Install a stable RabbitMQ release")
        .long_about(
            "Install a stable RabbitMQ release (beta, rc, or GA).\n\n\
            If no version is specified, tries to use the local .tool-versions file.\n\n\
            Alpha versions are not allowed; use 'frm alphas install' instead.",
        )
        .arg(
            Arg::new("version")
                .help("Version to install (e.g., 4.2.3 or 4.2.0-rc.1)")
                .index(1),
        )
        .arg(
            Arg::new("force")
                .long("force")
                .short('f')
                .help("Force reinstallation if version exists")
                .action(ArgAction::SetTrue),
        )
}

fn releases_reinstall_command() -> Command {
    Command::new("reinstall")
        .about("Reinstall a stable RabbitMQ release")
        .long_about(
            "Reinstall a stable RabbitMQ release.\n\n\
            Removes the existing installation and downloads a fresh copy.\n\n\
            If no version is specified, tries to use the local .tool-versions file.",
        )
        .arg(
            Arg::new("version")
                .help("Version to reinstall (e.g., 4.2.3)")
                .index(1),
        )
}

fn releases_uninstall_command() -> Command {
    Command::new("uninstall")
        .visible_alias("rm")
        .about("Uninstall a stable RabbitMQ release")
        .long_about(
            "Uninstall a stable RabbitMQ release.\n\n\
            Use 'latest' to uninstall the most recent installed GA version.\n\n\
            If no version is specified, tries to use the local .tool-versions file.",
        )
        .arg(
            Arg::new("version")
                .help("Version to uninstall (e.g., 4.2.3 or 'latest')")
                .index(1),
        )
}

fn alphas_command() -> Command {
    Command::new("alphas")
        .about("Install, manage, rotate alpha RabbitMQ releases")
        .arg_required_else_help(true)
        .subcommand(alphas_list_command())
        .subcommand(alphas_path_command())
        .subcommand(alphas_logs_command())
        .subcommand(alphas_install_command())
        .subcommand(alphas_reinstall_command())
        .subcommand(alphas_uninstall_command())
        .subcommand(alphas_prune_command())
        .subcommand(alphas_clean_command())
        .subcommand(alphas_completions_command())
}

fn alphas_completions_command() -> Command {
    Command::new("completions")
        .about("Output installed alpha versions for shell completion")
        .hide(true)
        .arg(
            Arg::new("shell")
                .long("shell")
                .short('s')
                .help("Shell type (bash, zsh, nu)")
                .value_parser(clap::value_parser!(Shell)),
        )
}

fn alphas_list_command() -> Command {
    Command::new("list")
        .visible_alias("ls")
        .about("List installed alpha RabbitMQ releases")
}

fn alphas_path_command() -> Command {
    Command::new("path")
        .about("Show the local path of an installed alpha release")
        .long_about(
            "Show the local path of an installed alpha release.\n\n\
            If no version is specified, tries to use the local .tool-versions file.",
        )
        .arg(version_arg())
}

fn alphas_logs_command() -> Command {
    Command::new("logs")
        .about("Show RabbitMQ log file information for an alpha release")
        .arg_required_else_help(true)
        .subcommand(
            Command::new("path")
                .about("Show the path to the log file")
                .arg(version_arg()),
        )
        .subcommand(
            Command::new("tail")
                .about("Show the last lines of the log file")
                .arg(version_arg())
                .arg(
                    Arg::new("lines")
                        .long("lines")
                        .short('n')
                        .help("Number of lines to show")
                        .default_value("10")
                        .value_parser(clap::value_parser!(usize)),
                ),
        )
}

fn alphas_install_command() -> Command {
    Command::new("install")
        .visible_alias("i")
        .about("Install an alpha RabbitMQ release")
        .long_about(
            "Install an alpha RabbitMQ release from rabbitmq/server-packages.\n\n\
            Use --latest to automatically install the most recent alpha release.",
        )
        .arg(
            Arg::new("version")
                .help("Alpha version to install (e.g., 4.3.0-alpha.132057c7)")
                .index(1)
                .conflicts_with("latest"),
        )
        .arg(
            Arg::new("latest")
                .long("latest")
                .short('l')
                .help("Install the most recent alpha release")
                .action(ArgAction::SetTrue),
        )
        .arg(
            Arg::new("force")
                .long("force")
                .short('f')
                .help("Force reinstallation if version exists")
                .action(ArgAction::SetTrue),
        )
}

fn alphas_reinstall_command() -> Command {
    Command::new("reinstall")
        .about("Reinstall an alpha RabbitMQ release")
        .long_about(
            "Reinstall an alpha RabbitMQ release.\n\n\
            Removes the existing installation and downloads a fresh copy.\n\n\
            Use 'latest' to reinstall the most recent installed alpha version.\n\n\
            If no version is specified, tries to use the local .tool-versions file.",
        )
        .arg(
            Arg::new("version")
                .help("Alpha version to reinstall (e.g., 4.3.0-alpha.132057c7 or 'latest')")
                .index(1),
        )
}

fn alphas_uninstall_command() -> Command {
    Command::new("uninstall")
        .visible_alias("rm")
        .about("Uninstall an alpha RabbitMQ release")
        .long_about(
            "Uninstall an alpha RabbitMQ release.\n\n\
            Use 'latest' to uninstall the most recent installed alpha version.\n\n\
            If no version is specified, tries to use the local .tool-versions file.",
        )
        .arg(
            Arg::new("version")
                .help("Alpha version to uninstall (e.g., 4.3.0-alpha.132057c7 or 'latest')")
                .index(1),
        )
}

fn alphas_prune_command() -> Command {
    Command::new("prune")
        .about("Remove all installed alpha releases")
        .long_about("Remove all installed alpha releases to free up disk space.")
}

fn alphas_clean_command() -> Command {
    Command::new("clean")
        .about("Remove alpha releases older than a specified time")
        .long_about(
            "Remove alpha releases older than a specified time.\n\n\
            The --older-than flag accepts human-readable time strings like:\n\
            - \"2 weeks ago\"\n\
            - \"1 month ago\"\n\
            - \"yesterday\"\n\
            - \"2025-01-01\" (absolute date)",
        )
        .arg(
            Arg::new("older_than")
                .long("older-than")
                .help("Remove alphas installed before this time (e.g., \"2 weeks ago\")")
                .required(true)
                .value_name("TIME"),
        )
}

fn tanzu_command() -> Command {
    Command::new("tanzu")
        .about("Install Tanzu RabbitMQ from local tarballs")
        .arg_required_else_help(true)
        .subcommand(tanzu_install_command())
}

fn tanzu_install_command() -> Command {
    Command::new("install")
        .visible_alias("i")
        .about("Install Tanzu RabbitMQ from a local tarball")
        .long_about(
            "Install Tanzu RabbitMQ from a local tarball.\n\n\
            Requires both the tarball path and the expected version.\n\
            The version in the tarball filename must match the specified version.\n\n\
            Supported formats: .tar.xz, .tar.gz, .tgz",
        )
        .arg(
            Arg::new("tarball_path")
                .long("local-tanzu-rabbitmq-tarball-path")
                .help("Path to the local Tanzu RabbitMQ tarball")
                .required(true)
                .value_name("PATH"),
        )
        .arg(
            Arg::new("version")
                .long("version")
                .short('V')
                .help("Expected RabbitMQ version (e.g., 4.2.3 or 4.2.3-rc.1)")
                .required(true)
                .value_name("VERSION"),
        )
        .arg(
            Arg::new("force")
                .long("force")
                .short('f')
                .help("Force reinstallation if version exists")
                .action(ArgAction::SetTrue),
        )
}

fn conf_command() -> Command {
    Command::new("conf")
        .about("Manage RabbitMQ configuration files")
        .arg_required_else_help(true)
        .subcommand(conf_get_key_command())
        .subcommand(conf_set_key_command())
}

fn conf_get_key_command() -> Command {
    Command::new("get-key")
        .about("Get a configuration key value from rabbitmq.conf")
        .long_about(
            "Get a configuration key value from rabbitmq.conf.\n\n\
            Supports pattern matching with * as a wildcard for a single segment:\n\n \
            * `listeners.tcp.*` matches `listeners.tcp.default`, `listeners.tcp.amqp`, etc.\n \
            * `log.*.level` matches `log.console.level`, `log.file.level`, etc.\n\n\
            If no version is specified, tries to use the local .tool-versions file.",
        )
        .arg(
            Arg::new("key")
                .help("Configuration key or pattern (e.g., listeners.tcp.* or heartbeat)")
                .required(true)
                .index(1),
        )
        .arg(version_arg())
}

fn conf_set_key_command() -> Command {
    Command::new("set-key")
        .about("Set a configuration key value in rabbitmq.conf")
        .long_about(
            "Set a configuration key value in rabbitmq.conf.\n\n\
            If no version is specified, tries to use the local .tool-versions file.\n\n\
            Keys are validated against the known RabbitMQ configuration schema.\n\
            Use --force to set unknown keys.",
        )
        .arg(
            Arg::new("key")
                .help("Configuration key (e.g., listeners.tcp.default)")
                .required(true)
                .index(1),
        )
        .arg(
            Arg::new("value")
                .help("Value to set")
                .required(true)
                .index(2),
        )
        .arg(version_arg())
        .arg(
            Arg::new("force")
                .long("force")
                .short('f')
                .help("Set the key even if it's not recognized")
                .action(ArgAction::SetTrue),
        )
}

fn use_command() -> Command {
    Command::new("use")
        .about("Output shell commands to use a specific version")
        .long_about(
            "Output shell commands to use a specific version.\n\n\
            Use 'latest' to select the most recent installed GA version.\n\n\
            If no version is specified, tries to use the local .tool-versions file.\n\n\
            bash/zsh: eval \"$(frm use [version])\"\n\
            nushell:  Use 'frm env nu' init script, then call 'frm-use [version]'",
        )
        .arg(
            Arg::new("version")
                .help("Version to use (e.g., 4.2.3 or 'latest'), or uses .tool-versions")
                .index(1),
        )
        .arg(
            Arg::new("shell")
                .long("shell")
                .short('s')
                .help("Shell type (bash, zsh, nu)")
                .value_parser(clap::value_parser!(Shell)),
        )
}

fn default_command() -> Command {
    Command::new("default")
        .about("Set the default RabbitMQ version")
        .long_about(
            "Set the default RabbitMQ version.\n\n\
            Use 'latest' to select the most recent installed GA version.",
        )
        .arg(
            Arg::new("version")
                .help("Version to set as default (e.g., 4.2.3 or 'latest')")
                .required(true)
                .index(1),
        )
}

fn shell_command() -> Command {
    Command::new("shell")
        .about("Shell-related operations")
        .arg_required_else_help(true)
        .subcommand(shell_completions_command())
        .subcommand(shell_env_command())
}

fn shell_env_command() -> Command {
    Command::new("env")
        .about("Output shell initialization script")
        .long_about(
            "Output shell initialization script.\n\n\
            Add to your shell profile:\n\
            - bash: eval \"$(frm shell env bash)\" in ~/.bashrc\n\
            - zsh: eval \"$(frm shell env zsh)\" in ~/.zshrc\n\
            - nu: frm shell env nu | save -f ~/.local/frm/env.nu, then source in config.nu\n\n\
            After setup, use 'frm-use <version>' to switch versions.",
        )
        .arg(
            Arg::new("shell")
                .help("Shell type (bash, zsh, nu)")
                .required(true)
                .index(1)
                .value_parser(clap::value_parser!(Shell)),
        )
}

fn shell_completions_command() -> Command {
    Command::new("completions")
        .about("Generate shell completions")
        .arg(
            Arg::new("shell")
                .help("Target shell (bash, elvish, fish, nushell, powershell, zsh)")
                .required(true)
                .index(1)
                .value_parser(clap::value_parser!(CompletionShell)),
        )
}

fn cli_command() -> Command {
    Command::new("cli")
        .about("Run a RabbitMQ CLI tool")
        .long_about(format!(
            "Run a RabbitMQ CLI tool from the specified version.\n\n\
            Available tools: {}\n\n\
            If no version is specified, tries to use the local .tool-versions file.\n\n\
            Use -- to separate tool arguments from frm options:\n\
            frm cli rabbitmqctl -V 4.2.3 -- status",
            RABBITMQ_TOOLS.join(", ")
        ))
        .trailing_var_arg(true)
        .arg(Arg::new("tool").help("Tool to run").required(true).index(1))
        .arg(version_arg())
        .arg(
            Arg::new("args")
                .help("Arguments to pass to the tool (after --)")
                .num_args(1..)
                .index(2),
        )
}

fn fg_command() -> Command {
    Command::new("fg")
        .about("Run RabbitMQ nodes in foreground")
        .arg_required_else_help(true)
        .subcommand(
            Command::new("node")
                .about("Start RabbitMQ server in foreground")
                .long_about(
                    "Start RabbitMQ server in foreground.\n\n\
                    If no version is specified, tries to use the local .tool-versions file.",
                )
                .arg(version_arg()),
        )
}

fn inspect_command() -> Command {
    Command::new("inspect")
        .about("Inspect a RabbitMQ configuration file")
        .long_about(format!(
            "Inspect a RabbitMQ configuration file from the specified version.\n\n\
            Available files: {}\n\n\
            If no version is specified, tries to use the local .tool-versions file.",
            CONFIG_FILES.join(", ")
        ))
        .arg(
            Arg::new("file")
                .help("Configuration file to inspect")
                .required(true)
                .index(1),
        )
        .arg(version_arg())
}

fn version_arg() -> Arg {
    Arg::new("version")
        .long("version")
        .short('V')
        .help("RabbitMQ version to use, or uses .tool-versions")
        .value_name("VERSION")
}