Skip to content

refactor: splitting clap::Parser from Config #18389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions src/binaries/query/cmd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2021 Datafuse Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use clap::Parser;
use clap::Subcommand;
use databend_common_config::Config;
use databend_common_config::InnerConfig;
use databend_common_exception::Result;
use databend_common_version::DATABEND_COMMIT_VERSION;

/// Databend
#[derive(Parser, Clone)]
#[command(name = "databend-query")]
#[command(about = "Databend: The Next-Gen Cloud [Data+AI] Analytics.")]
#[command(version = &**DATABEND_COMMIT_VERSION)]
pub struct Cmd {
/// Run a command and quit
#[command(subcommand)]
pub subcommand: Option<Commands>,

/// To be compatible with the old version, we keep the `cmd` arg
/// We should always use `databend-query ver` instead `databend-query --cmd ver` in latest version
#[clap(long)]
pub cmd: Option<String>,

#[clap(long, short = 'c', value_name = "PATH", default_value_t)]
pub config_file: String,

#[clap(flatten)]
pub config: Config,
}

impl Cmd {
pub fn normalize(&mut self) {
if self.cmd == Some("ver".to_string()) {
self.subcommand = Some(Commands::Ver);
}
}

pub async fn init_inner_config(self, check_meta: bool) -> Result<InnerConfig> {
let Cmd {
config,
config_file,
..
} = self;

let config = config.merge(&config_file).unwrap();
InnerConfig::init(config, check_meta).await
}
}

#[derive(Subcommand, Clone)]
pub enum Commands {
#[command(about = "Print version and quit")]
Ver,
Local {
#[clap(long, short = 'q', default_value_t)]
query: String,
#[clap(long, default_value_t)]
output_format: String,
#[clap(long, short = 'c')]
config: String,
},
}
12 changes: 9 additions & 3 deletions src/binaries/query/ee_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
#![allow(clippy::uninlined_format_args)]
#![feature(try_blocks)]

mod cmd;
mod entry;

use clap::Parser;
use databend_common_base::mem_allocator::TrackingGlobalAllocator;
use databend_common_base::runtime::Runtime;
use databend_common_base::runtime::ThreadTracker;
use databend_common_config::InnerConfig;
use databend_common_exception::Result;
use databend_common_exception::ResultExt;
use databend_common_tracing::pipe_file;
Expand All @@ -30,6 +31,7 @@ use databend_common_version::DATABEND_COMMIT_VERSION;
use databend_enterprise_query::enterprise_services::EnterpriseServices;
use entry::MainError;

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

let conf: InnerConfig = InnerConfig::load().await.with_context(make_error)?;
if run_cmd(&conf).await.with_context(make_error)? {
// if the usage is print, std::process::exit() will be called.
let mut cmd = Cmd::parse();
cmd.normalize();

if run_cmd(&cmd).await.with_context(make_error)? {
return Ok(());
}

let conf = cmd.init_inner_config(true).await.with_context(make_error)?;
init_services(&conf, true).await.with_context(make_error)?;
EnterpriseServices::init(conf.clone())
.await
Expand Down
18 changes: 11 additions & 7 deletions src/binaries/query/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use std::time::Duration;
use databend_common_base::mem_allocator::TrackingGlobalAllocator;
use databend_common_base::runtime::set_alloc_error_hook;
use databend_common_base::runtime::GLOBAL_MEM_STAT;
use databend_common_config::Commands;
use databend_common_config::InnerConfig;
use databend_common_exception::ErrorCode;
use databend_common_exception::Result;
Expand Down Expand Up @@ -47,12 +46,15 @@ use databend_query::servers::ShutdownHandle;
use databend_query::GlobalServices;
use log::info;

use super::cmd::Cmd;
use super::cmd::Commands;

pub struct MainError;

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

match &conf.subcommand {
match &cmd.subcommand {
None => return Ok(false),
Some(Commands::Ver) => {
println!("version: {}", *DATABEND_SEMVER);
Expand All @@ -63,12 +65,14 @@ pub async fn run_cmd(conf: &InnerConfig) -> Result<bool, MainError> {
output_format,
config,
}) => {
let mut conf = conf.clone();
let mut cmd = cmd.clone();
if !config.is_empty() {
let c =
databend_common_config::Config::load_with_config_file(config.as_str()).unwrap();
conf = c.try_into().unwrap();
cmd.config_file = config.to_string();
}
let conf = cmd
.init_inner_config(false)
.await
.with_context(make_error)?;
local::query_local(conf, query, output_format)
.await
.with_context(make_error)?
Expand Down
12 changes: 9 additions & 3 deletions src/binaries/query/oss_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
#![allow(clippy::uninlined_format_args)]
#![feature(try_blocks)]

mod cmd;
mod entry;

use clap::Parser;
use databend_common_base::mem_allocator::TrackingGlobalAllocator;
use databend_common_base::runtime::Runtime;
use databend_common_base::runtime::ThreadTracker;
use databend_common_config::InnerConfig;
use databend_common_exception::Result;
use databend_common_exception::ResultExt;
use databend_common_license::license_manager::LicenseManager;
Expand All @@ -31,6 +32,7 @@ use databend_common_tracing::SignalListener;
use databend_common_version::DATABEND_COMMIT_VERSION;
use entry::MainError;

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

let conf: InnerConfig = InnerConfig::load().await.with_context(make_error)?;
if run_cmd(&conf).await.with_context(make_error)? {
// if the usage is print, std::process::exit() will be called.
let mut cmd = Cmd::parse();
cmd.normalize();

if run_cmd(&cmd).await.with_context(make_error)? {
return Ok(());
}

let conf = cmd.init_inner_config(true).await.with_context(make_error)?;
init_services(&conf, false).await?;
// init oss license manager
OssLicenseManager::init(conf.query.tenant_id.tenant_name().to_string())
Expand Down
1 change: 0 additions & 1 deletion src/query/config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ databend-common-grpc = { workspace = true }
databend-common-meta-app = { workspace = true }
databend-common-storage = { workspace = true }
databend-common-tracing = { workspace = true }
databend-common-version = { workspace = true }
log = { workspace = true }
serde = { workspace = true }
serde_ignored = { workspace = true }
Expand Down
Loading
Loading