Skip to content

Commit a3b5b38

Browse files
committed
config
1 parent 40136f2 commit a3b5b38

File tree

9 files changed

+138
-96
lines changed

9 files changed

+138
-96
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/binaries/query/cmd.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2021 Datafuse Labs
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+
15+
use clap::Parser;
16+
use clap::Subcommand;
17+
use databend_common_config::Config;
18+
use databend_common_config::InnerConfig;
19+
use databend_common_exception::Result;
20+
use databend_common_version::DATABEND_COMMIT_VERSION;
21+
22+
/// Databend
23+
#[derive(Parser, Clone)]
24+
#[command(name = "databend-query")]
25+
#[command(about = "Databend: The Next-Gen Cloud [Data+AI] Analytics.")]
26+
#[command(version = &**DATABEND_COMMIT_VERSION)]
27+
pub struct Cmd {
28+
/// Run a command and quit
29+
#[command(subcommand)]
30+
pub subcommand: Option<Commands>,
31+
32+
/// To be compatible with the old version, we keep the `cmd` arg
33+
/// We should always use `databend-query ver` instead `databend-query --cmd ver` in latest version
34+
#[clap(long)]
35+
pub cmd: Option<String>,
36+
37+
#[clap(long, short = 'c', value_name = "PATH", default_value_t)]
38+
pub config_file: String,
39+
40+
#[clap(flatten)]
41+
pub config: Config,
42+
}
43+
44+
impl Cmd {
45+
pub fn normalize(&mut self) {
46+
if self.cmd == Some("ver".to_string()) {
47+
self.subcommand = Some(Commands::Ver);
48+
}
49+
}
50+
51+
pub async fn init_inner_config(self, check_meta: bool) -> Result<InnerConfig> {
52+
let Cmd {
53+
config,
54+
config_file,
55+
..
56+
} = self;
57+
58+
let config = config.merge(&config_file).unwrap();
59+
InnerConfig::init(config, check_meta).await
60+
}
61+
}
62+
63+
#[derive(Subcommand, Clone)]
64+
pub enum Commands {
65+
#[command(about = "Print version and quit")]
66+
Ver,
67+
Local {
68+
#[clap(long, short = 'q', default_value_t)]
69+
query: String,
70+
#[clap(long, default_value_t)]
71+
output_format: String,
72+
#[clap(long, short = 'c')]
73+
config: String,
74+
},
75+
}

src/binaries/query/ee_main.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
#![allow(clippy::uninlined_format_args)]
1616
#![feature(try_blocks)]
1717

18+
mod cmd;
1819
mod entry;
1920

21+
use clap::Parser;
2022
use databend_common_base::mem_allocator::TrackingGlobalAllocator;
2123
use databend_common_base::runtime::Runtime;
2224
use databend_common_base::runtime::ThreadTracker;
23-
use databend_common_config::InnerConfig;
2425
use databend_common_exception::Result;
2526
use databend_common_exception::ResultExt;
2627
use databend_common_tracing::pipe_file;
@@ -30,6 +31,7 @@ use databend_common_version::DATABEND_COMMIT_VERSION;
3031
use databend_enterprise_query::enterprise_services::EnterpriseServices;
3132
use entry::MainError;
3233

