Skip to main content

rocketmq_admin_core/
rocketmq_cli.rs

1// Copyright 2023 The RocketMQ Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use clap::CommandFactory;
16use clap::Parser;
17use clap_complete::generate;
18use clap_complete::shells::Bash;
19use clap_complete::shells::Fish;
20use clap_complete::shells::Zsh;
21
22use crate::commands::CommandExecute;
23use crate::commands::Commands;
24
25#[derive(Parser)]
26#[command(name = "rocketmq-admin-cli-rust")]
27#[command(about = "Rocketmq Rust admin commands", long_about = None, author="mxsm")]
28pub struct RocketMQCli {
29    #[arg(
30        long = "generate-completion",
31        value_name = "SHELL",
32        help = "Generate shell completion script (bash, zsh, fish)"
33    )]
34    completion: Option<String>,
35
36    #[command(subcommand)]
37    commands: Option<Commands>,
38}
39
40impl RocketMQCli {
41    pub async fn handle(&self) {
42        // Handle completion generation
43        if let Some(shell) = &self.completion {
44            let mut cmd = RocketMQCli::command();
45            let bin_name = "rocketmq-admin-cli-rust";
46
47            match shell.to_lowercase().as_str() {
48                "bash" => {
49                    generate(Bash, &mut cmd, bin_name, &mut std::io::stdout());
50                }
51                "zsh" => {
52                    generate(Zsh, &mut cmd, bin_name, &mut std::io::stdout());
53                }
54                "fish" => {
55                    generate(Fish, &mut cmd, bin_name, &mut std::io::stdout());
56                }
57                _ => {
58                    eprintln!("Unsupported shell: {}", shell);
59                    eprintln!("Supported shells: bash, zsh, fish");
60                    std::process::exit(1);
61                }
62            }
63            return;
64        }
65
66        // Handle regular commands
67        if let Some(ref commands) = self.commands {
68            if let Err(e) = commands.execute(None).await {
69                eprintln!("Error: {e}");
70            }
71        } else {
72            eprintln!("No command specified. Use --help for usage information.");
73        }
74    }
75}