1use 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::{error, App, Result};
24pub const APP_NAME: &str = "greptime-cli";
25use async_trait::async_trait;
26
27pub struct Instance {
28 tool: Box<dyn Tool>,
29
30 _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 clap::Parser;
106 use client::{Client, Database};
107 use common_catalog::consts::{DEFAULT_CATALOG_NAME, DEFAULT_SCHEMA_NAME};
108 use common_telemetry::logging::LoggingOptions;
109
110 use crate::error::Result as CmdResult;
111 use crate::options::GlobalOptions;
112 use crate::{cli, standalone, App};
113
114 #[tokio::test(flavor = "multi_thread")]
115 async fn test_export_create_table_with_quoted_names() -> CmdResult<()> {
116 let output_dir = tempfile::tempdir().unwrap();
117
118 let standalone = standalone::Command::parse_from([
119 "standalone",
120 "start",
121 "--data-home",
122 &*output_dir.path().to_string_lossy(),
123 ]);
124
125 let standalone_opts = standalone.load_options(&GlobalOptions::default()).unwrap();
126 let mut instance = standalone.build(standalone_opts).await?;
127 instance.start().await?;
128
129 let client = Client::with_urls(["127.0.0.1:4001"]);
130 let database = Database::new(DEFAULT_CATALOG_NAME, DEFAULT_SCHEMA_NAME, client);
131 database
132 .sql(r#"CREATE DATABASE "cli.export.create_table";"#)
133 .await
134 .unwrap();
135 database
136 .sql(
137 r#"CREATE TABLE "cli.export.create_table"."a.b.c"(
138 ts TIMESTAMP,
139 TIME INDEX (ts)
140 ) engine=mito;
141 "#,
142 )
143 .await
144 .unwrap();
145
146 let output_dir = tempfile::tempdir().unwrap();
147 let cli = cli::Command::parse_from([
148 "cli",
149 "export",
150 "--addr",
151 "127.0.0.1:4000",
152 "--output-dir",
153 &*output_dir.path().to_string_lossy(),
154 "--target",
155 "schema",
156 ]);
157 let mut cli_app = cli.build(LoggingOptions::default()).await?;
158 cli_app.start().await?;
159
160 instance.stop().await?;
161
162 let output_file = output_dir
163 .path()
164 .join("greptime")
165 .join("cli.export.create_table")
166 .join("create_tables.sql");
167 let res = std::fs::read_to_string(output_file).unwrap();
168 let expect = r#"CREATE TABLE IF NOT EXISTS "a.b.c" (
169 "ts" TIMESTAMP(3) NOT NULL,
170 TIME INDEX ("ts")
171)
172
173ENGINE=mito
174;
175"#;
176 assert_eq!(res.trim(), expect.trim());
177
178 Ok(())
179 }
180}