34+
use self::cmd::Cmd;
3335
use crate::entry::init_services;
3436
use crate::entry::run_cmd;
3537
use crate::entry::start_services;
@@ -63,11 +65,15 @@ fn main() {
6365
pub async fn main_entrypoint() -> Result<(), MainError> {
6466
let make_error = || "an fatal error occurred in query";
6567

66-
let conf: InnerConfig = InnerConfig::load().await.with_context(make_error)?;
67-
if run_cmd(&conf).await.with_context(make_error)? {
68+
// if the usage is print, std::process::exit() will be called.
69+
let mut cmd = Cmd::parse();
70+
cmd.normalize();
71+
72+
if run_cmd(&cmd).await.with_context(make_error)? {
6873
return Ok(());
6974
}
7075

76+
let conf = cmd.init_inner_config(true).await.with_context(make_error)?;
7177
init_services(&conf, true).await.with_context(make_error)?;
7278
EnterpriseServices::init(conf.clone())
7379
.await

src/binaries/query/entry.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use std::time::Duration;
1818
use databend_common_base::mem_allocator::TrackingGlobalAllocator;
1919
use databend_common_base::runtime::set_alloc_error_hook;
2020
use databend_common_base::runtime::GLOBAL_MEM_STAT;
21-
use databend_common_config::Commands;
2221
use databend_common_config::InnerConfig;
2322
use databend_common_exception::ErrorCode;
2423
use databend_common_exception::Result;
@@ -47,12 +46,15 @@ use databend_query::servers::ShutdownHandle;
4746
use databend_query::GlobalServices;
4847
use log::info;
4948

49+
use super::cmd::Cmd;
50+
use super::cmd::Commands;
51+
5052
pub struct MainError;
5153

52-
pub async fn run_cmd(conf: &InnerConfig) -> Result<bool, MainError> {
54+
pub async fn run_cmd(cmd: &Cmd) -> Result<bool, MainError> {
5355
let make_error = || "failed to run cmd";
5456

55-
match &conf.subcommand {
57+
match &cmd.subcommand {
5658
None => return Ok(false),
5759
Some(Commands::Ver) => {
5860
println!("version: {}", *DATABEND_SEMVER);
@@ -63,12 +65,14 @@ pub async fn run_cmd(conf: &InnerConfig) -> Result<bool, MainError> {
6365
output_format,
6466
config,
6567
}) => {
66-
let mut conf = conf.clone();
68+
let mut cmd = cmd.clone();
6769
if !config.is_empty() {
68-
let c =
69-
databend_common_config::Config::load_with_config_file(config.as_str()).unwrap();
70-
conf = c.try_into().unwrap();
70+
cmd.config_file = config.to_string();
7171
}
72+
let conf = cmd
73+
.init_inner_config(false)
74+
.await
75+
.with_context(make_error)?;
7276
local::query_local(conf, query, output_format)
7377
.await
7478
.with_context(make_error)?

src/binaries/query/oss_main.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
#![allow(clippy::uninlined_format_args)]
1616
#![feature(try_blocks)]
1717

18+
mod cmd;
1819
mod entry;
1920

21+
use clap::Parser;
2022
use databend_common_base::mem_allocator::TrackingGlobalAllocator;
2123
use databend_common_base::runtime::Runtime;
2224
use databend_common_base::runtime::ThreadTracker;
23-
use databend_common_config::InnerConfig;
2425
use databend_common_exception::Result;
2526
use databend_common_exception::ResultExt;
2627
use databend_common_license::license_manager::LicenseManager;
@@ -31,6 +32,7 @@ use databend_common_tracing::SignalListener;
3132
use databend_common_version::DATABEND_COMMIT_VERSION;
3233
use entry::MainError;
3334

35+
use self::cmd::Cmd;
3436
use crate::entry::init_services;
3537
use crate::entry::run_cmd;
3638
use crate::entry::start_services;
@@ -64,11 +66,15 @@ fn main() {
6466
async fn main_entrypoint() -> Result<(), MainError> {
6567
let make_error = || "an fatal error occurred in query";
6668

67-
let conf: InnerConfig = InnerConfig::load().await.with_context(make_error)?;
68-
if run_cmd(&conf).await.with_context(make_error)? {
69+
// if the usage is print, std::process::exit() will be called.
70+
let mut cmd = Cmd::parse();
71+
cmd.normalize();
72+
73+
if run_cmd(&cmd).await.with_context(make_error)? {
6974
return Ok(());
7075
}
7176

77+
let conf = cmd.init_inner_config(true).await.with_context(make_error)?;
7278
init_services(&conf, false).await?;
7379
// init oss license manager
7480
OssLicenseManager::init(conf.query.tenant_id.tenant_name().to_string())

src/query/config/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ databend-common-grpc = { workspace = true }
2424
databend-common-meta-app = { workspace = true }
2525
databend-common-storage = { workspace = true }
2626
databend-common-tracing = { workspace = true }
27-
databend-common-version = { workspace = true }
2827
log = { workspace = true }
2928
serde = { workspace = true }
3029
serde_ignored = { workspace = true }

0 commit comments

Comments
 (0)