Skip to main content

cmd/
cli.rs

1// Copyright 2023 Greptime Team
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::Parser;
16use cli::Tool;
17use common_telemetry::logging::{LoggingOptions, TracingOptions};
18use plugins::SubCommand;
19use snafu::ResultExt;
20use tracing_appender::non_blocking::WorkerGuard;
21
22use crate::options::GlobalOptions;
23use crate::{App, Result, error};
24pub const APP_NAME: &str = "greptime-cli";
25use async_trait::async_trait;
26
27pub struct Instance {
28    tool: Box<dyn Tool>,
29
30    // Keep the logging guard to prevent the worker from being dropped.
31    _guard: Vec<WorkerGuard>,
32}
33
34impl Instance {
35    pub fn new(tool: Box<dyn Tool>, guard: Vec<WorkerGuard>) -> Self {
36        Self {
37            tool,
38            _guard: guard,
39        }
40    }
41
42    pub async fn start(&mut self) -> Result<()> {
43        self.tool.do_work().await.context(error::StartCliSnafu)
44    }
45}
46
47#[async_trait]
48impl App for Instance {
49    fn name(&self) -> &str {
50        APP_NAME
51    }
52
53    async fn start(&mut self) -> Result<()> {
54        self.start().await
55    }
56
57    fn wait_signal(&self) -> bool {
58        false
59    }
60
61    async fn stop(&mut self) -> Result<()> {
62        Ok(())
63    }
64}
65
66#[derive(Parser)]
67pub struct Command {
68    #[clap(subcommand)]
69    cmd: SubCommand,
70}
71
72impl Command {
73    pub async fn build(&self, opts: LoggingOptions) -> Result<Instance> {
74        let guard = common_telemetry::init_global_logging(
75            APP_NAME,
76            &opts,
77            &TracingOptions::default(),
78            None,
79            None,
80        );
81
82        let tool = self.cmd.build().await.context(error::BuildCliSnafu)?;
83        let instance = Instance {
84            tool,
85            _guard: guard,
86        };
87        Ok(instance)
88    }
89
90    pub fn load_options(&self, global_options: &GlobalOptions) -> Result<LoggingOptions> {
91        let mut logging_opts = LoggingOptions::default();
92
93        if let Some(dir) = &global_options.log_dir {
94            logging_opts.dir.clone_from(dir);
95        }
96
97        logging_opts.level.clone_from(&global_options.log_level);
98
99        Ok(logging_opts)
100    }
101}
102
103#[cfg(test)]
104mod tests {
105    use std::net::TcpListener;
106    use std::ops::RangeInclusive;
107
108    use clap::Parser;
109    use client::{Client, Database};
110    use common_catalog::consts::{DEFAULT_CATALOG_NAME, DEFAULT_SCHEMA_NAME};
111    use common_telemetry::logging::LoggingOptions;
112    use rand::Rng;
113
114    use crate::error::Result as CmdResult;
115    use crate::options::GlobalOptions;
116    use crate::{App, cli, standalone};
117
118    fn random_standalone_addrs() -> (String, String, String, String) {
119        let offset = choose_random_unused_port_offset(14000..=24000, 10);
120
121        (
122            format!("127.0.0.1:{}", 4000 + offset),
123            format!("127.0.0.1:{}", 4001 + offset),
124            format!("127.0.0.1:{}", 4002 + offset),
125            format!("127.0.0.1:{}", 4003 + offset),
126        )
127    }
128
129    fn choose_random_unused_port_offset(
130        port_range: RangeInclusive<u16>,
131        max_attempts: usize,
132    ) -> u16 {
133        let mut rng = rand::rng();
134
135        for _ in 0..max_attempts {
136            let http_port = rng.random_range(port_range.clone());
137            let offset = http_port - 4000;
138            let ports = [4000 + offset, 4001 + offset, 4002 + offset, 4003 + offset];
139
140            let listeners = ports
141                .into_iter()
142                .map(|port| TcpListener::bind(("127.0.0.1", port)))
143                .collect::<Result<Vec<_>, _>>();
144
145            if listeners.is_ok() {
146                return offset;
147            }
148        }
149
150        panic!("failed to find unused standalone test ports");
151    }
152
153    #[tokio::test(flavor = "multi_thread")]
154    async fn test_export_create_table_with_quoted_names() -> CmdResult<()> {
155        let output_dir = tempfile::tempdir().unwrap();
156        let (http_addr, rpc_addr, mysql_addr, postgres_addr) = random_standalone_addrs();
157
158        let standalone = standalone::Command::parse_from([
159            "standalone",
160            "start",
161            "--data-home",
162            &*output_dir.path().to_string_lossy(),
163            "--http-addr",
164            &http_addr,
165            "--grpc-bind-addr",
166            &rpc_addr,
167            "--mysql-addr",
168            &mysql_addr,
169            "--postgres-addr",
170            &postgres_addr,
171        ]);
172
173        let standalone_opts = standalone.load_options(&GlobalOptions::default()).unwrap();
174        let mut instance = standalone.build(standalone_opts).await?;
175        instance.start().await?;
176
177        let client = Client::with_urls([rpc_addr.as_str()]);
178        let database = Database::new(DEFAULT_CATALOG_NAME, DEFAULT_SCHEMA_NAME, client);
179        database
180            .sql(r#"CREATE DATABASE "cli.export.create_table";"#)
181            .await
182            .unwrap();
183        database
184            .sql(
185                r#"CREATE TABLE "cli.export.create_table"."a.b.c"(
186                        ts TIMESTAMP,
187                        TIME INDEX (ts)
188                    ) engine=mito;
189                "#,
190            )
191            .await
192            .unwrap();
193
194        let output_dir = tempfile::tempdir().unwrap();
195        let cli = cli::Command::parse_from([
196            "cli",
197            "data",
198            "export",
199            "--addr",
200            &http_addr,
201            "--output-dir",
202            &*output_dir.path().to_string_lossy(),
203            "--target",
204            "schema",
205        ]);
206        let mut cli_app = cli.build(LoggingOptions::default()).await?;
207        cli_app.start().await?;
208
209        instance.stop().await?;
210
211        let output_file = output_dir
212            .path()
213            .join("greptime")
214            .join("cli.export.create_table")
215            .join("create_tables.sql");
216        let res = std::fs::read_to_string(output_file).unwrap();
217        let expect = r#"CREATE TABLE IF NOT EXISTS "a.b.c" (
218  "ts" TIMESTAMP(3) NOT NULL,
219  TIME INDEX ("ts")
220)
221
222ENGINE=mito
223;
224"#;
225        assert_eq!(res.trim(), expect.trim());
226
227        Ok(())
228    }
229}